(function(){vare,n,t,s,i;(t=classe{constructor(){this.header={sid:0,cid:0,type:0,size:0},this.data=void0}as_raw(){varn,t,s;returns=13+this.header.size,(n=newUint8Array(s)).set(e.MAGIC_START,0),n[2]=this.header.type,t=e.bytes_of(this.header.cid),n.set(t,3),t=e.bytes_of(this.header.sid),n.set(t,5),t=e.bytes_of(this.header.size,4),n.set(t,7),this.data&&n.set(this.data,11),n.set(e.MAGIC_END,this.header.size+11),n.buffer}}).decode=function(e){returnnewPromise((function(n,s){vari;returni=newt,t.int_from(t.MAGIC_START,0)!==t.int_from(e,0)?s("Unmatch message begin magic number"):(i.header.type=e[2],i.header.cid=t.int_from(e,3),i.header.sid=t.int_from(e,5),i.header.size=t.int_from(e,7,4),i.data=e.slice(11,11+i.header.size),t.int_from(t.MAGIC_END,0)!==t.int_from(e,11+i.header.size)?s("Unmatch message end magic number"):n(i))}))},t.bytes_of=function(e,n){vart;return4!==n&&(n=2),(t=newUint8Array(n))[0]=255&e,e>>=8,t[1]=255&e,4!==n||(e>>=8,t[2]=255&e,e>>=8,t[3]=255&e),t},t.int_from=function(e,n,t){return4!==t?e[n]|e[n+1]<<8:e[n]|e[n+1]<<8|e[n+2]<<16|e[n+3]<<24},t.OK=0,t.ERROR=1,t.DATA=6,t.CLOSE=5,t.SUBSCRIBE=2,t.UNSUBSCRIBE=3,t.CTRL=7,t.PING=8,t.MAGIC_END=[68,84],t.MAGIC_START=[78,65],s=class{constructor(e){this.channel=e,this.id=void0,this.channel_id=void0,this.onmessage=void0,this.onerror=void0,this.onopen=void0,this.onclose=void0,this.tunnel=void0,this.is_opened=!1}send(e,n){if(this.tunnel){if(this.is_opened)returnthis.tunnel.send(this.genmsg(e,n));this.onerror&&this.onerror("Channel is not opened yet")}elsethis.onerror&&this.onerror("Tunnel is not opened")}genmsg(e,n){vars;return(s=newt).header.sid=this.id,s.header.cid=this.channel_id,s.header.type=e,s.header.size=n?n.length:0,s.data=n,s}close(e){if(this.is_opened=!1,this.tunnel)returnthis.tunnel.unsubscribe(this,e)}},e=class{constructor(e){this.uri=e,this.socket=void0,this.pending={},this.subscribers={},this.onclose=void0}ready(){returnnewPromise((e,n)=>this.uri?void0!==this.socket?e():(console.log("Connect to "+this.uri),this.socket=newWebSocket(this.uri),this.socket.binaryType="arraybuffer",this.socket.onmessage=e=>this.process(e),this.socket.onclose=e=>{varn,t,s,i;for(ninthis.socket=void0,t=this.pending)(i=t[n]).tunnel=void0,i.onclose&&i.onclose();for(nins=this.subscribers)(i=s[n]).tunnel=void0,i.is_opened=!1,i.onclose&&i.onclose();if(this.pending={},this.subscribe={},this.onclose())returnthis.onclose()},this.socket.onerror=e=>{varn,t,s,i,r;for(nint=this.pending)(r=t[n]).onerror&&r.onerror(e.toString());for(nini=[],s=this.subscribers)(r=s[n]).onerror&&i.push(r.onerror(e.toString()));returni},this.socket.onopen=n=>e()):n())}process(e){returnt.decode(newUint8Array(e.data)).then(e=>{varn,s;switch(n=(e,n)=>{vart;if(!(t=this.pending[e.header.sid]))return(t=this.subscribers[e.header.sid])&&t[n]?t[n](e):void0;t[n]&&t[n](e)},e.header.type){caset.OK:if(!(s=this.pending[e.header.sid]))returnn(e,"onmessage");if(deletethis.pending[e.header.sid],s.id=t.int_from(e.data,0),s.channel_id=e.header.cid,this.subscribers[s.id]=s,s.is_opened=!0,s.onopen)returns.onopen();break;caset.DATA:returnn(e,"onmessage");caset.ERROR:returnn(e,"onerror");caset.UNSUBSCRIBE:if(!(s=this.subscribers[e.header.sid]))return;returns.close(!0);caset.PING:break;default:returnconsole.error(`Message of type ${e.header.type} is unsupported`,e)}}).catch(e=>{varn,t,s,i;for(nint=this.pending)(i=t[n]).onerror&&i.onerror(e);for(nins=this.subscribers)(i=s[n]).onerror&&i.onerror(e);returnconsole.log(e)})}subscribe(e){returnthis.ready().then(()=>{for(e.tunnel=this,e.id=Math.floor(1e3*Math.random())+1;this.subscribers[e.id]||this.pending[e.id];)e.id=Math.floor(1e3*Math.random())+1;returnthis.pending[e.id]=e,this.send(e.genmsg(t.SUBSCRIBE,(newTextEncoder).encode(e.channel)))}).catch((function(n){if(e.onerror)returne.onerror(n.toString())}))}unsubscribe(e,n){returnthis.ready().then(()=>{if(this.subscribers[e.id])returnn||this.send(e.genmsg(t.UNSUBSCRIBE,void0)),e.onclose&&e.onclose(),deletethis.subscribers[e.id],e.tunnel=void0,e.is_opened=!1}).catch((fun