mirror of
https://github.com/truewhile/MeBox.git
synced 2026-10-02 12:26:36 +08:00
bug处理优化
bug处理优化
This commit is contained in:
@@ -11,6 +11,8 @@ const MAX_RECONNECT_ATTEMPTS = 5
|
||||
export function useWebSocket(onEvent: (topic: string, payload: unknown) => void) {
|
||||
const ref = useRef<WebSocket | null>(null)
|
||||
const token = useAuthStore((s) => s.token)
|
||||
const onEventRef = useRef(onEvent)
|
||||
onEventRef.current = onEvent
|
||||
|
||||
useEffect(() => {
|
||||
if (!token) return
|
||||
@@ -32,7 +34,7 @@ export function useWebSocket(onEvent: (topic: string, payload: unknown) => void)
|
||||
try {
|
||||
const msg = JSON.parse(ev.data)
|
||||
if (msg && typeof msg.topic === 'string') {
|
||||
onEvent(msg.topic, msg.payload)
|
||||
onEventRef.current(msg.topic, msg.payload)
|
||||
}
|
||||
} catch {
|
||||
// ignore malformed frames
|
||||
@@ -52,5 +54,5 @@ export function useWebSocket(onEvent: (topic: string, payload: unknown) => void)
|
||||
if (timer) window.clearTimeout(timer)
|
||||
ref.current?.close()
|
||||
}
|
||||
}, [token, onEvent])
|
||||
}, [token])
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user