UNPKG

@cf-platform/cim

Version:

消息队列库

166 lines (165 loc) 6.25 kB
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); import "./sdk/message.js"; import "./sdk/replybody.js"; import "./sdk/sentbody.js"; import { browserVersion as l } from "@mm-custom/method"; class C { /** * @param options CIM参数 */ constructor(t) { /** WebSocket 实例 */ 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(); this.loginConfiguration.osVersion = s, this.loginConfiguration.deviceName = e, this.loginConfiguration.language = window.navigator.language, this.initialize(t); } /** * 初始化 * @param options CIM初始化参数 */ 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, h; 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), h = { code: o.getCode(), key: o.getKey(), message: o.getMessage(), timestamp: o.getTimestamp(), data: {} }, o.getDataMap().forEach((r, u) => { h.data[u] = r; }), this.receiveReply(h); 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); } /** * 接收消息 * @param message 收到的消息 */ receiveMessage(t) { var e; t.action === this._ForceOfflineEnum && (this._manualClose = !0), (e = this.customEvents) != null && e.onMessage && this.customEvents.onMessage(t); } /** * 接收回复 * @param reply */ 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)); } /** * 发送消息 * @param data.key 消息Key */ sendMessage(t) { var h; const { key: e, ...s } = t, n = new proto.com.farsunset.cim.sdk.web.model.SentBody(); n.setKey(e), n.setTimestamp((/* @__PURE__ */ new Date()).getTime()), Object.keys(s).forEach((r) => { n.getDataMap().set(r, s[r]); }); const i = n.serializeBinary(), o = new Uint8Array(i.length + 1); o[0] = this._MessageEnum.SENT_BODY, o.set(i, 1), (h = this._socket) == null || h.send(o); } } export { C as CIM };