@jecloud/utils
Version:
JECloud 工具库
1 lines • 4.65 kB
JavaScript
import{SendCommandEnum as e,ReceiveMessageEnum as t}from"./enum";import n from"./model/handshake-message";import s from"./model/send-message";import o from"./model/receive-message";import{dateFormat as c}from"../date";var r=10,i=function(){function c(e){this.destroyed=!1,this.debugger=!1,this._unreadMessage=[],this._failedMessage=[],this.url=e.url,this._onWebsocketLog=e.onLog,this._onMessageEvent=e.onMessage,this._onMessage4UnreadEvent=e.onUnreadMessage}return c.prototype.enableDebugger=function(e){void 0===e&&(e=!0),this.debugger=!!e},c.prototype.isConnected=function(){var e;return 1==(null===(e=this.getSocket())||void 0===e?void 0:e.readyState)},c.prototype.getSocket=function(){return this.instance},c.prototype.connect=function(e){var n=this;if(this.isConnected()){if(!e||this.url===e)return void a(this,{type:"unconnect"});this.close()}else if(!window.navigator.onLine)return void a(this,{type:"unnetwork"});this.destroyed=!1,this.url=e||this.url,this.instance=new window.WebSocket(this.url),this.instance.onmessage=function(e){var s=e.data;n.heartBeat(),a(n,{type:"message",message:s});var c=new o(s);c.type===t.HANDSHAKE&&(n._failedMessage.forEach((function(e){var t=e.cmd,s=e.body;n.send(t,s)})),n._failedMessage=[]),n.onReceive(c)},this.instance.onopen=function(){n.isConnected()&&(a(n,{type:"open"}),n.handshake())},this.instance.onclose=function(e){n.instance&&(a(n,{type:"close",message:e}),n.destroyed||n._reconnecting||n.reconnect())},this.instance.onerror=function(e){a(n,{type:"error",message:e})},a(this,{type:"connect",message:this.url})},c.prototype.reconnect=function(e){var t=this;e&&(this._reconnecting=!1,this._reconnectCheckTimes=0),clearTimeout(this._reconnectFn);var n=this._reconnectCheckTimes==r;this.isConnected()||n?n?this.close():a(this,{type:"reconnect"}):(this._reconnecting=!0,this._reconnectCheckTimes=this._reconnectCheckTimes||0,this._reconnectCheckTimes++,a(this,{type:"reconnectNumber",message:this._reconnectCheckTimes}),this.connect(),this._reconnectFn=setTimeout((function(){t.reconnect()}),1e3*this._reconnectCheckTimes))},c.prototype.close=function(){this.destroyed=!0,this.isConnected()&&(clearTimeout(this._reconnectFn),clearInterval(this._heartBeatFn),this.instance.close()),this.instance=null,a(this,{type:"destroyed"})},c.prototype.handshake=function(){this.send(e.HANDSHAKE,new n)},c.prototype.heartBeat=function(){var t=this;if(clearInterval(this._heartBeatFn),this.isConnected()){this._heartBeatFn=setInterval((function(){t.send(e.HEARTBEAT)}),15e3)}},c.prototype.send=function(t,n){void 0===t&&(t=e.CHAT);var o=new s({cmd:t,body:n});Object.values(e).includes(t)?this.isConnected()?this.instance.send(JSON.stringify(o)):this.instance&&-1==[this.instance.CLOSED,this.instance.CLOSING].indexOf(this.instance.readyState)?this.instance.readyState==this.instance.CONNECTING&&(a(this,{type:"reconnecting"}),this._failedMessage.push(o)):(a(this,{type:"beginReconnect"}),this._failedMessage.push(o),this.reconnect(!0)):console.error("非法的消息类型,请检查",t,n)},c.prototype.onReceive=function(e){var n,s,c,r=e.type,i=e.content;switch(r){case t.HANDSHAKE:!i.success&&a(this,{type:"handshake",message:i.msg});break;case t.NOREAD_MESSAGE:var h=null===(n=i.map)||void 0===n?void 0:n.call(i,(function(e){return new o(e)}));try{null===(s=this._onMessage4UnreadEvent)||void 0===s||s.call(this,h)}catch(e){console.error(e)}break;default:try{null===(c=this._onMessageEvent)||void 0===c||c.call(this,e)}catch(e){console.error(e)}}},c}();export default i;function a(e,t){var n,s=t.type,o={type:"info",message:t.message,log:""};switch(s){case"open":o.type="success",o.log="Web Socket opened!";break;case"close":o.type="error",o.log="Web Socket closed!";break;case"error":o.type="error",o.log="Web Socket error!";break;case"connect":o.log="Web Socket try connect server, url=";break;case"reconnect":o.type="success",o.log="Web Socket reconnect success !";break;case"reconnectNumber":o.log="Web Socket reconnect count :";break;case"beginReconnect":o.log="Web Socket begin reconnect ...";break;case"reconnecting":o.log="Web Socket reconnecting ...";break;case"message":o.log="Web Socket receive message:";break;case"handshake":o.type="error",o.log="Web Socket handshake error!";break;case"destroyed":o.type="error",o.log="Web Socket destroyed!";break;case"unconnect":o.type="error",o.log="Web Socket is connected!";break;case"unnetwork":o.type="error",o.log="The network is disconnected!"}o.log="【"+c(new Date)+"】"+o.log,null===(n=e._onWebsocketLog)||void 0===n||n.call(e,o),e.debugger&&console.log("%c "+o.log,{info:"color:blue",success:"color:green",error:"color:red"}[o.type],o.message)}