fix:ws错误处理
This commit is contained in:
+10
-2
@@ -26,13 +26,14 @@ export default class WebSocketUtil {
|
||||
close: [], // 连接关闭回调
|
||||
error: [] // 错误处理回调
|
||||
};
|
||||
this.init(); // 初始化WebSocket连接
|
||||
this.init(true); // 初始化WebSocket连接
|
||||
}
|
||||
|
||||
/**
|
||||
* 初始化WebSocket连接
|
||||
*/
|
||||
init() {
|
||||
init(flag = false) {
|
||||
if(flag) this.reconnectCount = 0
|
||||
// 创建WebSocket连接
|
||||
this.socketTask = uni.connectSocket({
|
||||
url: this.url,
|
||||
@@ -60,6 +61,11 @@ export default class WebSocketUtil {
|
||||
// 监听WebSocket消息接收事件
|
||||
this.socketTask.onMessage((res) => {
|
||||
console.log('收到WebSocket消息', res);
|
||||
const { rtnCode } = JSON.parse(res.data)
|
||||
if(['0005', 'QQ0005'].includes(res.rtnCode)) {
|
||||
goto_login_fun();
|
||||
return
|
||||
}
|
||||
this.triggerEvent('message', res); // 触发消息接收事件
|
||||
});
|
||||
|
||||
@@ -198,6 +204,8 @@ export default class WebSocketUtil {
|
||||
} else {
|
||||
console.error('达到最大重连次数,停止尝试');
|
||||
this.triggerEvent('error', new Error('达到最大重连次数'));
|
||||
this.reconnectTimer = null
|
||||
this.close()
|
||||
goto_login_fun()
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user