-
Notifications
You must be signed in to change notification settings - Fork 1
/
client.min.js
1 lines (1 loc) · 6.07 KB
/
client.min.js
1
"use strict";!function(){const t=document.body||document.querySelector("body")[0];let e;const s=document.querySelector("script[nonce]");s&&(e=s.getAttribute("nonce")),function(s){const n=document.createElement("script");n.src=s,e&&n.setAttribute("nonce",e),t.appendChild(n)}("https://cdn.jsdelivr.net/gh/nodeca/[email protected]/dist/pako.min.js")}();class ServerIO{constructor(t=null){let e;e="string"==typeof t&&t.match(/^(http|ws)s?:\/\//)?t.replace(/^http/,"ws"):window.location.origin.replace(/^http/,"ws"),this.origin=e;let s=new WebSocket(e+"/ws");this.socket=s,this._sendingData=0,this._data=void 0,this.autoReconnect=!0,this._listeners={},this._currentListeners=[],this.lastConnect=0,this.lastDisconnect=0,this.connected=!1,this._disconnectCode=void 0;const n=t=>{t.target.url&&t.target.url.startsWith(e)&&(this.lastConnect=Date.now(),this.connected=!0)},i=t=>{if(!t.target.url||!t.target.url.startsWith(e))return;this.connected=!1;let s=Date.now();if(s-this.lastDisconnect<100)return;this.lastDisconnect=s,this._oldData=this._data,this._data=void 0;let n=t.code||this._disconnectCode||1e3;if(this._disconnectCode&&1e3===n&&(n=this._disconnectCode),this._disconnectCode=void 0,this._listeners["@disconnect"])for(let t=0;t<this._listeners["@disconnect"].length;t++)this._listeners["@disconnect"][t].call(this,n);this.autoReconnect&&[1006,1009,1011,1012,1013,1014,1015].includes(n)&&(console.log("reconnecting"),this.connect("@retry"))},o=t=>{if(t.origin!==e)return;let s=t.data;if(this._data&&1===this._data.compress)try{let t=pako.ungzip(atob(s),{to:"string"});t&&""!==t&&(s=t)}catch(t){}try{s=JSON.parse(s)}catch(t){return}if("@connection"===s.name)"connect"!==s.data||this._data||setTimeout((()=>{let t=0;s.canCompress&&void 0!==window.pako&&(t=1),this._data={clientID:s.clientID,token:s.token,serverKey:s.serverKey,encKey:s.encKey,compress:t},setTimeout((()=>{this._socketSend(JSON.stringify({name:"@connection",data:"connect",token:s.token,compress:t}))}),100)}),500);else{if(s.token!==this._data.serverKey)return;if("@error"===s.name){if(this._listeners["@error"])for(let t=0;t<this._listeners["@error"].length;t++)this._listeners["@error"][t].call(this,s.data);if("migrate"===s.data)for(let t=0;t<this._currentListeners.length;t++)this._currentListeners[t].startsWith("@")||this.on(this._currentListeners[t]);return}if(this._listeners[s.name])for(let t=0;t<this._listeners[s.name].length;t++)this._listeners[s.name][t].call(this,s.data)}};this._socketFuncs={onConnect:n,onDisconnect:i,onMessage:o},Object.freeze(this._socketFuncs),s.addEventListener("open",n,{passive:!0}),s.addEventListener("close",i,{passive:!0}),s.addEventListener("error",i,{passive:!0}),s.addEventListener("message",o,{passive:!0})}async _socketSend(t){if("string"==typeof t){for(;this._sendingData>0;)await new Promise((t=>setTimeout(t,100)));if(this._sendingData++,await new Promise((t=>setTimeout(t,10))),this._sendingData>1)return this._sendingData--,void this._socketSend(json);this.socket.send(t),await new Promise((t=>setTimeout(t,100))),this._sendingData--}}async connect(t){return setTimeout((async()=>{if("function"==typeof t){for(;!this._data;)await new Promise((t=>setTimeout(t,100)));return this._listeners["@connect"]||(this._listeners["@connect"]=[]),this._listeners["@connect"].push(t),void t.call(this,!0)}if(this._data)return;let e=Date.now();if(e-this.lastConnect<1e4)setTimeout((()=>{this.connect(t)}),e-this.lastConnect);else{for(this.lastConnect=e,this.socket=new WebSocket(this.origin+"/ws"),this.socket.addEventListener("open",this._socketFuncs.onConnect,{passive:!0}),this.socket.addEventListener("close",this._socketFuncs.onDisconnect,{passive:!0}),this.socket.addEventListener("error",this._socketFuncs.onDisconnect,{passive:!0}),this.socket.addEventListener("message",this._socketFuncs.onMessage,{passive:!0});!this._data;)await new Promise((t=>setTimeout(t,100)));if("@retry"===t&&this._oldData&&(this._socketSend(JSON.stringify({name:"@connection",data:"migrate",token:this._data.token,oldClient:this._oldData.clientID,oldToken:this._oldData.token,oldServerKey:this._oldData.serverKey,oldEncKey:this._oldData.encKey})),this._oldData=void 0),this._listeners["@connect"])for(let t=0;t<this._listeners["@connect"].length;t++)this._listeners["@connect"][t].call(this,!1)}}),100),this}async disconnect(t){if("function"==typeof t)return this._listeners["@disconnect"]||(this._listeners["@disconnect"]=[]),this._listeners["@disconnect"].push(t),this;for(;!this._data;)await new Promise((t=>setTimeout(t,100)));let e=1e3;return"number"==typeof t&&(e=Number(t)),e<1e3&&(e+=1e3),this._disconnectCode=e,this._socketSend(JSON.stringify({name:"@connection",data:"disconnect",token:this._data.token,code:e})),setTimeout((()=>{this.connected&&this.socket.close(e)}),1e3),this}async on(t,e){if("string"!=typeof t||"string"!=typeof t.toString())return this;if(""===(t=t.toString().replace(/[^\w_-]+/g,"")))return this;for(this._listeners[t]||(this._listeners[t]=[]),this._currentListeners.includes(t)||this._currentListeners.push(t),"function"==typeof e&&this._listeners[t].push(e);!this._data;)await new Promise((t=>setTimeout(t,100)));return this._socketSend(JSON.stringify({name:"@listener",data:t,token:this._data.token})),this}async off(t,e=!0){if("string"!=typeof t||"string"!=typeof t.toString())return this;if(""===(t=t.toString().replace(/[^\w_-]+/g,"")))return this;this._listeners[t]&&e&&delete this._listeners[t];let s=this._currentListeners.indexOf(t);for(-1!==s&&this._currentListeners.splice(s,1);!this._data;)await new Promise((t=>setTimeout(t,100)));return this._socketSend(JSON.stringify({name:"@listener",data:"!"+t,token:this._data.token})),this}async send(t,e){if("string"!=typeof t||"string"!=typeof t.toString()||"function"==typeof e)return this;if(""===(t=t.toString().replace(/[^\w_-]+/g,"")))return this;for(;!this._data;)await new Promise((t=>setTimeout(t,100)));let s=JSON.stringify({name:t,data:e,token:this._data.token});if(1===this._data.compress)try{let t=btoa(pako.gzip(s,{to:"string"}));t&&""!==t&&(s=t)}catch(t){}return this._socketSend(s),this}async error(t){return"function"==typeof t&&(this._listeners["@error"]||(this._listeners["@error"]=[]),this._listeners["@error"].push(t)),this}}