This commit is contained in:
魏啾
2024-06-28 11:29:09 +08:00
parent 49e5ed4b5c
commit 736ff85457
5 changed files with 113 additions and 42 deletions

View File

@@ -57,13 +57,18 @@ class webSocketUtils {
// 监听连接失败这里代码我注释掉的原因是因为如果服务器关闭后和下面的onclose方法一起发起重连操作这样会导致重复连接
uni.onSocketError((res) => {
console.log('网络断开,请检查!');
// this.socketTask = null;
// this.is_open_socket = false;
this.Close()
this.socketTask = null;
this.is_open_socket = false;
// this.Close()
this.canReconnect = true;
clearInterval(this.heartbeatInterval);
clearInterval(this.reconnectTimeOut);
if (this.connectNum <= 10) {
// uni.showLoading({
// title: `网络连接失败,正尝试第${this.connectNum}次连接`,
// mask: true
// })
uni.$emit('message', 1) //进行重连
uni.showToast({
title: `网络连接失败,正尝试第${this.connectNum}次连接`,
icon: 'none',
@@ -78,6 +83,13 @@ class webSocketUtils {
});
this.connectNum = 1;
this.canReconnect = false;
this.Close()
setTimeout(res => {
uni.switchTab({
url: '/pages/index/index'
})
uni.hideLoading()
}, 1000)
}
});
// 这里仅是事件监听【如果socket关闭了会执行】
@@ -134,10 +146,6 @@ class webSocketUtils {
clearInterval(this.heartbeatInterval);
//如果不是人为关闭的话,进行重连
if (!this.is_open_socket) {
uni.showLoading({
title: '加载中',
mask: true
})
console.log('进行重连');
this.canReconnect = true;
this.reconnectTimeOut = setInterval(() => {