|
@@ -1,4 +1,21 @@
|
|
class websocketUtil {
|
|
class websocketUtil {
|
|
|
|
+ is_open_socket = false;
|
|
|
|
+ socketStatus = 0 //避免重复连接 0 无连接 1-9连接中 10 已连接
|
|
|
|
+ url = '';
|
|
|
|
+ data = '';
|
|
|
|
+ uuid = '';
|
|
|
|
+ timeout = 10000;
|
|
|
|
+ heartbeatInterval = null;
|
|
|
|
+ reconnectTimeOut = null;
|
|
|
|
+ reconnectNum = 0 //重连次数
|
|
|
|
+ msgQueue = []
|
|
|
|
+ socketTask = null //ws对象
|
|
|
|
+ netIsConnected = false
|
|
|
|
+ netType = 'none'
|
|
|
|
+
|
|
|
|
+ autoFlag = true
|
|
|
|
+
|
|
|
|
+
|
|
constructor(url, uuid) {
|
|
constructor(url, uuid) {
|
|
this.is_open_socket = false; //避免重复连接
|
|
this.is_open_socket = false; //避免重复连接
|
|
this.url = url; //地址
|
|
this.url = url; //地址
|
|
@@ -9,13 +26,82 @@ class websocketUtil {
|
|
this.heartbeatInterval = null; //检测服务器端是否还活着
|
|
this.heartbeatInterval = null; //检测服务器端是否还活着
|
|
this.reconnectTimeOut = null; //重连之后多久再次重连
|
|
this.reconnectTimeOut = null; //重连之后多久再次重连
|
|
|
|
|
|
|
|
+ // 监听网络状态变化
|
|
|
|
+ let that = this;
|
|
|
|
+ uni.onNetworkStatusChange((res) => {
|
|
|
|
+ console.log('WebSocket NetStatus', res.isConnected);
|
|
|
|
+ console.log('WebSocket NetStatus', res.networkType);
|
|
|
|
+ that.netIsConnected = res.isConnected
|
|
|
|
+ that.netType = res.networkType
|
|
|
|
+ that.reconnectNum = 0
|
|
|
|
+ if (that.autoFlag) this.reconnect()
|
|
|
|
+ });
|
|
try {
|
|
try {
|
|
- return this.connectSocketInit()
|
|
|
|
|
|
+ uni.getNetworkType({
|
|
|
|
+ success: (res) => {
|
|
|
|
+ console.log('WebSocket getNetworkType', res.networkType);
|
|
|
|
+ that.netType = res.networkType;
|
|
|
|
+ that.netIsConnected = true;
|
|
|
|
+
|
|
|
|
+ that.reconnect();
|
|
|
|
+ }
|
|
|
|
+ });
|
|
|
|
+ // 有return ,则构造方法返回return的对象,没有,则返回new的对象
|
|
|
|
+ // return this.hhh
|
|
} catch (e) {
|
|
} catch (e) {
|
|
- console.log('catch');
|
|
|
|
- this.is_open_socket = false
|
|
|
|
- this.reconnect();
|
|
|
|
|
|
+ console.log('连接初始化失败', e);
|
|
}
|
|
}
|
|
|
|
+ // try {
|
|
|
|
+ // return this.connectSocketInit()
|
|
|
|
+ // } catch (e) {
|
|
|
|
+ // console.log('catch');
|
|
|
|
+ // this.is_open_socket = false
|
|
|
|
+ // this.reconnect();
|
|
|
|
+ // }
|
|
|
|
+ }
|
|
|
|
+ //重新连接
|
|
|
|
+ reconnect() {
|
|
|
|
+ // 失败重连频率
|
|
|
|
+ const reconnect_time = [3000, 3000, 3000, 3000, 10000, 10000, 10000, 20000, 20000, 50000]
|
|
|
|
+ // 自身巡检频率
|
|
|
|
+ let reconnect_timeout = 30000
|
|
|
|
+ //停止发送心跳
|
|
|
|
+ this.heartbeatInterval && clearInterval(this.heartbeatInterval)
|
|
|
|
+ if (this.socketStatus < 10) {
|
|
|
|
+ // 失败重连
|
|
|
|
+ if (this.reconnectNum > 50) return;
|
|
|
|
+ if (this.reconnectNum < this.reconnect_time.length) {
|
|
|
|
+ reconnect_timeout = reconnect_time[this.reconnectNum]
|
|
|
|
+ } else {
|
|
|
|
+ reconnect_timeout = reconnect_time[this.reconnect_time.length - 1]
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ console.log("uniWebsocket 重新连接", this.socketStatus, this.reconnectNum, this.reconnect_timeout)
|
|
|
|
+
|
|
|
|
+ this.reconnectNum = this.reconnectNum + 1
|
|
|
|
+
|
|
|
|
+ this.reconnectTimeOut = setTimeout(() => {
|
|
|
|
+ // 连接创建
|
|
|
|
+ this.connectSocketInit();
|
|
|
|
+ this.reconnect()
|
|
|
|
+ }, reconnect_timeout)
|
|
|
|
+
|
|
|
|
+ return;
|
|
|
|
+ }
|
|
|
|
+ console.log("uniWebsocket 自身巡检", socketStatus, reconnectNum, reconnect_timeout)
|
|
|
|
+
|
|
|
|
+ // 自身巡检循环
|
|
|
|
+ this.reconnectTimeOut = setTimeout(() => {
|
|
|
|
+ // 连接创建
|
|
|
|
+ this.connectSocketInit();
|
|
|
|
+ this.reconnect()
|
|
|
|
+ }, reconnect_timeout)
|
|
|
|
+ //如果不是人为关闭的话,进行重连
|
|
|
|
+ // if (!this.is_open_socket) {
|
|
|
|
+ // this.reconnectTimeOut = setTimeout(() => {
|
|
|
|
+ // this.connectSocketInit();
|
|
|
|
+ // }, 3000)
|
|
|
|
+ // }
|
|
}
|
|
}
|
|
generateSeq() {
|
|
generateSeq() {
|
|
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
|
|
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
|
|
@@ -91,7 +177,7 @@ class websocketUtil {
|
|
seq: that.generateSeq(),
|
|
seq: that.generateSeq(),
|
|
cmd: "login",
|
|
cmd: "login",
|
|
data: {
|
|
data: {
|
|
- "skey": that.uuid,
|
|
|
|
|
|
+ "uuid": that.uuid,
|
|
"appId": 101
|
|
"appId": 101
|
|
}
|
|
}
|
|
});
|
|
});
|
|
@@ -99,13 +185,29 @@ class websocketUtil {
|
|
|
|
|
|
//发送消息
|
|
//发送消息
|
|
send(value) {
|
|
send(value) {
|
|
|
|
+ console.log("send-content:", value);
|
|
value.seq = this.generateSeq();
|
|
value.seq = this.generateSeq();
|
|
// 注:只有连接正常打开中 ,才能正常成功发送消息
|
|
// 注:只有连接正常打开中 ,才能正常成功发送消息
|
|
|
|
+ let that = this;
|
|
this.socketTask.send({
|
|
this.socketTask.send({
|
|
data: JSON.stringify(value),
|
|
data: JSON.stringify(value),
|
|
async success() {
|
|
async success() {
|
|
// console.log("消息发送成功");
|
|
// console.log("消息发送成功");
|
|
},
|
|
},
|
|
|
|
+ fail(e) {
|
|
|
|
+ console.log("send-err:", e);
|
|
|
|
+ // {"errMsg":"sendSocketMessage:fail WebSocket is not connected"} at common/websocketUtil.js:111
|
|
|
|
+ if (e.errMsg.includes("fail WebSocket") || e.errMsg.includes("not connected")) {
|
|
|
|
+ uni.closeSocket();
|
|
|
|
+ that.reconnect();
|
|
|
|
+ console.log('is now reconnect');
|
|
|
|
+ // console.log(`"${str}" 包含 "${substr}"`);
|
|
|
|
+ } else {
|
|
|
|
+ console.log('errMsg', e.errMsg)
|
|
|
|
+ // console.log(`"${str}" 不包含 "${substr}"`);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
});
|
|
});
|
|
}
|
|
}
|
|
//开启心跳检测
|
|
//开启心跳检测
|
|
@@ -139,17 +241,6 @@ class websocketUtil {
|
|
stop() {
|
|
stop() {
|
|
uni.closeSocket();
|
|
uni.closeSocket();
|
|
}
|
|
}
|
|
- //重新连接
|
|
|
|
- reconnect() {
|
|
|
|
- //停止发送心跳
|
|
|
|
- clearInterval(this.heartbeatInterval)
|
|
|
|
- //如果不是人为关闭的话,进行重连
|
|
|
|
- if (!this.is_open_socket) {
|
|
|
|
- this.reconnectTimeOut = setTimeout(() => {
|
|
|
|
- this.connectSocketInit();
|
|
|
|
- }, 3000)
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
//外部获取消息
|
|
//外部获取消息
|
|
getMessage(callback) {
|
|
getMessage(callback) {
|
|
this.socketTask.onMessage((res) => {
|
|
this.socketTask.onMessage((res) => {
|