Support binary websockets

This commit is contained in:
Andrew Henry 2024-01-26 10:35:34 -08:00
parent 12b921a006
commit 4cc0a9e402

View File

@ -32,7 +32,7 @@ export default function installWorker() {
* On an error or dropout, will automatically reconnect. * On an error or dropout, will automatically reconnect.
* *
* Additionally, messages will be queued and sent only when WebSocket is * Additionally, messages will be queued and sent only when WebSocket is
* connected meaning that client code does not need to check the state of * connected meaning that client code does not need to check the state of
* the socket before sending. * the socket before sending.
*/ */
class ResilientWebSocket extends EventTarget { class ResilientWebSocket extends EventTarget {
@ -61,7 +61,10 @@ export default function installWorker() {
this.#isConnecting = true; this.#isConnecting = true;
this.#webSocket = new WebSocket(url); //TODO: Make this configurable
this.#webSocket = new WebSocket(url, 'protobuf');
//TODO: Make this configurable
this.#webSocket.binaryType = 'arraybuffer';
const boundConnected = this.#connected.bind(this); const boundConnected = this.#connected.bind(this);
this.#webSocket.addEventListener('open', boundConnected); this.#webSocket.addEventListener('open', boundConnected);
@ -249,10 +252,15 @@ export default function installWorker() {
if (this.#messageBatcher.shouldBatchMessage(data)) { if (this.#messageBatcher.shouldBatchMessage(data)) {
this.#messageBatcher.addMessageToBatch(data); this.#messageBatcher.addMessageToBatch(data);
} else { } else {
this.#worker.postMessage({ this.#worker.postMessage(
type: 'message', {
message: data type: 'message',
}); message: data
},
{
transfer: data
}
);
} }
} }
} }
@ -267,11 +275,13 @@ export default function installWorker() {
#maxBatchSize; #maxBatchSize;
#readyForNextBatch; #readyForNextBatch;
#worker; #worker;
#transferables;
constructor(worker) { constructor(worker) {
this.#maxBatchSize = 10; this.#maxBatchSize = 10;
this.#readyForNextBatch = false; this.#readyForNextBatch = false;
this.#worker = worker; this.#worker = worker;
this.#transferables = [];
this.#resetBatch(); this.#resetBatch();
} }
#resetBatch() { #resetBatch() {
@ -323,6 +333,7 @@ export default function installWorker() {
} else { } else {
this.#hasBatch = true; this.#hasBatch = true;
} }
this.#transferables.push(message);
} }
setMaxBatchSize(maxBatchSize) { setMaxBatchSize(maxBatchSize) {
this.#maxBatchSize = maxBatchSize; this.#maxBatchSize = maxBatchSize;
@ -343,12 +354,18 @@ export default function installWorker() {
#sendNextBatch() { #sendNextBatch() {
const batch = this.#batch; const batch = this.#batch;
this.#resetBatch(); this.#resetBatch();
this.#worker.postMessage({ this.#worker.postMessage(
type: 'batch', {
batch type: 'batch',
}); batch
},
{
transfer: this.#transferables
}
);
this.#readyForNextBatch = false; this.#readyForNextBatch = false;
this.#hasBatch = false; this.#hasBatch = false;
this.#transferables = [];
} }
} }