From 8a7991261f0534b24979e032bbdb69ad9fb34db5 Mon Sep 17 00:00:00 2001 From: YunaiV Date: Sun, 26 Apr 2026 23:08:23 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20fix(im):=20=E4=B8=BB=E5=8A=A8?= =?UTF-8?q?=E6=96=AD=E5=BC=80=20WS=20=E5=90=8E=E4=B8=8D=E5=86=8D=E8=87=AA?= =?UTF-8?q?=E5=8A=A8=E9=87=8D=E8=BF=9E=EF=BC=8C=E5=90=8C=E6=AD=A5=E5=A4=8D?= =?UTF-8?q?=E4=BD=8D=20isConnected=20disconnect()=20=E4=B9=8B=E5=89=8D?= =?UTF-8?q?=E8=B0=83=E7=94=A8=20socket.close()=20=E6=98=AF=E5=BC=82?= =?UTF-8?q?=E6=AD=A5=E8=A7=A6=E5=8F=91=20onclose=EF=BC=8C=E5=9B=9E?= =?UTF-8?q?=E8=B0=83=E9=87=8C=E4=BC=9A=20=E6=97=A0=E6=9D=A1=E4=BB=B6?= =?UTF-8?q?=E8=B5=B0=20reconnect=EF=BC=8C=E5=AF=BC=E8=87=B4=E7=A6=BB?= =?UTF-8?q?=E5=BC=80=20IM=20=E4=B8=BB=E5=A3=B3=E5=90=8E=203=20=E7=A7=92?= =?UTF-8?q?=E5=8F=88=E4=BC=9A=E5=9C=A8=E5=90=8E=E5=8F=B0=E9=87=8D=E6=96=B0?= =?UTF-8?q?=E6=89=93=E5=BC=80=20WebSocket=E3=80=82=20=E4=BF=AE=E5=A4=8D?= =?UTF-8?q?=EF=BC=9A=20-=20close=20=E5=89=8D=E5=85=88=E8=A7=A3=E7=BB=91=20?= =?UTF-8?q?onclose=20/=20onerror=20handler=EF=BC=8C=E4=B8=BB=E5=8A=A8?= =?UTF-8?q?=E5=85=B3=E9=97=AD=E8=B7=AF=E5=BE=84=E4=B8=8D=E5=86=8D=E8=B5=B0?= =?UTF-8?q?=E8=87=AA=E5=8A=A8=E9=87=8D=E8=BF=9E=20-=20onclose=20=E5=B7=B2?= =?UTF-8?q?=E8=A2=AB=E8=A7=A3=E7=BB=91=E5=90=8E=E6=B2=A1=E4=BA=BA=E5=B8=AE?= =?UTF-8?q?=E6=88=91=E4=BB=AC=E8=AE=BE=20isConnected=3Dfalse=EF=BC=8Cdisco?= =?UTF-8?q?nnect=20=E5=86=85=E6=89=8B=E5=8A=A8=E5=A4=8D=E4=BD=8D=EF=BC=8C?= =?UTF-8?q?=20=20=20=E9=81=BF=E5=85=8D=20socket=3Dnull=20=E4=BD=86=20isCon?= =?UTF-8?q?nected=3Dtrue=20=E7=9A=84=E7=8A=B6=E6=80=81=E4=B8=8D=E4=B8=80?= =?UTF-8?q?=E8=87=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/views/im/home/store/websocketStore.ts | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/views/im/home/store/websocketStore.ts b/src/views/im/home/store/websocketStore.ts index 8ff3c69be..07918f3d1 100644 --- a/src/views/im/home/store/websocketStore.ts +++ b/src/views/im/home/store/websocketStore.ts @@ -544,9 +544,15 @@ export const useImWebSocketStore = defineStore('imWebSocketStore', { /** 主动断开(切换用户 / 退出登录时用):关 socket + 停心跳 + 取消待重连 */ disconnect() { if (this.socket) { + // close() 异步触发 onclose / onerror,回调里会无条件 reconnect; + // 主动关闭路径必须先解绑这两个 handler,否则 3 秒后会自动连回去 + this.socket.onclose = null + this.socket.onerror = null this.socket.close() this.socket = null } + // onclose 已被解绑,不会再帮我们设 isConnected=false,这里手动复位 + this.isConnected = false this.stopHeartbeat() if (this.reconnectTimer) { clearTimeout(this.reconnectTimer) @@ -557,7 +563,9 @@ export const useImWebSocketStore = defineStore('imWebSocketStore', { /** 自动重连,3 秒后再试(onclose / onerror 都会进来,靠 reconnectTimer 自身防重) */ reconnect() { this.stopHeartbeat() - if (this.reconnectTimer) clearTimeout(this.reconnectTimer) + if (this.reconnectTimer) { + clearTimeout(this.reconnectTimer) + } this.reconnectTimer = setTimeout(() => { console.log('[IM WS] reconnecting...') this.connect()