silphius/app/net/client/prediction.js
2024-06-12 23:17:24 -05:00

22 lines
519 B
JavaScript

let connected = false;
let socket;
onmessage = async (event) => {
if (!connected) {
const url = new URL(`wss://${event.data.host}/ws`)
if ('production' === process.env.NODE_ENV) {
url.protocol = 'ws:';
}
socket = new WebSocket(url.href);
socket.binaryType = 'arraybuffer';
await new Promise((resolve) => {
socket.onopen = resolve;
});
socket.onmessage = (event) => {
postMessage(event.data);
};
connected = true;
return;
}
socket.send(event.data);
};