From c27642bc1234d5c0a447b67476c7057f0c910bc8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=B6=85=E7=BA=A7=E8=80=81=E7=99=BD=E5=85=94?= Date: Wed, 3 Sep 2025 16:36:10 +0800 Subject: [PATCH] =?UTF-8?q?feat(websocket):=20=E6=B7=BB=E5=8A=A0=E6=B8=85?= =?UTF-8?q?=E7=A9=BA=E8=BF=9E=E6=8E=A5=E7=8A=B6=E6=80=81=E6=96=B9=E6=B3=95?= =?UTF-8?q?=E7=94=A8=E4=BA=8E=E9=80=80=E5=87=BA=E7=99=BB=E5=BD=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 新增clearConnectionState方法,用于在用户退出登录时清空WebSocket连接状态。该方法会关闭现有连接、停止所有定时器并重置所有相关状态变量,确保用户登出后不会保留之前的连接信息。 --- .../src/store/module/websocket/websocket.ts | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/Cunkebao/src/store/module/websocket/websocket.ts b/Cunkebao/src/store/module/websocket/websocket.ts index d67e2a06..46f9ed18 100644 --- a/Cunkebao/src/store/module/websocket/websocket.ts +++ b/Cunkebao/src/store/module/websocket/websocket.ts @@ -61,6 +61,7 @@ interface WebSocketState { clearMessages: () => void; markAsRead: () => void; reconnect: () => void; + clearConnectionState: () => void; // 清空连接状态 // 内部方法 _handleOpen: () => void; @@ -289,6 +290,34 @@ export const useWebSocketStore = createPersistStore( } }, + // 清空连接状态(用于退出登录时) + clearConnectionState: () => { + const currentState = get(); + + // 断开现有连接 + if (currentState.ws) { + currentState.ws.close(); + } + + // 停止所有定时器 + currentState._stopReconnectTimer(); + currentState._stopAliveStatusTimer(); + + // 重置所有状态 + set({ + status: WebSocketStatus.DISCONNECTED, + ws: null, + config: null, + messages: [], + unreadCount: 0, + reconnectAttempts: 0, + reconnectTimer: null, + aliveStatusTimer: null, + }); + + // console.log("WebSocket连接状态已清空"); + }, + // 内部方法:处理连接打开 _handleOpen: () => { const currentState = get();