添加WebSocket状态管理中的活跃状态请求逻辑,防止频繁请求。引入新的状态变量以跟踪最后请求时间,并在发送活跃状态请求时进行时间间隔检查。清理相关状态以确保一致性。
This commit is contained in:
@@ -53,6 +53,7 @@ interface WebSocketState {
|
||||
reconnectTimer: NodeJS.Timeout | null;
|
||||
aliveStatusTimer: NodeJS.Timeout | null; // 客服用户状态查询定时器
|
||||
aliveStatusUnsubscribe: (() => void) | null;
|
||||
aliveStatusLastRequest: number | null;
|
||||
|
||||
// 方法
|
||||
connect: (config: Partial<WebSocketConfig>) => void;
|
||||
@@ -88,6 +89,8 @@ const DEFAULT_CONFIG: WebSocketConfig = {
|
||||
maxReconnectAttempts: 5,
|
||||
};
|
||||
|
||||
const ALIVE_STATUS_MIN_INTERVAL = 5 * 1000; // ms
|
||||
|
||||
export const useWebSocketStore = createPersistStore<WebSocketState>(
|
||||
(set, get) => ({
|
||||
status: WebSocketStatus.DISCONNECTED,
|
||||
@@ -99,6 +102,7 @@ export const useWebSocketStore = createPersistStore<WebSocketState>(
|
||||
reconnectTimer: null,
|
||||
aliveStatusTimer: null,
|
||||
aliveStatusUnsubscribe: null,
|
||||
aliveStatusLastRequest: null,
|
||||
|
||||
// 连接WebSocket
|
||||
connect: (config: Partial<WebSocketConfig>) => {
|
||||
@@ -234,11 +238,6 @@ export const useWebSocketStore = createPersistStore<WebSocketState>(
|
||||
currentState.status !== WebSocketStatus.CONNECTED ||
|
||||
!currentState.ws
|
||||
) {
|
||||
// Toast.show({
|
||||
// content: "WebSocket未连接,正在重新连接...",
|
||||
// position: "top",
|
||||
// });
|
||||
|
||||
// 重置连接状态并发起重新连接
|
||||
set({ status: WebSocketStatus.DISCONNECTED });
|
||||
if (currentState.config) {
|
||||
@@ -485,6 +484,14 @@ export const useWebSocketStore = createPersistStore<WebSocketState>(
|
||||
return;
|
||||
}
|
||||
|
||||
const now = Date.now();
|
||||
if (
|
||||
state.aliveStatusLastRequest &&
|
||||
now - state.aliveStatusLastRequest < ALIVE_STATUS_MIN_INTERVAL
|
||||
) {
|
||||
return;
|
||||
}
|
||||
|
||||
const { customerList } = useCustomerStore.getState();
|
||||
const { kfUserList } = useCkChatStore.getState();
|
||||
const targets =
|
||||
@@ -498,6 +505,7 @@ export const useWebSocketStore = createPersistStore<WebSocketState>(
|
||||
state.sendCommand("CmdRequestWechatAccountsAliveStatus", {
|
||||
wechatAccountIds: targets.map(v => v.id),
|
||||
});
|
||||
set({ aliveStatusLastRequest: now });
|
||||
}
|
||||
};
|
||||
|
||||
@@ -556,7 +564,11 @@ export const useWebSocketStore = createPersistStore<WebSocketState>(
|
||||
if (currentState.aliveStatusTimer) {
|
||||
clearInterval(currentState.aliveStatusTimer);
|
||||
}
|
||||
set({ aliveStatusTimer: null, aliveStatusUnsubscribe: null });
|
||||
set({
|
||||
aliveStatusTimer: null,
|
||||
aliveStatusUnsubscribe: null,
|
||||
aliveStatusLastRequest: null,
|
||||
});
|
||||
},
|
||||
}),
|
||||
{
|
||||
@@ -568,6 +580,7 @@ export const useWebSocketStore = createPersistStore<WebSocketState>(
|
||||
messages: state.messages.slice(-100), // 只保留最近100条消息
|
||||
unreadCount: state.unreadCount,
|
||||
reconnectAttempts: state.reconnectAttempts,
|
||||
aliveStatusLastRequest: state.aliveStatusLastRequest,
|
||||
// 注意:定时器不需要持久化,重新连接时会重新创建
|
||||
}),
|
||||
onRehydrateStorage: () => state => {
|
||||
|
||||
Reference in New Issue
Block a user