diff --git a/src/api/socket.js b/src/api/socket.js index 5abf5514..c24d59e0 100644 --- a/src/api/socket.js +++ b/src/api/socket.js @@ -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() } } diff --git a/src/store/socket.js b/src/store/socket.js index 2fe21721..c3bd8109 100644 --- a/src/store/socket.js +++ b/src/store/socket.js @@ -18,6 +18,11 @@ export const useSocketStore = defineStore('socketStore',{ let _baseUrl = get_base_url().split('http').join('ws') if(this.SocketObj && this.SocketObj?.close) return this.SocketObj = new SocketClass(_baseUrl + '/trastudy/trasoket/open?summary=' + getToken()); + this.SocketObj.on('close', () => { + setTimeout(()=>{ + this.SocketObj = null + },100) + }) }, closeSocket() { if(!(this.SocketObj && this.SocketObj?.close)) return