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();