-
Notifications
You must be signed in to change notification settings - Fork 0
/
ws.worker.ts
72 lines (68 loc) · 2.78 KB
/
ws.worker.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import { WorkerMessage, WebSocketMessage } from "./pkg/wasm_sync_ws_client";
let ws: WebSocket;
let responseArray: Int32Array;
self.addEventListener("message", (event: MessageEvent) => {
if (event.data.constructor == SharedArrayBuffer) {
let message: SharedArrayBuffer = event.data;
responseArray = new Int32Array(message);
} else {
let message: WorkerMessage = event.data;
if (message.connect) {
ws = new WebSocket(message.connect);
ws.addEventListener("open", (event) => {
console.log("Ws connected!");
// notify wasm thread that connection is open
self.postMessage("connected");
}),
ws.addEventListener("close", (event) => {
console.log("Ws closed!");
// notify wasm thread that connection is closed
self.postMessage("closed");
}),
ws.addEventListener(
"message",
(event: MessageEvent<string | Uint8Array>) => {
if (event.data.constructor == Uint8Array) {
// received binary message
let bytes: Uint8Array = event.data;
storeBytesAndNotify(bytes);
} else if (event.data.constructor == String) {
// received text message
let message: string = event.data;
// encode message in utf8
let bytes = new TextEncoder().encode(message);
storeBytesAndNotify(bytes);
}
}
),
console.log("Websocket created!");
} else if (message.request) {
let request: WebSocketMessage = message.request;
if (request.binary) {
if (ws.readyState == ws.OPEN) {
ws.send(request.binary);
}
} else if (request.text) {
if (ws.readyState == ws.OPEN) {
ws.send(request.text);
}
}
} else if (message.close) {
console.log("Closing websocket...");
ws.close();
}
}
});
function storeBytesAndNotify(bytes: Uint8Array) {
for (let i = 0; i < responseArray.length; i++) {
if (i < bytes.length) {
// store response bytes in response array
Atomics.store(responseArray, i, bytes[i]);
} else {
// store neutral value 256 in remaining response array slots
Atomics.store(responseArray, i, 256);
}
}
// notify wasm thread that response is ready
Atomics.notify(responseArray, 0, 1);
}