ws reconnect
This commit is contained in:
parent
14f611e796
commit
19d020faec
@ -54,7 +54,7 @@ export function WSProvider({ children }: MapProviderProps) {
|
||||
reconnectAttempts: 0
|
||||
});
|
||||
const [isOnline, setIsOnline] = useState(typeof window !== 'undefined' ? navigator.onLine : true);
|
||||
|
||||
|
||||
const { data, loading, error, restart } = useSubscription(SUBSCRIPTION_QUERY, {
|
||||
errorPolicy: 'all',
|
||||
onError: (error) => {
|
||||
@ -91,7 +91,7 @@ export function WSProvider({ children }: MapProviderProps) {
|
||||
useEffect(() => {
|
||||
const unsubscribe = subscribeToConnectionState((state) => {
|
||||
setConnectionState(state);
|
||||
|
||||
|
||||
switch (state.status) {
|
||||
case 'connecting':
|
||||
setWsStatus(WsStatus.CONNECTING);
|
||||
@ -113,16 +113,25 @@ export function WSProvider({ children }: MapProviderProps) {
|
||||
|
||||
// 监听订阅状态变化
|
||||
useEffect(() => {
|
||||
if (loading && wsStatus !== WsStatus.CONNECTING) {
|
||||
setWsStatus(WsStatus.CONNECTING);
|
||||
if (loading) {
|
||||
// 只在第一次加载时设置为 connecting,避免覆盖 WebSocket 的实际状态
|
||||
if (connectionState.status === 'disconnected') {
|
||||
setWsStatus(WsStatus.CONNECTING);
|
||||
}
|
||||
} else if (error && !loading) {
|
||||
if (isOnline) {
|
||||
console.error('WebSocket subscription error:', error);
|
||||
if (isOnline && connectionState.status !== 'reconnecting') {
|
||||
setTimeout(() => {
|
||||
restart();
|
||||
}, 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 = () => {
|
||||
Sentry.startSpan({
|
||||
|
||||
Loading…
Reference in New Issue
Block a user