feat(websocket): 添加清空连接状态方法用于退出登录
新增clearConnectionState方法,用于在用户退出登录时清空WebSocket连接状态。该方法会关闭现有连接、停止所有定时器并重置所有相关状态变量,确保用户登出后不会保留之前的连接信息。
This commit is contained in:
@@ -61,6 +61,7 @@ interface WebSocketState {
|
||||
clearMessages: () => void;
|
||||
markAsRead: () => void;
|
||||
reconnect: () => void;
|
||||
clearConnectionState: () => void; // 清空连接状态
|
||||
|
||||
// 内部方法
|
||||
_handleOpen: () => void;
|
||||
@@ -289,6 +290,34 @@ export const useWebSocketStore = createPersistStore<WebSocketState>(
|
||||
}
|
||||
},
|
||||
|
||||
// 清空连接状态(用于退出登录时)
|
||||
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();
|
||||
|
||||
Reference in New Issue
Block a user