|
@@ -1,4 +1,21 @@
|
|
|
class websocketUtil {
|
|
|
+ var is_open_socket = false;
|
|
|
+ var socketStatus = 0 //避免重复连接 0 无连接 1-9连接中 10 已连接
|
|
|
+ var url = '';
|
|
|
+ var data = '';
|
|
|
+ var uuid = '';
|
|
|
+ var timeout = 10000;
|
|
|
+ var heartbeatInterval = null;
|
|
|
+ var reconnectTimeOut = null;
|
|
|
+ var reconnectNum = 0 //重连次数
|
|
|
+ var msgQueue = []
|
|
|
+ var socketTask = null //ws对象
|
|
|
+ var netIsConnected = false
|
|
|
+ var netType = 'none'
|
|
|
+
|
|
|
+ var autoFlag = true
|
|
|
+
|
|
|
+
|
|
|
constructor(url, uuid) {
|
|
|
this.is_open_socket = false; //避免重复连接
|
|
|
this.url = url; //地址
|
|
@@ -9,13 +26,38 @@ class websocketUtil {
|
|
|
this.heartbeatInterval = 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 {
|
|
|
- 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) {
|
|
|
- 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();
|
|
|
+ // }
|
|
|
}
|
|
|
generateSeq() {
|
|
|
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
|
|
@@ -91,7 +133,7 @@ class websocketUtil {
|
|
|
seq: that.generateSeq(),
|
|
|
cmd: "login",
|
|
|
data: {
|
|
|
- "skey": that.uuid,
|
|
|
+ "uuid": that.uuid,
|
|
|
"appId": 101
|
|
|
}
|
|
|
});
|
|
@@ -99,13 +141,29 @@ class websocketUtil {
|
|
|
|
|
|
//发送消息
|
|
|
send(value) {
|
|
|
+ console.log("send-content:", value);
|
|
|
value.seq = this.generateSeq();
|
|
|
// 注:只有连接正常打开中 ,才能正常成功发送消息
|
|
|
+ let that = this;
|
|
|
this.socketTask.send({
|
|
|
data: JSON.stringify(value),
|
|
|
async success() {
|
|
|
// 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}"`);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
});
|
|
|
}
|
|
|
//开启心跳检测
|