ws reconnect

This commit is contained in:
Tsuki 2025-08-25 00:14:25 +08:00
parent 14f611e796
commit 19d020faec

View File

@ -113,16 +113,25 @@ export function WSProvider({ children }: MapProviderProps) {
// 监听订阅状态变化 // 监听订阅状态变化
useEffect(() => { useEffect(() => {
if (loading && wsStatus !== WsStatus.CONNECTING) { if (loading) {
// 只在第一次加载时设置为 connecting避免覆盖 WebSocket 的实际状态
if (connectionState.status === 'disconnected') {
setWsStatus(WsStatus.CONNECTING); setWsStatus(WsStatus.CONNECTING);
}
} else if (error && !loading) { } else if (error && !loading) {
if (isOnline) { console.error('WebSocket subscription error:', error);
if (isOnline && connectionState.status !== 'reconnecting') {
setTimeout(() => { setTimeout(() => {
restart(); restart();
}, 5000); }, 5000);
} }
} else if (!loading && !error && data) {
// 确保在有数据时状态是 connected
if (wsStatus !== WsStatus.CONNECTED) {
setWsStatus(WsStatus.CONNECTED);
} }
}, [loading, error, isOnline, restart, wsStatus]); }
}, [loading, error, data, isOnline, restart, wsStatus, connectionState.status]);
const forceReconnect = () => { const forceReconnect = () => {
Sentry.startSpan({ Sentry.startSpan({