22 lines
519 B
JavaScript
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);
|
|
};
|