sdk封装接口调整、接口文档调整

This commit is contained in:
zhuhailiang 2023-11-01 10:23:40 +08:00
parent ce2377c72b
commit a9909a4039
2 changed files with 473 additions and 512 deletions

View File

@ -7,43 +7,47 @@
### 1.0连接服务器 ### 1.0连接服务器
初始化完成后,调用连接服务器 初始化完成后,调用连接服务器
```javascript ```javascript
const uniSocket new UniSocket({ const uniSocket = new UniSocket({
url: 'websocketUrl' url: 'websocketUrl'
}); });
``` ```
### 1.1绑定账号 ### 1.1绑定账号
在页面定义 function sendBufferRegister
当socket连接成功回调然后绑定用户ID 当socket连接成功回调然后绑定用户ID
```javascript ```javascript
uniSocket.sendBufferRegister() uniSocket.on('connectioned', function() {
uniSocket.bindAccount(id)
}, true)
``` ```
### 1.2绑定会议房间号
### 1.2接收消息 当socket连接成功回调然后绑定用户ID
在页面定义function emitToClientAllEvent 当收到服务端发送的消息时回调 ```javascript
uniSocket.sendBufferTag(tag) // 会议结束删除房间号传null或者空
```
### 1.3接收消息
```javascript ```javascript
uniSocket.on('*', async (message) => { uniSocket.on('*', async (message) => {
}) })
``` ```
### 1.3停止接收消息 ### 1.4停止接收消息
停止接受推送,将会退出当前账号登录,端口与服务端的连接 停止接受推送,将会退出当前账号登录,端口与服务端的连接
```javascript ```javascript
uniSocket.close(); uniSocket.close();
``` ```
### 1.4恢复接收消息 ### 1.5恢复接收消息
重新恢复接收推送,重新连接服务端,并登录当前账号 重新恢复接收推送,重新连接服务端,并登录当前账号
```javascript ```javascript
uniSocket.reconnection(); uniSocket.reconnection();
``` ```
### 1.5发送SentBody请求 ### 1.6发送SentBody请求
支持通过长连接发送一个异步请求到服务的进行处理 支持通过长连接发送一个异步请求到服务的进行处理
例如发送一个位置上报请求 例如发送一个位置上报请求
key client_cycle_location 需要在服务端创建一个实现的handler参照BindHandler key client_cycle_location 需要在服务端创建一个实现的handler参照BindHandler
#### 1.5.1 protobuf序列化 #### 1.6.1 protobuf序列化
```javascript ```javascript
const SENT_BODY = 3 const SENT_BODY = 3
var body = new proto.com.farsunset.cim.sdk.web.model.SentBody(); 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("latitude","123.82455");
body.getDataMap().set("longitude","412.245645"); body.getDataMap().set("longitude","412.245645");
body.getDataMap().set("location","上海市徐汇区云景路8弄"); body.getDataMap().set("location","上海市徐汇区云景路8弄");
let data = body.serializeBinary(); uniSocket.sendRequest(body)
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('成功')
},
});
``` ```
#### 1.5.2 json序列化 #### 1.6.2 json序列化
```javascript ```javascript
let body = {}; let body = {};
body.key ="client_cycle_location"; body.key ="client_cycle_location";
@ -76,17 +69,5 @@ body.data.uid = 10000;
body.data.latitude = 123.82455; body.data.latitude = 123.82455;
body.data.longitude = 412.245645; body.data.longitude = 412.245645;
body.data.location = "上海市徐汇区云景路8弄"; body.data.location = "上海市徐汇区云景路8弄";
uniSocket.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.uniSocket.send({
data: buffer,
success: (res) => {
// console.log(res)
console.log('成功')
},
});
``` ```

View File

