fix: ws cleanup before unload

This commit is contained in:
hamster1963 2025-01-09 11:29:39 +08:00
parent 0a64cdc8c3
commit 4da870dd79
3 changed files with 12 additions and 5 deletions

BIN
bun.lockb

Binary file not shown.

View File

@ -24,8 +24,8 @@
"@radix-ui/react-separator": "^1.1.1", "@radix-ui/react-separator": "^1.1.1",
"@radix-ui/react-slot": "^1.1.1", "@radix-ui/react-slot": "^1.1.1",
"@radix-ui/react-switch": "^1.1.2", "@radix-ui/react-switch": "^1.1.2",
"@tanstack/react-query": "^5.62.15", "@tanstack/react-query": "^5.63.0",
"@tanstack/react-query-devtools": "^5.62.15", "@tanstack/react-query-devtools": "^5.63.0",
"@tanstack/react-table": "^8.20.6", "@tanstack/react-table": "^8.20.6",
"@trivago/prettier-plugin-sort-imports": "^5.2.1", "@trivago/prettier-plugin-sort-imports": "^5.2.1",
"@types/d3-geo": "^3.1.0", "@types/d3-geo": "^3.1.0",
@ -37,7 +37,7 @@
"dayjs": "^1.11.13", "dayjs": "^1.11.13",
"framer-motion": "^12.0.0-alpha.2", "framer-motion": "^12.0.0-alpha.2",
"i18n-iso-countries": "^7.13.0", "i18n-iso-countries": "^7.13.0",
"i18next": "^24.2.0", "i18next": "^24.2.1",
"lucide-react": "^0.460.0", "lucide-react": "^0.460.0",
"luxon": "^3.5.0", "luxon": "^3.5.0",
"prettier-plugin-tailwindcss": "^0.6.9", "prettier-plugin-tailwindcss": "^0.6.9",
@ -53,7 +53,7 @@
"devDependencies": { "devDependencies": {
"@eslint/js": "^9.17.0", "@eslint/js": "^9.17.0",
"@types/node": "^22.10.5", "@types/node": "^22.10.5",
"@types/react": "^19.0.2", "@types/react": "^19.0.4",
"@types/react-dom": "^19.0.2", "@types/react-dom": "^19.0.2",
"@vitejs/plugin-react-swc": "^3.7.2", "@vitejs/plugin-react-swc": "^3.7.2",
"autoprefixer": "^10.4.20", "autoprefixer": "^10.4.20",
@ -64,7 +64,7 @@
"postcss": "^8.4.49", "postcss": "^8.4.49",
"tailwindcss": "^3.4.17", "tailwindcss": "^3.4.17",
"typescript": "~5.6.3", "typescript": "~5.6.3",
"typescript-eslint": "^8.19.0", "typescript-eslint": "^8.19.1",
"vite": "^6.0.7" "vite": "^6.0.7"
} }
} }

View File

@ -106,8 +106,15 @@ export const WebSocketProvider: React.FC<WebSocketProviderProps> = ({ url, child
useEffect(() => { useEffect(() => {
connect() connect()
// 添加页面卸载事件监听
const handleBeforeUnload = () => {
cleanup()
}
window.addEventListener('beforeunload', handleBeforeUnload)
return () => { return () => {
cleanup() cleanup()
window.removeEventListener('beforeunload', handleBeforeUnload)
} }
}, [url]) }, [url])