From c9177612ea0f76fcc6cca0a49332229d3e6f9589 Mon Sep 17 00:00:00 2001 From: zhuhailiang Date: Wed, 25 Oct 2023 11:32:02 +0800 Subject: [PATCH] =?UTF-8?q?README.md=E8=AF=B4=E6=98=8E=E6=96=87=E6=A1=A3?= =?UTF-8?q?=E6=8F=90=E4=BA=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cim-client-sdk/cim-uniapp-sdk/README.md | 105 ++++++++++++++++++++ cim-client-sdk/cim-uniapp-sdk/uni.socket.js | 17 ---- 2 files changed, 105 insertions(+), 17 deletions(-) create mode 100644 cim-client-sdk/cim-uniapp-sdk/README.md diff --git a/cim-client-sdk/cim-uniapp-sdk/README.md b/cim-client-sdk/cim-uniapp-sdk/README.md new file mode 100644 index 0000000..aaa34cf --- /dev/null +++ b/cim-client-sdk/cim-uniapp-sdk/README.md @@ -0,0 +1,105 @@ +**文档最新更新时间:****2023年10月25日** + +## 前言 + +CIM是一套完善的消息推送框架,可应用于信令推送,即时聊天,移动设备指令推送等领域。开发者可沉浸于业务开发,不用关心消息通道长连接、消息编解码协议等繁杂处理。 + +CIM采用业内主流开源技术构建,易于扩展和使用,并完美支持集群部署支持海量链接,目前支持websocket,android,ios,桌面应用,系统应用等多端接入持,可应用于移动应用,物联网,智能家居,嵌入式开发,桌面应用,WEB应用即时消服务。 + +和信是基于CIM组件开发的一整套完整的产品,面向所有人开放注册的试用场景。具有丰富的功能,聊天、群组、好友列表、黑名单、公众号、朋友圈等功能。不依赖任何第三方服务,可以私有化部署。 + +该软件为uniapp版本,支持多端适配,如有需要请联系作者购买。 + + +[CIM协议开源地址:](https://gitee.com/farsunset/cim) + +## 2、常用功能接口 +所有开放外部接口都集中在uni.socket.js +服务端配置地址配置以及客户端相关配置都在这里面 + +### 2.0连接服务器 +初始化完成后,调用连接服务器 +```javascript +const uniSocket new UniSocket({ + url: 'websocketUrl' +}); +``` +### 2.1绑定账号 +在页面定义 function sendBufferRegister +当socket连接成功回调,然后绑定用户ID +```javascript +uniSocket.sendBufferRegister() +``` + +### 1.2接收消息 +在页面定义function emitToClientAllEvent 当收到服务端发送的消息时回调 +```javascript +uniSocket.on('*', async (message) => { + +}) +``` + +### 2.3停止接收消息 +停止接受推送,将会退出当前账号登录,端口与服务端的连接 +```javascript +uniSocket.close(); +``` + +### 2.4恢复接收消息 +重新恢复接收推送,重新连接服务端,并登录当前账号 +```javascript +uniSocket.reconnection(); +``` + +### 2.5发送SentBody请求 +支持通过长连接发送一个异步请求到服务的进行处理 +例如发送一个位置上报请求 +key :client_cycle_location 需要在服务端创建一个实现的handler参照BindHandler + +#### 2.5.1 protobuf序列化 +```javascript +const SENT_BODY = 3 +var body = new proto.com.farsunset.cim.sdk.web.model.SentBody(); +body.setKey("client_cycle_location"); +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('成功') +}, +}); +``` + +#### 2.5.1 json序列化 +```javascript +let body = {}; +body.key ="client_cycle_location"; +body.timestamp=new Date().getTime(); +body.data = {}; +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('成功') +}, +}); +``` \ 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 be4d666..032838a 100644 --- a/cim-client-sdk/cim-uniapp-sdk/uni.socket.js +++ b/cim-client-sdk/cim-uniapp-sdk/uni.socket.js @@ -72,12 +72,6 @@ export default class Socket { // 注册事件 this.on_register[event.trim()].push(handler); - // if (!single || (isSingle && single)) { - // if (this.on_register[event] === undefined) { - // this.on_register[event] = []; - // } - // this.on_register[event.trim()].push(handler); - // } } } @@ -207,7 +201,6 @@ export default class Socket { //绑定cid //#ifdef APP-PLUS let clientid=uni.getStorageSync("clientId") - console.log("开始绑定设备cid",clientid) body.getDataMap().set("clientId", clientid); // #endif let data = body.serializeBinary(); @@ -243,11 +236,9 @@ export default class Socket { this.SocketTask.send({ data: buffer, success: (res) => { - // console.log(res) console.log('成功') }, }); - // console.log(body) } } generateUUID() { @@ -292,8 +283,6 @@ export default class Socket { if (this.on_register["connectioned"] !== undefined) { this.invokeHandlerFunctionOnRegistr("connectioned"); } - // 给服务器发送心跳 - // this.beginSendHeartBeat(); this.sendBufferRegister(); @@ -339,12 +328,6 @@ export default class Socket { async init() { console.log('开始链接init'); this.connection(); - // 发送缓存池中的数据 - // if (this._auto_emit_buffer_data) { - // setInterval(() => { - // this._connectioned && this.sendBufferRegister(); - // }, 20000); - // } } /**