@ -7,7 +7,6 @@
import "./message.js"; import "./message.js";
import "./replybody.js"; import "./replybody.js";
import "./sentbody.js"; import "./sentbody.js";
import store from '@/store'
let APP_VERSION = "1.0.0"; let APP_VERSION = "1.0.0";
let APP_CHANNEL = 'app' let APP_CHANNEL = 'app'
const APP_PACKAGE = "com.farsunset.cim"; const APP_PACKAGE = "com.farsunset.cim";
@ -20,8 +19,6 @@ const DATA_HEADER_LENGTH = 1;
const PONG_BODY = new Uint8Array([80, 79, 78, 71]); const PONG_BODY = new Uint8Array([80, 79, 78, 71]);
export default class Socket { export default class Socket {
constructor(option = {}) { constructor(option = {}) {
this.globalData = getApp().globalData
// console.log(this.globalData)
this._url = option.url; this._url = option.url;
// 是否设置重新连接 // 是否设置重新连接
this._reconnection = option.reconnection || true; this._reconnection = option.reconnection || true;
@ -145,11 +142,9 @@ export default class Socket {
throw new UniSocketError("The socket is not initialization or connection error!"); throw new UniSocketError("The socket is not initialization or connection error!");
} }
} }
/** // 绑定账号
* 将缓存池的数据发送 bindAccount(account) {
*/ console.log(account)
async sendBufferRegister() {
const tag = this.globalData.tag
if (this._connectioned) { if (this._connectioned) {
// 缓存池备份 // 缓存池备份
let browser = { let browser = {
@ -159,20 +154,19 @@ export default class Socket {
}; };
uni.getSystemInfo({ uni.getSystemInfo({
success: (res) => { success: (res) => {
// console.log(res)
APP_VERSION = res.appVersion APP_VERSION = res.appVersion
browser.version = res.osVersion browser.version = res.osVersion
browser.name = res.osName browser.name = res.osName
browser.appLanguage = res.appLanguage browser.appLanguage = res.appLanguage
if(res.uniPlatform === 'web') { if (res.uniPlatform === 'web') {
APP_CHANNEL = 'uni-h5' APP_CHANNEL = 'uni-h5'
browser.version = res.hostVersion browser.version = res.hostVersion
browser.name = res.hostName browser.name = res.hostName
} else { } else {
if(res.osName === "android") { if (res.osName === "android") {
APP_CHANNEL = 'uni-android' APP_CHANNEL = 'uni-android'
} }
if(res.osName === "ios") { if (res.osName === "ios") {
APP_CHANNEL = 'uni-ios' APP_CHANNEL = 'uni-ios'
} }
} }
@ -180,8 +174,7 @@ export default class Socket {
}); });
// '绑定账号' APP_CHANNEL // '绑定账号' APP_CHANNEL
let account = String(store.getters.user.id) uni.setStorageSync('account', String(account))
uni.setStorageSync('account', account)
let deviceId = uni.getStorageSync('deviceId') let deviceId = uni.getStorageSync('deviceId')
if (deviceId == "" || deviceId == undefined) { if (deviceId == "" || deviceId == undefined) {
deviceId = this.generateUUID(); deviceId = this.generateUUID();
@ -190,7 +183,7 @@ export default class Socket {
let body = new proto.com.farsunset.cim.sdk.web.model.SentBody(); let body = new proto.com.farsunset.cim.sdk.web.model.SentBody();
body.setKey("client_bind"); body.setKey("client_bind");
body.setTimestamp(new Date().getTime()); body.setTimestamp(new Date().getTime());
body.getDataMap().set("uid", account); body.getDataMap().set("uid", String(account));
body.getDataMap().set("channel", APP_CHANNEL); body.getDataMap().set("channel", APP_CHANNEL);
body.getDataMap().set("appVersion", APP_VERSION); body.getDataMap().set("appVersion", APP_VERSION);
body.getDataMap().set("osVersion", browser.version); body.getDataMap().set("osVersion", browser.version);
@ -200,34 +193,26 @@ export default class Socket {
body.getDataMap().set("language", browser.appLanguage); body.getDataMap().set("language", browser.appLanguage);
//绑定cid //绑定cid
//#ifdef APP-PLUS //#ifdef APP-PLUS
let clientid=uni.getStorageSync("clientId") let clientid = uni.getStorageSync("clientId")
body.getDataMap().set("clientId", clientid); body.getDataMap().set("clientId", clientid);
// #endif // #endif
let data = body.serializeBinary(); this.sendRequest(body)
// 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('成功')
},
});
} }
} //绑定tag
async sendBufferTag() { async sendBufferTag(tag) {
if (this._connectioned) { if (this._connectioned) {
const tag = this.globalData.tag
let body = new proto.com.farsunset.cim.sdk.web.model.SentBody(); let body = new proto.com.farsunset.cim.sdk.web.model.SentBody();
if(tag){ if (tag) {
body.setKey("client_set_tag"); body.setKey("client_set_tag");
body.getDataMap().set("tag", tag); body.getDataMap().set("tag", tag);
} else body.setKey("client_remove_tag");
this.sendRequest(body)
} }
else body.setKey("client_remove_tag"); }
// 发送缓存池数据
async sendRequest(body) {
let data = body.serializeBinary(); let data = body.serializeBinary();
let protobuf = new Uint8Array(data.length + 1); let protobuf = new Uint8Array(data.length + 1);
protobuf[0] = SENT_BODY; protobuf[0] = SENT_BODY;
@ -240,7 +225,6 @@ export default class Socket {
}, },
}); });
} }
}
generateUUID() { generateUUID() {
let d = new Date().getTime(); let d = new Date().getTime();
let uuid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) { let uuid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
@ -283,10 +267,6 @@ export default class Socket {
if (this.on_register["connectioned"] !== undefined) { if (this.on_register["connectioned"] !== undefined) {
this.invokeHandlerFunctionOnRegistr("connectioned"); this.invokeHandlerFunctionOnRegistr("connectioned");
} }
this.sendBufferRegister();
this.sendBufferTag()
} }
/** /**