@cf-platform/cim
Version:
消息队列库
2 lines (1 loc) • 4.37 kB
JavaScript
"use strict";var m=Object.defineProperty;var E=(c,t,e)=>t in c?m(c,t,{enumerable:!0,configurable:!0,writable:!0,value:e}):c[t]=e;var a=(c,t,e)=>(E(c,typeof t!="symbol"?t+"":t,e),e);Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});require("./sdk/message.js");require("./sdk/replybody.js");require("./sdk/sentbody.js");const l=require("@mm-custom/method");class _{constructor(t){a(this,"_socket",null);a(this,"_url");a(this,"_restartInterval",3e3);a(this,"_manualClose",!1);a(this,"_ForceOfflineEnum","999");a(this,"_AuthenticationEnum",{PONG_BODY:new Uint8Array([80,79,78,71]),CODE_UNAUTHORIZED:"401",CODE_OK:"200",CLIENT_BIND:"client_bind",CLIENT_CLOSED:"client_closed",CLIENT_HANDSHAKE:"client_handshake"});a(this,"loginConfiguration",{uid:"",channel:"web",appVersion:"",osVersion:"",packageName:"com.farsunset.cim",language:"",deviceId:"",deviceName:""});a(this,"_MessageEnum",{PONG:0,PING:1,MESSAGE:2,SENT_BODY:3,REPLY_BODY:4});a(this,"customEvents");const{name:e,version:s}=l.browserVersion();this.loginConfiguration.osVersion=s,this.loginConfiguration.deviceName=e,this.loginConfiguration.language=window.navigator.language,this.initialize(t)}initialize(t){const{url:e,restartInterval:s,loginParams:n,customEvents:i}=t;this._url=e,!isNaN(Number(s))&&(this._restartInterval=s??0),this.loginConfiguration={...this.loginConfiguration,...n},i&&(this.customEvents=i)}connect(){var t;if(!this._url)throw new Error("连接地址为空!");(t=this.customEvents)!=null&&t.onSocketConnect&&this.customEvents.onSocketConnect(this._url),this._manualClose=!1,this._socket=new WebSocket(this._url),this._socket.binaryType="arraybuffer",this._socket.onopen=()=>{var e;(e=this.customEvents)!=null&&e.onSocketOpen&&this.customEvents.onSocketOpen(),this.sendMessage({key:this._AuthenticationEnum.CLIENT_BIND,...this.loginConfiguration})},this._socket.onmessage=e=>{const s=new Uint8Array(e.data),n=s.length?s[0]:"",i=s.subarray(1,s.length);let o,r;switch(n){case this._MessageEnum.PING:this.sendPong();break;case this._MessageEnum.MESSAGE:o=proto.com.farsunset.cim.sdk.web.model.Message.deserializeBinary(i),this.receiveMessage(o.toObject(!1));break;case this._MessageEnum.REPLY_BODY:o=proto.com.farsunset.cim.sdk.web.model.ReplyBody.deserializeBinary(i),r={code:o.getCode(),key:o.getKey(),message:o.getMessage(),timestamp:o.getTimestamp(),data:{}},o.getDataMap().forEach((u,h)=>{r.data[h]=u}),this.receiveReply(r);break}},this._socket.onclose=()=>{var e;(e=this.customEvents)!=null&&e.onSocketClose&&this.customEvents.onSocketClose(),!this._manualClose&&this._restartInterval&&setTimeout(()=>{this.reconnect()},this._restartInterval)},this._socket.onerror=()=>{var e;(e=this.customEvents)!=null&&e.onSocketError&&this.customEvents.onSocketError()}}reconnect(){this._manualClose=!1,this.connect()}close(){var t;this._manualClose=!0,(t=this._socket)==null||t.close(),this._socket=null}sendPong(){var e,s,n;(e=this.customEvents)!=null&&e.onSendPong&&this.customEvents.onSendPong();const t=new Uint8Array(((s=this._AuthenticationEnum.PONG_BODY)==null?void 0:s.byteLength)+1);t[0]=this._MessageEnum.PONG,t.set(this._AuthenticationEnum.PONG_BODY,1),(n=this._socket)==null||n.send(t)}receiveMessage(t){var e;t.action===this._ForceOfflineEnum&&(this._manualClose=!0),(e=this.customEvents)!=null&&e.onMessage&&this.customEvents.onMessage(t)}receiveReply(t){var e,s,n,i;(e=this.customEvents)!=null&&e.onReply&&this.customEvents.onReply(t),t.key===this._AuthenticationEnum.CLIENT_BIND&&t.code===this._AuthenticationEnum.CODE_OK&&(s=this.customEvents)!=null&&s.onLoginSuccess&&this.customEvents.onLoginSuccess(t),t.key===this._AuthenticationEnum.CLIENT_CLOSED&&t.code===this._AuthenticationEnum.CODE_OK&&(n=this.customEvents)!=null&&n.onLoginDisconnected&&this.customEvents.onLoginDisconnected(t),t.key===this._AuthenticationEnum.CLIENT_HANDSHAKE&&t.code===this._AuthenticationEnum.CODE_UNAUTHORIZED&&(this._manualClose=!0,(i=this.customEvents)!=null&&i.onAuthenticationFailed&&this.customEvents.onAuthenticationFailed(t))}sendMessage(t){var r;const{key:e,...s}=t,n=new proto.com.farsunset.cim.sdk.web.model.SentBody;n.setKey(e),n.setTimestamp(new Date().getTime()),Object.keys(s).forEach(u=>{n.getDataMap().set(u,s[u])});const i=n.serializeBinary(),o=new Uint8Array(i.length+1);o[0]=this._MessageEnum.SENT_BODY,o.set(i,1),(r=this._socket)==null||r.send(o)}}exports.CIM=_;