mirror of
https://github.com/woodchen-ink/nezha-dash-v1.git
synced 2025-07-18 09:31:55 +08:00
fix: ws cleanup when page visibility change
This commit is contained in:
parent
4da870dd79
commit
b1c5b6e0a8
@ -106,15 +106,29 @@ export const WebSocketProvider: React.FC<WebSocketProviderProps> = ({ url, child
|
||||
useEffect(() => {
|
||||
connect()
|
||||
|
||||
// 添加页面可见性变化监听
|
||||
const handleVisibilityChange = () => {
|
||||
if (document.hidden) {
|
||||
// 页面隐藏时断开连接
|
||||
cleanup()
|
||||
} else {
|
||||
// 页面可见时重新连接
|
||||
connect()
|
||||
}
|
||||
}
|
||||
|
||||
// 添加页面卸载事件监听
|
||||
const handleBeforeUnload = () => {
|
||||
cleanup()
|
||||
}
|
||||
window.addEventListener('beforeunload', handleBeforeUnload)
|
||||
|
||||
document.addEventListener("visibilitychange", handleVisibilityChange)
|
||||
window.addEventListener("beforeunload", handleBeforeUnload)
|
||||
|
||||
return () => {
|
||||
cleanup()
|
||||
window.removeEventListener('beforeunload', handleBeforeUnload)
|
||||
document.removeEventListener("visibilitychange", handleVisibilityChange)
|
||||
window.removeEventListener("beforeunload", handleBeforeUnload)
|
||||
}
|
||||
}, [url])
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user