2 lines
598 B
JavaScript
2 lines
598 B
JavaScript
"use strict";const a=[];let t=null;function n(o){t=new WebSocket(o),t.onclose=e=>console.log("ws closed",e),t.onerror=e=>console.log("ws error",e),t.onmessage=({data:e})=>{const c=JSON.parse(e);a.forEach(s=>s.postMessage(c))}}function r(){t&&t.readyState!=WebSocket.OPEN||t.close()}function l(){this.postMessage({readyState:t?t.readyState:WebSocket.CLOSED})}self.onconnect=o=>{const e=o.ports[0];e.onmessage=({data:{command:c,data:s}})=>{switch(c){case"initWs":return Reflect.apply(n,e,s||[]);case"closeWs":return Reflect.apply(r,e,s||[]);case"status":return Reflect.apply(l,e,s||[])}},a.push(e)};
|