|
@@ -11,64 +11,67 @@ const heartCheck = {
|
|
// 表示连接已经关闭或者连接不能打开。
|
|
// 表示连接已经关闭或者连接不能打开。
|
|
if (ws && ws.readyState === 3) return;
|
|
if (ws && ws.readyState === 3) return;
|
|
//发送一个心跳,后端收到后,返回一个心跳消息,onmessage拿到返回的心跳就说明连接正常
|
|
//发送一个心跳,后端收到后,返回一个心跳消息,onmessage拿到返回的心跳就说明连接正常
|
|
- ws && ws.send({data: "ping"});
|
|
|
|
|
|
+ ws && ws.send({ data: "ping" });
|
|
self.serverTimeoutObj = setTimeout(() => {
|
|
self.serverTimeoutObj = setTimeout(() => {
|
|
ws && ws.close();
|
|
ws && ws.close();
|
|
- websocket.reconnect();
|
|
|
|
|
|
+ websocket.reconnect();
|
|
}, self.timeout + 3000);
|
|
}, self.timeout + 3000);
|
|
}, this.timeout);
|
|
}, this.timeout);
|
|
}
|
|
}
|
|
};
|
|
};
|
|
-
|
|
|
|
|
|
+
|
|
const websocket = {
|
|
const websocket = {
|
|
ws: null,
|
|
ws: null,
|
|
url: '',
|
|
url: '',
|
|
connected: false,
|
|
connected: false,
|
|
connecting: false,
|
|
connecting: false,
|
|
reconnectTimeId: null,
|
|
reconnectTimeId: null,
|
|
- callback: null, // 存放回调函数
|
|
|
|
- reconnetCount: 0, // 重连次数
|
|
|
|
|
|
+ callback: null, // 存放回调函数
|
|
|
|
+ reconnetCount: 0, // 重连次数
|
|
lockReconnect: false, // 避免ws重复连接
|
|
lockReconnect: false, // 避免ws重复连接
|
|
createWebSocket(callback) {
|
|
createWebSocket(callback) {
|
|
|
|
+
|
|
if (this.connected || this.connecting) { // 正在连接或者已经连接,请勿重复连接
|
|
if (this.connected || this.connecting) { // 正在连接或者已经连接,请勿重复连接
|
|
return false
|
|
return false
|
|
}
|
|
}
|
|
try {
|
|
try {
|
|
- if (callback) websocket.callback = callback;
|
|
|
|
|
|
+ if (callback) websocket.callback = callback;
|
|
this.connecting = true;
|
|
this.connecting = true;
|
|
// 创建
|
|
// 创建
|
|
this.ws = uni.connectSocket({
|
|
this.ws = uni.connectSocket({
|
|
url: this.url,
|
|
url: this.url,
|
|
success(data) {
|
|
success(data) {
|
|
- // console.log("websocket连接成功!");
|
|
|
|
|
|
+ console.log("websocket连接成功!");
|
|
}
|
|
}
|
|
});
|
|
});
|
|
// 监听 Ws 连接打开
|
|
// 监听 Ws 连接打开
|
|
this.ws.onOpen((res) => {
|
|
this.ws.onOpen((res) => {
|
|
this.connecting = false;
|
|
this.connecting = false;
|
|
this.connected = true;
|
|
this.connected = true;
|
|
- // console.log("websocket连接打开正常!");
|
|
|
|
|
|
+ console.log("websocket连接打开正常!");
|
|
//心跳检测重置
|
|
//心跳检测重置
|
|
heartCheck.start(this.ws);
|
|
heartCheck.start(this.ws);
|
|
- this.reconnetCount && (this.reconnetCount = 0);
|
|
|
|
|
|
+ this.reconnetCount && (this.reconnetCount = 0);
|
|
});
|
|
});
|
|
this.ws.onError((res) => {
|
|
this.ws.onError((res) => {
|
|
this.connecting = false;
|
|
this.connecting = false;
|
|
this.connected = false;
|
|
this.connected = false;
|
|
- // console.log("ws连接错误!", res);
|
|
|
|
|
|
+ console.log("ws连接错误!", res);
|
|
this.reconnect();
|
|
this.reconnect();
|
|
});
|
|
});
|
|
this.ws.onMessage((res) => {
|
|
this.ws.onMessage((res) => {
|
|
- if (res.data === 'ping') {
|
|
|
|
- heartCheck.start(this.ws); //获取到消息,心跳检测重置
|
|
|
|
- this.reconnetCount && (this.reconnetCount = 0);
|
|
|
|
- } else {
|
|
|
|
- const { type, data } = JSON.parse(res.data);
|
|
|
|
- // console.log("ws 收到消息啦:" + type);
|
|
|
|
|
|
+ if (res.data === 'ping') {
|
|
|
|
+ heartCheck.start(this.ws); //获取到消息,心跳检测重置
|
|
|
|
+ console.log("ws 没有收到消息");
|
|
|
|
+ this.reconnetCount && (this.reconnetCount = 0);
|
|
|
|
+ } else {
|
|
|
|
+ // const { type, data } = JSON.parse(res.data);
|
|
|
|
+ const { data } = res;
|
|
|
|
+ console.log("ws 收到消息啦:" + data);
|
|
if (callback) {
|
|
if (callback) {
|
|
- callback(type, data);
|
|
|
|
|
|
+ callback(data);
|
|
} else if (websocket.callback) {
|
|
} else if (websocket.callback) {
|
|
- websocket.callback(type, data);
|
|
|
|
|
|
+ websocket.callback(data);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
});
|
|
});
|
|
@@ -86,12 +89,12 @@ const websocket = {
|
|
},
|
|
},
|
|
reconnect() { // 重连
|
|
reconnect() { // 重连
|
|
if (this.lockReconnect) return;
|
|
if (this.lockReconnect) return;
|
|
- if (this.reconnetCount === 6) {
|
|
|
|
- this.close();
|
|
|
|
- return
|
|
|
|
- }
|
|
|
|
|
|
+ if (this.reconnetCount === 6) {
|
|
|
|
+ this.close();
|
|
|
|
+ return
|
|
|
|
+ }
|
|
this.lockReconnect = true;
|
|
this.lockReconnect = true;
|
|
- this.reconnetCount++;
|
|
|
|
|
|
+ this.reconnetCount++;
|
|
this.reconnectTimeId && clearTimeout(this.reconnectTimeId);
|
|
this.reconnectTimeId && clearTimeout(this.reconnectTimeId);
|
|
this.reconnectTimeId = setTimeout(() => {//没连接上会一直重连,设置延迟避免请求过多
|
|
this.reconnectTimeId = setTimeout(() => {//没连接上会一直重连,设置延迟避免请求过多
|
|
this.createWebSocket(websocket.callback);
|
|
this.createWebSocket(websocket.callback);
|
|
@@ -103,14 +106,14 @@ const websocket = {
|
|
this.ws.close && this.ws.close();
|
|
this.ws.close && this.ws.close();
|
|
this.ws = null;
|
|
this.ws = null;
|
|
}
|
|
}
|
|
- this.connected = false;
|
|
|
|
- this.connecting = false;
|
|
|
|
- this.lockReconnect = false;
|
|
|
|
- this.reconnetCount = 0;
|
|
|
|
- this.reconnectTimeId && clearTimeout(this.reconnectTimeId);
|
|
|
|
- heartCheck.timeoutObj && clearTimeout(heartCheck.timeoutObj);
|
|
|
|
- heartCheck.serverTimeoutObj && clearTimeout(heartCheck.serverTimeoutObj);
|
|
|
|
|
|
+ this.connected = false;
|
|
|
|
+ this.connecting = false;
|
|
|
|
+ this.lockReconnect = false;
|
|
|
|
+ this.reconnetCount = 0;
|
|
|
|
+ this.reconnectTimeId && clearTimeout(this.reconnectTimeId);
|
|
|
|
+ heartCheck.timeoutObj && clearTimeout(heartCheck.timeoutObj);
|
|
|
|
+ heartCheck.serverTimeoutObj && clearTimeout(heartCheck.serverTimeoutObj);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
-
|
|
|
|
|
|
+
|
|
export default websocket;
|
|
export default websocket;
|