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连接服务器
初始化完成后,调用连接服务器
```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)
```

View File

@ -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";
@ -20,8 +19,6 @@ 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;
@ -145,11 +142,9 @@ export default class Socket {
throw new UniSocketError("The socket is not initialization or connection error!");
}
}
/**
* 将缓存池的数据发送
*/
async sendBufferRegister() {
const tag = this.globalData.tag
// 绑定账号
bindAccount(account) {
console.log(account)
if (this._connectioned) {
// 缓存池备份
let browser = {
@ -159,7 +154,6 @@ export default class Socket {
};
uni.getSystemInfo({
success: (res) => {
// console.log(res)
APP_VERSION = res.appVersion
browser.version = res.osVersion
browser.name = res.osName
@ -180,8 +174,7 @@ export default class Socket {
});
// '绑定账号' APP_CHANNEL
let account = String(store.getters.user.id)
uni.setStorageSync('account', account)
uni.setStorageSync('account', String(account))
let deviceId = uni.getStorageSync('deviceId')
if (deviceId == "" || deviceId == undefined) {
deviceId = this.generateUUID();
@ -190,7 +183,7 @@ export default class Socket {
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("uid", String(account));
body.getDataMap().set("channel", APP_CHANNEL);
body.getDataMap().set("appVersion", APP_VERSION);
body.getDataMap().set("osVersion", browser.version);
@ -203,31 +196,23 @@ export default class Socket {
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('成功')
},
});
this.sendRequest(body)
}
}
}
async sendBufferTag() {
//绑定tag
async sendBufferTag(tag) {
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");
this.sendRequest(body)
}
else body.setKey("client_remove_tag");
}
// 发送缓存池数据
async sendRequest(body) {
let data = body.serializeBinary();
let protobuf = new Uint8Array(data.length + 1);
protobuf[0] = SENT_BODY;
@ -240,7 +225,6 @@ export default class Socket {
},
});
}
}
generateUUID() {
let d = new Date().getTime();
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) {
this.invokeHandlerFunctionOnRegistr("connectioned");
}
this.sendBufferRegister();
this.sendBufferTag()
}
/**