mirror of
https://github.com/hi2shark/nazhua.git
synced 2026-01-17 17:50: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 LayoutMain from './layout/main.vue';
|
||||||
|
|
||||||
import activeWebsocketService, {
|
import activeWebsocketService, {
|
||||||
|
wsService,
|
||||||
|
restart,
|
||||||
msg,
|
msg,
|
||||||
} from './ws';
|
} from './ws';
|
||||||
|
|
||||||
@ -43,9 +45,11 @@ async function wsReconnect() {
|
|||||||
if (stopReconnect) {
|
if (stopReconnect) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
stopReconnect = true;
|
||||||
await sleep(1000);
|
await sleep(1000);
|
||||||
console.log('reconnect ws');
|
console.log('reconnect ws');
|
||||||
activeWebsocketService();
|
activeWebsocketService();
|
||||||
|
stopReconnect = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
onMounted(async () => {
|
onMounted(async () => {
|
||||||
@ -65,6 +69,13 @@ onMounted(async () => {
|
|||||||
store.dispatch('watchWsMsg');
|
store.dispatch('watchWsMsg');
|
||||||
});
|
});
|
||||||
activeWebsocketService();
|
activeWebsocketService();
|
||||||
|
|
||||||
|
// 监听窗口重新获得焦点
|
||||||
|
window.addEventListener('focus', () => {
|
||||||
|
if (wsService.connected !== 1) {
|
||||||
|
restart();
|
||||||
|
}
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
window.addEventListener('unhandledrejection', (event) => {
|
window.addEventListener('unhandledrejection', (event) => {
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user