(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=11+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),n.set(t,7),this.data&&n.set(this.data,9),n.set(e.MAGIC_END,this.header.size+9),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),i.data=e.slice(9,9+i.header.size),t.int_from(t.MAGIC_END,0)!==t.int_from(e,9+i.header.size)?s("Unmatch message end magic number"):n(i))}))},t.bytes_of=function(e){varn;return(n=newUint8Array(2))[0]=255&e,e>>=8,n[1]=255&e,n},t.int_from=function(e,n){returne[n]|e[n+1]<<8},t.OK=0,t.ERROR=1,t.DATA=6,t.CLOSE=5,t.SUBSCRIBE=2,t.UNSUBSCRIBE=3,t.CTRL=7,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);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((function(n){if(e.onerror)returne.onerror(n.toString())}))}send(e){returnthis.socket.send(e.as_raw())}close(){if(console.log("Closeconnection