mirror of
https://github.com/hi2shark/nazhua.git
synced 2026-01-12 07:10:43 +08:00
✨ 添加窗口焦点事件监听,优化 WebSocket 连接重启逻辑
This commit is contained in:
parent
861261a969
commit
845d21ca70
11
src/App.vue
11
src/App.vue
@ -15,6 +15,8 @@ import sleep from '@/utils/sleep';
|
||||
import LayoutMain from './layout/main.vue';
|
||||
|
||||
import activeWebsocketService, {
|
||||
wsService,
|
||||
restart,
|
||||
msg,
|
||||
} from './ws';
|
||||
|
||||
@ -43,9 +45,11 @@ async function wsReconnect() {
|
||||
if (stopReconnect) {
|
||||
return;
|
||||
}
|
||||
stopReconnect = true;
|
||||
await sleep(1000);
|
||||
console.log('reconnect ws');
|
||||
activeWebsocketService();
|
||||
stopReconnect = false;
|
||||
}
|
||||
|
||||
onMounted(async () => {
|
||||
@ -65,6 +69,13 @@ onMounted(async () => {
|
||||
store.dispatch('watchWsMsg');
|
||||
});
|
||||
activeWebsocketService();
|
||||
|
||||
// 监听窗口重新获得焦点
|
||||
window.addEventListener('focus', () => {
|
||||
if (wsService.connected !== 1) {
|
||||
restart();
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
window.addEventListener('unhandledrejection', (event) => {
|
||||
|
||||
Loading…
Reference in New Issue
Block a user