|
@@ -34,8 +34,6 @@ class WebSocketManager {
|
|
|
console.log('已存在连接,正在关闭旧连接...');
|
|
|
this.socketTask.close();
|
|
|
}
|
|
|
- //重新创建链接后、重连次数重置
|
|
|
- this.connectCount = 0;//重连次数
|
|
|
|
|
|
this.isConnecting = true;
|
|
|
console.log('建立 WebSocket 连接:', this.url);
|
|
@@ -83,9 +81,9 @@ class WebSocketManager {
|
|
|
});
|
|
|
|
|
|
this.socketTask.onClose(() => {
|
|
|
- console.log('WebSocket 已关闭');
|
|
|
+ console.log('WebSocket 已关闭',this.connectCount);
|
|
|
this.stopHeartbeat();
|
|
|
- this.connectCount <= 3 && this.reconnect();
|
|
|
+ // this.connectCount <= 3 && this.reconnect();
|
|
|
});
|
|
|
}
|
|
|
|
|
@@ -120,12 +118,9 @@ class WebSocketManager {
|
|
|
* 重新连接
|
|
|
*/
|
|
|
reconnect() {
|
|
|
- clearTimeout(this.reconnectTimer);
|
|
|
- this.reconnectTimer = setTimeout(() => {
|
|
|
- console.log('尝试重新连接...');
|
|
|
- this.connectCount = this.connectCount +1;
|
|
|
- this.connect();
|
|
|
- }, 3000);
|
|
|
+ console.log('尝试重新连接...');
|
|
|
+ this.connectCount = this.connectCount +1;
|
|
|
+ this.connect();
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -147,8 +142,7 @@ class WebSocketManager {
|
|
|
* 主动关闭连接
|
|
|
*/
|
|
|
closeConnection() {
|
|
|
- this.connectCount = 3; //手动关闭不做重新连接
|
|
|
- console.log('主动关闭连接',this.socketTask);
|
|
|
+ console.log('主动关闭连接',this.socketTask,this.isConnected);
|
|
|
uni.closeSocket(this.socketTask);
|
|
|
this.socketTask.close();
|
|
|
this.socketTask = null;
|
|
@@ -165,4 +159,6 @@ class WebSocketManager {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+
|
|
|
+
|
|
|
export default WebSocketManager;
|