添加窗口焦点事件监听,优化 WebSocket 连接重启逻辑

This commit is contained in:
hi2hi 2024-12-04 14:17:49 +00:00
parent 861261a969
commit 845d21ca70

View File

@ -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) => {