From a9909a40396e32cf138752316cc33e8e0a88d139 Mon Sep 17 00:00:00 2001 From: zhuhailiang Date: Wed, 1 Nov 2023 10:23:40 +0800 Subject: [PATCH] =?UTF-8?q?sdk=E5=B0=81=E8=A3=85=E6=8E=A5=E5=8F=A3?= =?UTF-8?q?=E8=B0=83=E6=95=B4=E3=80=81=E6=8E=A5=E5=8F=A3=E6=96=87=E6=A1=A3?= =?UTF-8?q?=E8=B0=83=E6=95=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cim-client-sdk/cim-uniapp-sdk/README.md | 53 +- cim-client-sdk/cim-uniapp-sdk/uni.socket.js | 932 ++++++++++---------- 2 files changed, 473 insertions(+), 512 deletions(-) diff --git a/cim-client-sdk/cim-uniapp-sdk/README.md b/cim-client-sdk/cim-uniapp-sdk/README.md index b9f19f1..900ab45 100644 --- a/cim-client-sdk/cim-uniapp-sdk/README.md +++ b/cim-client-sdk/cim-uniapp-sdk/README.md @@ -7,43 +7,47 @@ ### 1.0连接服务器 初始化完成后,调用连接服务器 ```javascript -const uniSocket new UniSocket({ +const uniSocket = new UniSocket({ url: 'websocketUrl' }); ``` ### 1.1绑定账号 -在页面定义 function sendBufferRegister 当socket连接成功回调,然后绑定用户ID ```javascript -uniSocket.sendBufferRegister() +uniSocket.on('connectioned', function() { + uniSocket.bindAccount(id) +}, true) ``` - -### 1.2接收消息 -在页面定义function emitToClientAllEvent 当收到服务端发送的消息时回调 +### 1.2绑定会议房间号 +当socket连接成功回调,然后绑定用户ID +```javascript +uniSocket.sendBufferTag(tag) // 会议结束删除房间号传null或者空 +``` +### 1.3接收消息 ```javascript uniSocket.on('*', async (message) => { }) ``` -### 1.3停止接收消息 +### 1.4停止接收消息 停止接受推送,将会退出当前账号登录,端口与服务端的连接 ```javascript uniSocket.close(); ``` -### 1.4恢复接收消息 +### 1.5恢复接收消息 重新恢复接收推送,重新连接服务端,并登录当前账号 ```javascript uniSocket.reconnection(); ``` -### 1.5发送SentBody请求 +### 1.6发送SentBody请求 支持通过长连接发送一个异步请求到服务的进行处理 例如发送一个位置上报请求 key :client_cycle_location 需要在服务端创建一个实现的handler参照BindHandler -#### 1.5.1 protobuf序列化 +#### 1.6.1 protobuf序列化 ```javascript const SENT_BODY = 3 var body = new proto.com.farsunset.cim.sdk.web.model.SentBody(); @@ -52,21 +56,10 @@ body.getDataMap().set("uid","10000"); body.getDataMap().set("latitude","123.82455"); body.getDataMap().set("longitude","412.245645"); body.getDataMap().set("location","上海市徐汇区云景路8弄"); -let data = body.serializeBinary(); -let protobuf = new Uint8Array(data.length + 1); -protobuf[0] = SENT_BODY; -protobuf.set(data, 1); -const buffer = protobuf -this.uniSocket.send({ -data: buffer, -success: (res) => { - // console.log(res) - console.log('成功') -}, -}); +uniSocket.sendRequest(body) ``` -#### 1.5.2 json序列化 +#### 1.6.2 json序列化 ```javascript let body = {}; body.key ="client_cycle_location"; @@ -76,17 +69,5 @@ body.data.uid = 10000; body.data.latitude = 123.82455; body.data.longitude = 412.245645; body.data.location = "上海市徐汇区云景路8弄"; - -let data = body.serializeBinary(); -let protobuf = new Uint8Array(data.length + 1); -protobuf[0] = SENT_BODY; -protobuf.set(data, 1); -const buffer = protobuf -this.uniSocket.send({ -data: buffer, -success: (res) => { - // console.log(res) - console.log('成功') -}, -}); +uniSocket.sendRequest(body) ``` \ No newline at end of file diff --git a/cim-client-sdk/cim-uniapp-sdk/uni.socket.js b/cim-client-sdk/cim-uniapp-sdk/uni.socket.js index 032838a..908190b 100644 --- a/cim-client-sdk/cim-uniapp-sdk/uni.socket.js +++ b/cim-client-sdk/cim-uniapp-sdk/uni.socket.js @@ -7,7 +7,6 @@ import "./message.js"; import "./replybody.js"; import "./sentbody.js"; -import store from '@/store' let APP_VERSION = "1.0.0"; let APP_CHANNEL = 'app' const APP_PACKAGE = "com.farsunset.cim"; @@ -19,513 +18,494 @@ const PONG = 0; const DATA_HEADER_LENGTH = 1; const PONG_BODY = new Uint8Array([80, 79, 78, 71]); export default class Socket { - constructor(option = {}) { - this.globalData = getApp().globalData - // console.log(this.globalData) - this._url = option.url; - // 是否设置重新连接 - this._reconnection = option.reconnection || true; - // 是否建立缓存池,默认true,如果建立缓存池,会将因为程序错误导致未发送成功的消息发送 - this._buffer = option.buffer || true; - /// on方法注册的事件 - this.on_register = {}; - // 是否已成功连接 - this._connectioned = false; - // 缓存池 - this._buffer_register = []; - // 发送缓存池的数据 - this._auto_emit_buffer_data = option.autoEmitBuffer || false; - // 被动断开 - this.closed = false; - // 开始重连 - this.begin_reconnection = false; - // 多少毫秒发送一次心跳 - this._heart_rate = option.heartRate > 0 ? option.heartRate : 60000; - // 后端心跳字段 - this._heart_rate_type = option.heartRateType || "HEARTBEAT"; - this.init(); - } + constructor(option = {}) { + this._url = option.url; + // 是否设置重新连接 + this._reconnection = option.reconnection || true; + // 是否建立缓存池,默认true,如果建立缓存池,会将因为程序错误导致未发送成功的消息发送 + this._buffer = option.buffer || true; + /// on方法注册的事件 + this.on_register = {}; + // 是否已成功连接 + this._connectioned = false; + // 缓存池 + this._buffer_register = []; + // 发送缓存池的数据 + this._auto_emit_buffer_data = option.autoEmitBuffer || false; + // 被动断开 + this.closed = false; + // 开始重连 + this.begin_reconnection = false; + // 多少毫秒发送一次心跳 + this._heart_rate = option.heartRate > 0 ? option.heartRate : 60000; + // 后端心跳字段 + this._heart_rate_type = option.heartRateType || "HEARTBEAT"; + this.init(); + } - /** - * 注册一个事件 - * @param {Object} event 事件 - * @param {Object} handler 事件处理者 - * @param {Boolean} single 此handler是否只处理一次 - */ - async on(event, handler, single = false) { - const eType = await this.getType(event); - if (eType === "[object String]" && eType.trim() !== "") { - if (this.on_register[event] == void 0) { - this.on_register[event] = []; - } + /** + * 注册一个事件 + * @param {Object} event 事件 + * @param {Object} handler 事件处理者 + * @param {Boolean} single 此handler是否只处理一次 + */ + async on(event, handler, single = false) { + const eType = await this.getType(event); + if (eType === "[object String]" && eType.trim() !== "") { + if (this.on_register[event] == void 0) { + this.on_register[event] = []; + } - if (single) { - console.log('this.on_register[event]: ', this.on_register[event].length); - for (let i = 0; i < this.on_register[event].length; i++) { - console.log(handler === this.on_register[event][i]); - if (handler === this.on_register[event][i]) { - console.log('触发错误'); - throw new UniSocketError(`当前「${event}」事件已被注册...`); - } - } - } + if (single) { + console.log('this.on_register[event]: ', this.on_register[event].length); + for (let i = 0; i < this.on_register[event].length; i++) { + console.log(handler === this.on_register[event][i]); + if (handler === this.on_register[event][i]) { + console.log('触发错误'); + throw new UniSocketError(`当前「${event}」事件已被注册...`); + } + } + } - // 注册事件 - this.on_register[event.trim()].push(handler); - } - } + // 注册事件 + this.on_register[event.trim()].push(handler); + } + } - /** - * 移除指定注册的事件 - * @param {Object} name 事件名称 - */ - async removeEventByName(name) { - return Promise.then(() => { - delete this.on_register[name]; - }); - } + /** + * 移除指定注册的事件 + * @param {Object} name 事件名称 + */ + async removeEventByName(name) { + return Promise.then(() => { + delete this.on_register[name]; + }); + } - /** - * 给缓存池添加记录 - */ - async addBuffer(data = {}) { - const da = JSON.stringify(data); - this._buffer_register.push(data); - } + /** + * 给缓存池添加记录 + */ + async addBuffer(data = {}) { + const da = JSON.stringify(data); + this._buffer_register.push(data); + } - /** - * 获取缓存池 - */ - async getBuffer() { - return this._buffer_register; - } + /** + * 获取缓存池 + */ + async getBuffer() { + return this._buffer_register; + } - /** - * 获取连接状态 - * @return {number} 0 表示连接中,1表示连接成功,2表示重连中,3表示失败 - */ - async getState() { - return this.begin_reconnection ? 2 : this._connectioned ? 1 : this.isError ? 3 : 0; - } + /** + * 获取连接状态 + * @return {number} 0 表示连接中,1表示连接成功,2表示重连中,3表示失败 + */ + async getState() { + return this.begin_reconnection ? 2 : this._connectioned ? 1 : this.isError ? 3 : 0; + } - /** - * 关闭当前socket - */ - async close() { - this.closed = true; - this.SocketTask && this._connectioned && this.SocketTask.close(); - } + /** + * 关闭当前socket + */ + async close() { + this.closed = true; + this.SocketTask && this._connectioned && this.SocketTask.close(); + } - /** - * 发送消息 - */ - async emit(event, data = {}) { - if ( - this.getType(event) === "[object Object]" && - this.getType(event) === "[object String]" - ) { - let e = data; - data = event; - event = e; - } - if (this.SocketTask) { - const da = { - type: event, - data: data, - }; - this.SocketTask.send({ - data: JSON.stringify(da), - fail: (e) => { - // 消息发送失败时将消息缓存 - this.addBuffer(da); - throw new UniSocketError("Failed to send message to server... " + e); - }, - }); - } else { - throw new UniSocketError("The socket is not initialization or connection error!"); - } - } - /** - * 将缓存池的数据发送 - */ - async sendBufferRegister() { - const tag = this.globalData.tag - if (this._connectioned) { - // 缓存池备份 - let browser = { - name: "Other", - version: "1.0.0", - appLanguage: 'zh-CN' - }; - uni.getSystemInfo({ - success: (res) => { - // console.log(res) - APP_VERSION = res.appVersion - browser.version = res.osVersion - browser.name = res.osName - browser.appLanguage = res.appLanguage - if(res.uniPlatform === 'web') { - APP_CHANNEL = 'uni-h5' - browser.version = res.hostVersion - browser.name = res.hostName - } else { - if(res.osName === "android") { - APP_CHANNEL = 'uni-android' - } - if(res.osName === "ios") { - APP_CHANNEL = 'uni-ios' - } - } - } - }); - - // '绑定账号' APP_CHANNEL - let account = String(store.getters.user.id) - uni.setStorageSync('account', account) - let deviceId = uni.getStorageSync('deviceId') - if (deviceId == "" || deviceId == undefined) { - deviceId = this.generateUUID(); - uni.setStorageSync('deviceId', deviceId) - } - let body = new proto.com.farsunset.cim.sdk.web.model.SentBody(); - body.setKey("client_bind"); - body.setTimestamp(new Date().getTime()); - body.getDataMap().set("uid", account); - body.getDataMap().set("channel", APP_CHANNEL); - body.getDataMap().set("appVersion", APP_VERSION); - body.getDataMap().set("osVersion", browser.version); - body.getDataMap().set("packageName", APP_PACKAGE); - body.getDataMap().set("deviceId", deviceId); - body.getDataMap().set("deviceName", browser.name); - body.getDataMap().set("language", browser.appLanguage); - //绑定cid - //#ifdef APP-PLUS - let clientid=uni.getStorageSync("clientId") - body.getDataMap().set("clientId", clientid); - // #endif - let data = body.serializeBinary(); - // console.log(body) - let protobuf = new Uint8Array(data.length + 1); - protobuf[0] = SENT_BODY; - protobuf.set(data, 1); - const buffer = protobuf - this.SocketTask.send({ - data: buffer, - success: (res) => { - // console.log(res) - console.log('成功') - }, - }); + /** + * 发送消息 + */ + async emit(event, data = {}) { + if ( + this.getType(event) === "[object Object]" && + this.getType(event) === "[object String]" + ) { + let e = data; + data = event; + event = e; + } + if (this.SocketTask) { + const da = { + type: event, + data: data, + }; + this.SocketTask.send({ + data: JSON.stringify(da), + fail: (e) => { + // 消息发送失败时将消息缓存 + this.addBuffer(da); + throw new UniSocketError("Failed to send message to server... " + e); + }, + }); + } else { + throw new UniSocketError("The socket is not initialization or connection error!"); + } + } + // 绑定账号 + bindAccount(account) { + console.log(account) + if (this._connectioned) { + // 缓存池备份 + let browser = { + name: "Other", + version: "1.0.0", + appLanguage: 'zh-CN' + }; + uni.getSystemInfo({ + success: (res) => { + APP_VERSION = res.appVersion + browser.version = res.osVersion + browser.name = res.osName + browser.appLanguage = res.appLanguage + if (res.uniPlatform === 'web') { + APP_CHANNEL = 'uni-h5' + browser.version = res.hostVersion + browser.name = res.hostName + } else { + if (res.osName === "android") { + APP_CHANNEL = 'uni-android' + } + if (res.osName === "ios") { + APP_CHANNEL = 'uni-ios' + } + } + } + }); - } - } - async sendBufferTag() { - if (this._connectioned) { - const tag = this.globalData.tag - let body = new proto.com.farsunset.cim.sdk.web.model.SentBody(); - if(tag){ - body.setKey("client_set_tag"); - body.getDataMap().set("tag", tag); - } - else body.setKey("client_remove_tag"); - let data = body.serializeBinary(); - let protobuf = new Uint8Array(data.length + 1); - protobuf[0] = SENT_BODY; - protobuf.set(data, 1); - const buffer = protobuf - this.SocketTask.send({ - data: buffer, - success: (res) => { - console.log('成功') - }, - }); - } - } - generateUUID() { - let d = new Date().getTime(); - let uuid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) { - let r = (d + Math.random() * 16) % 16 | 0; - d = Math.floor(d / 16); - return (c == 'x' ? r : (r & 0x3 | 0x8)).toString(16); - }); - return uuid.replace(/-/g, ''); - } - /** - * 发生错误 - * @param {Object} callback - */ - async error(err) { - this.isError = true; - if (this.on_register["error"] !== undefined) { - this.invokeHandlerFunctionOnRegistr("error", err); - } - } + // '绑定账号' APP_CHANNEL + uni.setStorageSync('account', String(account)) + let deviceId = uni.getStorageSync('deviceId') + if (deviceId == "" || deviceId == undefined) { + deviceId = this.generateUUID(); + uni.setStorageSync('deviceId', deviceId) + } + let body = new proto.com.farsunset.cim.sdk.web.model.SentBody(); + body.setKey("client_bind"); + body.setTimestamp(new Date().getTime()); + body.getDataMap().set("uid", String(account)); + body.getDataMap().set("channel", APP_CHANNEL); + body.getDataMap().set("appVersion", APP_VERSION); + body.getDataMap().set("osVersion", browser.version); + body.getDataMap().set("packageName", APP_PACKAGE); + body.getDataMap().set("deviceId", deviceId); + body.getDataMap().set("deviceName", browser.name); + body.getDataMap().set("language", browser.appLanguage); + //绑定cid + //#ifdef APP-PLUS + let clientid = uni.getStorageSync("clientId") + body.getDataMap().set("clientId", clientid); + // #endif + this.sendRequest(body) + } - /** - * 重新连接错误 - * @param {Object} err 错误信息 - */ - async reconnectionError(err) { - this.isError = true; - if (this.on_register["reconnectionerror"] !== undefined) { - this.invokeHandlerFunctionOnRegistr("reconnectionerror", err); - } - } + } + //绑定tag + async sendBufferTag(tag) { + if (this._connectioned) { + let body = new proto.com.farsunset.cim.sdk.web.model.SentBody(); + if (tag) { + body.setKey("client_set_tag"); + body.getDataMap().set("tag", tag); + } else body.setKey("client_remove_tag"); + this.sendRequest(body) + } + } + // 发送缓存池数据 + async sendRequest(body) { + let data = body.serializeBinary(); + let protobuf = new Uint8Array(data.length + 1); + protobuf[0] = SENT_BODY; + protobuf.set(data, 1); + const buffer = protobuf + this.SocketTask.send({ + data: buffer, + success: (res) => { + console.log('成功') + }, + }); + } + generateUUID() { + let d = new Date().getTime(); + let uuid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) { + let r = (d + Math.random() * 16) % 16 | 0; + d = Math.floor(d / 16); + return (c == 'x' ? r : (r & 0x3 | 0x8)).toString(16); + }); + return uuid.replace(/-/g, ''); + } + /** + * 发生错误 + * @param {Object} callback + */ + async error(err) { + this.isError = true; + if (this.on_register["error"] !== undefined) { + this.invokeHandlerFunctionOnRegistr("error", err); + } + } - /** - * 连接成功 - */ - async connectioned() { - this.isError = false; - // 关闭重连状态 - this.begin_reconnection = false; - this._connectioned = true; - if (this.on_register["connectioned"] !== undefined) { - this.invokeHandlerFunctionOnRegistr("connectioned"); - } - - this.sendBufferRegister(); - - this.sendBufferTag() - } + /** + * 重新连接错误 + * @param {Object} err 错误信息 + */ + async reconnectionError(err) { + this.isError = true; + if (this.on_register["reconnectionerror"] !== undefined) { + this.invokeHandlerFunctionOnRegistr("reconnectionerror", err); + } + } - /** - * 开始发送心跳 - */ - async beginSendHeartBeat() { - this._heart_rate_interval = setInterval((res) => { - this.emit(this._heart_rate_type); - this.emitMessageToTargetEventByName("HEARTBEAT", { - msg: "Send a heartbeat to the server...", - }); - }, this._heart_rate); - } + /** + * 连接成功 + */ + async connectioned() { + this.isError = false; + // 关闭重连状态 + this.begin_reconnection = false; + this._connectioned = true; + if (this.on_register["connectioned"] !== undefined) { + this.invokeHandlerFunctionOnRegistr("connectioned"); + } + } - /** - * 将心跳结束 - */ - async killApp() { - this._heart_rate_interval && clearInterval(this._heart_rate_interval); - } + /** + * 开始发送心跳 + */ + async beginSendHeartBeat() { + this._heart_rate_interval = setInterval((res) => { + this.emit(this._heart_rate_type); + this.emitMessageToTargetEventByName("HEARTBEAT", { + msg: "Send a heartbeat to the server...", + }); + }, this._heart_rate); + } - /** - * 重连socket - */ - async reconnection() { - // 处于与服务器断开状态并且不是被动断开 - this._connectioned = false; - if (!this.closed) { - this.reconnection_time = setTimeout(() => { - this.begin_reconnection = true; - this.connection(); - }, 1000); - } - } + /** + * 将心跳结束 + */ + async killApp() { + this._heart_rate_interval && clearInterval(this._heart_rate_interval); + } - /** - * 初始化程序 - */ - async init() { - console.log('开始链接init'); - this.connection(); - } + /** + * 重连socket + */ + async reconnection() { + // 处于与服务器断开状态并且不是被动断开 + this._connectioned = false; + if (!this.closed) { + this.reconnection_time = setTimeout(() => { + this.begin_reconnection = true; + this.connection(); + }, 1000); + } + } - /** - * 连接socket - */ - async connection() { - // 是否有重连任务 - if (this.reconnection_time) { - console.log('clearTimeout') - clearTimeout(this.reconnection_time); - } - /// 创建一个socket对象,返回socket连接 - const SocketTask = uni.connectSocket({ - url: this._url, - success: () => { - console.log('connectSocket-success') - }, - }); - /// 打开连接的监听 - SocketTask.onOpen(() => { - this.SocketTask = SocketTask; - console.log('打开中') - // 标记已成功连接socket - this._connectioned = true; - SocketTask.onClose(() => { - // 重新连接 - if (!this.closed) { - this.reconnection(); - } - }); - this.connectioned(); - }); + /** + * 初始化程序 + */ + async init() { + console.log('开始链接init'); + this.connection(); + } - SocketTask.onMessage((msg) => { - // console.log(msg) - const message = this.changeMsg(msg) - if (message === false) return - try { - this.emitToClientAllEvent(message); - } catch (e) { - /// 服务器发来的不是一个标准的数据 - this.emitToClientNotNameEvents(message); - } - }); + /** + * 连接socket + */ + async connection() { + // 是否有重连任务 + if (this.reconnection_time) { + console.log('clearTimeout') + clearTimeout(this.reconnection_time); + } + /// 创建一个socket对象,返回socket连接 + const SocketTask = uni.connectSocket({ + url: this._url, + success: () => { + console.log('connectSocket-success') + }, + }); + /// 打开连接的监听 + SocketTask.onOpen(() => { + this.SocketTask = SocketTask; + console.log('打开中') + // 标记已成功连接socket + this._connectioned = true; + SocketTask.onClose(() => { + // 重新连接 + if (!this.closed) { + this.reconnection(); + } + }); + this.connectioned(); + }); - /// 连接打开失败 - SocketTask.onError((res) => { - // 不在重连状态 - if (!this.begin_reconnection) { - this.error(res); - } else { - this.reconnectionError(res); - } - // 重新连接 - this.reconnection(); - }); - } - changeMsg(e) { // 格式化消息 - let data = new Uint8Array(e.data); - let type = data[0]; - let body = data.subarray(DATA_HEADER_LENGTH, data.length); - if (type === PING) { - let pong = new Uint8Array(PONG_BODY.byteLength + 1); - pong[0] = PONG; - pong.set(PONG_BODY, 1); - // console.log('心跳') - this.SocketTask.send({ - data: pong, - fail: (e) => { - throw new UniSocketError("Failed to send message to server... " + e); - }, - }); - return false; - } - if (type == MESSAGE) { - let message = proto.com.farsunset.cim.sdk.web.model.Message.deserializeBinary(body); - // console.log(message) - return message.toObject(false) - } + SocketTask.onMessage((msg) => { + // console.log(msg) + const message = this.changeMsg(msg) + if (message === false) return + try { + this.emitToClientAllEvent(message); + } catch (e) { + /// 服务器发来的不是一个标准的数据 + this.emitToClientNotNameEvents(message); + } + }); - if (type == REPLY_BODY) { - let message = proto.com.farsunset.cim.sdk.web.model.ReplyBody.deserializeBinary(body); - // console.log(message) - /** - * 将proto对象转换成json对象,去除无用信息 - */ - let reply = {}; - reply.code = message.getCode(); - reply.key = message.getKey(); - reply.message = message.getMessage(); - reply.timestamp = message.getTimestamp(); - reply.data = {}; + /// 连接打开失败 + SocketTask.onError((res) => { + // 不在重连状态 + if (!this.begin_reconnection) { + this.error(res); + } else { + this.reconnectionError(res); + } + // 重新连接 + this.reconnection(); + }); + } + changeMsg(e) { // 格式化消息 + let data = new Uint8Array(e.data); + let type = data[0]; + let body = data.subarray(DATA_HEADER_LENGTH, data.length); + if (type === PING) { + let pong = new Uint8Array(PONG_BODY.byteLength + 1); + pong[0] = PONG; + pong.set(PONG_BODY, 1); + // console.log('心跳') + this.SocketTask.send({ + data: pong, + fail: (e) => { + throw new UniSocketError("Failed to send message to server... " + e); + }, + }); + return false; + } + if (type == MESSAGE) { + let message = proto.com.farsunset.cim.sdk.web.model.Message.deserializeBinary(body); + // console.log(message) + return message.toObject(false) + } - /** - * 注意,遍历map这里的参数 value在前key在后 - */ - message.getDataMap().forEach(function(v, k) { - reply.data[k] = v; - }); - return reply - } - } - /** - * 注销监听 - */ - off(event, handler) { - const handlers = JSON.stringify(JSON.parse(this.on_register)); - for (let i = 0; i < handlers.length; i++) { - if (handler === handlers[i]) { - handlers.splice(i, 1); - } - } - return this.off; - } + if (type == REPLY_BODY) { + let message = proto.com.farsunset.cim.sdk.web.model.ReplyBody.deserializeBinary(body); + // console.log(message) + /** + * 将proto对象转换成json对象,去除无用信息 + */ + let reply = {}; + reply.code = message.getCode(); + reply.key = message.getKey(); + reply.message = message.getMessage(); + reply.timestamp = message.getTimestamp(); + reply.data = {}; - // async function handler + /** + * 注意,遍历map这里的参数 value在前key在后 + */ + message.getDataMap().forEach(function(v, k) { + reply.data[k] = v; + }); + return reply + } + } + /** + * 注销监听 + */ + off(event, handler) { + const handlers = JSON.stringify(JSON.parse(this.on_register)); + for (let i = 0; i < handlers.length; i++) { + if (handler === handlers[i]) { + handlers.splice(i, 1); + } + } + return this.off; + } - /** - * 给指定的事件发送消息 - * @param {Object} name 事件名称 - */ - async emitMessageToTargetEventByName(name, data) { - this.invokeHandlerFunctionOnRegistr(name, data); - } + // async function handler - /** - * 联系使用on(**)注册的事件 - */ - async emitToClientNotNameEvents(msg) { - this.invokeHandlerFunctionOnRegistr("**", msg); - } + /** + * 给指定的事件发送消息 + * @param {Object} name 事件名称 + */ + async emitMessageToTargetEventByName(name, data) { + this.invokeHandlerFunctionOnRegistr(name, data); + } - /** - * 联系使用on(*)注册的事件 - */ - async emitToClientAllEvent(data) { - this.invokeHandlerFunctionOnRegistr("*", data); - } + /** + * 联系使用on(**)注册的事件 + */ + async emitToClientNotNameEvents(msg) { + this.invokeHandlerFunctionOnRegistr("**", msg); + } - /** - * 获取对象类型 - * @param {Object} o 需要验证的对象 - */ - async getType(o) { - return Object.prototype.toString.call(o); - } + /** + * 联系使用on(*)注册的事件 + */ + async emitToClientAllEvent(data) { + this.invokeHandlerFunctionOnRegistr("*", data); + } - /** - * 给指定的事件发送数据 - * @param {Object} register 事件 - * @param {Object} data 需要发送的数据 - */ - async invokeHandlerFunctionOnRegistr(register, data) { - // console.log(data) - if (this.on_register[register] !== undefined) { - const eventList = this.on_register[register]; - for (var i = 0; i < eventList.length; i++) { - const event = eventList[i]; - event(data); - } - } - } + /** + * 获取对象类型 + * @param {Object} o 需要验证的对象 + */ + async getType(o) { + return Object.prototype.toString.call(o); + } + + /** + * 给指定的事件发送数据 + * @param {Object} register 事件 + * @param {Object} data 需要发送的数据 + */ + async invokeHandlerFunctionOnRegistr(register, data) { + // console.log(data) + if (this.on_register[register] !== undefined) { + const eventList = this.on_register[register]; + for (var i = 0; i < eventList.length; i++) { + const event = eventList[i]; + event(data); + } + } + } } // 自定义Error var __extends = (this && this.__extends) || (function() { - var extendStatics = function(d, b) { - extendStatics = Object.setPrototypeOf || - ({ - __proto__: [] - } - instanceof Array && function(d, b) { - d.__proto__ = b; - }) || - function(d, b) { - for (var p in b) - if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; - }; - return extendStatics(d, b); - }; - return function(d, b) { - if (typeof b !== "function" && b !== null) - throw new TypeError("Class extends value " + String(b) + " is not a constructor or null"); - extendStatics(d, b); + var extendStatics = function(d, b) { + extendStatics = Object.setPrototypeOf || + ({ + __proto__: [] + } + instanceof Array && function(d, b) { + d.__proto__ = b; + }) || + function(d, b) { + for (var p in b) + if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; + }; + return extendStatics(d, b); + }; + return function(d, b) { + if (typeof b !== "function" && b !== null) + throw new TypeError("Class extends value " + String(b) + " is not a constructor or null"); + extendStatics(d, b); - function __() { - this.constructor = d; - } - d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); - }; + function __() { + this.constructor = d; + } + d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); + }; })(); var UniSocketError = /** @class */ (function(_super) { - __extends(UniSocketError, _super); + __extends(UniSocketError, _super); - function UniSocketError(message) { - var _this = _super.call(this, message) || this; - _this.name = 'UniSocketError'; - return _this; - } - return UniSocketError; -}(Error)); + function UniSocketError(message) { + var _this = _super.call(this, message) || this; + _this.name = 'UniSocketError'; + return _this; + } + return UniSocketError; +}(Error)); \ No newline at end of file