mirror of
https://gitee.com/farsunset/cim.git
synced 2025-06-30 14:14:50 +08:00
修复uni微信小程序模式下,Uint8Array转换的问题
参见https://blog.csdn.net/cnzzs/article/details/143497925
This commit is contained in:
parent
c4a90251db
commit
b02d43af32
@ -9,37 +9,27 @@ import "./replybody.js";
|
|||||||
import "./sentbody.js";
|
import "./sentbody.js";
|
||||||
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 MESSAGE = 2;
|
const MESSAGE = 2;
|
||||||
const REPLY_BODY = 4;
|
const REPLY_BODY = 4;
|
||||||
const SENT_BODY = 3;
|
const SENT_BODY = 3;
|
||||||
const PING = 1;
|
const PING = 1;
|
||||||
const PONG = 0;
|
const PONG = 0;
|
||||||
const DATA_HEADER_LENGTH = 1;
|
const DATA_HEADER_LENGTH = 1;
|
||||||
|
/*80, 79, 78, 71是 PONG 字符串转换字节的数组*/
|
||||||
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._url = option.url;
|
this._url = option.url;
|
||||||
// 是否设置重新连接
|
// 是否设置重新连接
|
||||||
this._reconnection = option.reconnection || true;
|
this._reconnection = option.reconnection || true;
|
||||||
// 是否建立缓存池,默认true,如果建立缓存池,会将因为程序错误导致未发送成功的消息发送
|
|
||||||
this._buffer = option.buffer || true;
|
|
||||||
/// on方法注册的事件
|
/// on方法注册的事件
|
||||||
this.on_register = {};
|
this.on_register = {};
|
||||||
// 是否已成功连接
|
// 是否已成功连接
|
||||||
this._connectioned = false;
|
this._connectioned = false;
|
||||||
// 缓存池
|
|
||||||
this._buffer_register = [];
|
|
||||||
// 发送缓存池的数据
|
|
||||||
this._auto_emit_buffer_data = option.autoEmitBuffer || false;
|
|
||||||
// 被动断开
|
// 被动断开
|
||||||
this.closed = false;
|
this.closed = false;
|
||||||
// 开始重连
|
// 开始重连
|
||||||
this.begin_reconnection = false;
|
this.begin_reconnection = false;
|
||||||
// 多少毫秒发送一次心跳
|
|
||||||
this._heart_rate = option.heartRate > 0 ? option.heartRate : 60000;
|
|
||||||
// 后端心跳字段
|
|
||||||
this._heart_rate_type = option.heartRateType || "HEARTBEAT";
|
|
||||||
this.init();
|
this.init();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -82,20 +72,7 @@ export default class Socket {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* 给缓存池添加记录
|
|
||||||
*/
|
|
||||||
async addBuffer(data = {}) {
|
|
||||||
const da = JSON.stringify(data);
|
|
||||||
this._buffer_register.push(data);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 获取缓存池
|
|
||||||
*/
|
|
||||||
async getBuffer() {
|
|
||||||
return this._buffer_register;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取连接状态
|
* 获取连接状态
|
||||||
@ -113,35 +90,7 @@ export default class Socket {
|
|||||||
this.SocketTask && this._connectioned && this.SocketTask.close();
|
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!");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// 绑定账号
|
// 绑定账号
|
||||||
bindAccount(account) {
|
bindAccount(account) {
|
||||||
console.log(account)
|
console.log(account)
|
||||||
@ -187,7 +136,6 @@ export default class Socket {
|
|||||||
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);
|
||||||
body.getDataMap().set("packageName", APP_PACKAGE);
|
|
||||||
body.getDataMap().set("deviceId", deviceId);
|
body.getDataMap().set("deviceId", deviceId);
|
||||||
body.getDataMap().set("deviceName", browser.name);
|
body.getDataMap().set("deviceName", browser.name);
|
||||||
body.getDataMap().set("language", browser.appLanguage);
|
body.getDataMap().set("language", browser.appLanguage);
|
||||||
@ -200,24 +148,15 @@ export default class Socket {
|
|||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
//绑定tag
|
|
||||||
async bindRoomTag(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) {
|
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;
|
||||||
protobuf.set(data, 1);
|
protobuf.set(data, 1);
|
||||||
const buffer = protobuf
|
//微信小程序无法使用Uint8Array 需要转换一下
|
||||||
|
const buffer = new Uint8Array(protobuf).buffer;
|
||||||
this.SocketTask.send({
|
this.SocketTask.send({
|
||||||
data: buffer,
|
data: buffer,
|
||||||
success: (res) => {
|
success: (res) => {
|
||||||
@ -269,17 +208,7 @@ export default class Socket {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* 开始发送心跳
|
|
||||||
*/
|
|
||||||
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);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 将心跳结束
|
* 将心跳结束
|
||||||
@ -374,8 +303,9 @@ export default class Socket {
|
|||||||
pong[0] = PONG;
|
pong[0] = PONG;
|
||||||
pong.set(PONG_BODY, 1);
|
pong.set(PONG_BODY, 1);
|
||||||
// console.log('心跳')
|
// console.log('心跳')
|
||||||
|
const buffer = new Uint8Array(pong).buffer;
|
||||||
this.SocketTask.send({
|
this.SocketTask.send({
|
||||||
data: pong,
|
data: buffer,
|
||||||
fail: (e) => {
|
fail: (e) => {
|
||||||
throw new UniSocketError("Failed to send message to server... " + e);
|
throw new UniSocketError("Failed to send message to server... " + e);
|
||||||
},
|
},
|
||||||
|
@ -1,3 +1,6 @@
|
|||||||
#微信小程序客户端代码示例
|
#微信小程序客户端代码示例
|
||||||
|
|
||||||
该示例使用json编解码策略,没有使用protobuf,所以服务端需要设置消息编码格式为json
|
该示例使用json编解码策略,没有使用protobuf,所以服务端需要设置消息编码格式为json
|
||||||
|
|
||||||
|
如果使用protobuf 请使用如下SDK
|
||||||
|
cim-client-sdk/cim-uniapp-sdk
|
Loading…
x
Reference in New Issue
Block a user