From 2b8793199d0979a3f387da9dd0b019533a1ca73f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=96=AF=E7=8B=82=E7=9A=84=E7=8B=AE=E5=AD=90Li?= <15040126243@163.com> Date: Thu, 25 Jun 2026 17:23:44 +0800 Subject: [PATCH] =?UTF-8?q?fix=20=E4=BF=AE=E5=A4=8D=20push=20=E6=A8=A1?= =?UTF-8?q?=E5=9D=97=20sse/ws=20=E6=B5=8F=E8=A7=88=E5=99=A8=E5=A4=9A?= =?UTF-8?q?=E6=A0=87=E7=AD=BE=E9=A1=B5=E4=BA=92=E6=8C=A4=E6=8E=89=E7=BA=BF?= =?UTF-8?q?=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/utils/push.ts | 43 +++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 41 insertions(+), 2 deletions(-) diff --git a/src/utils/push.ts b/src/utils/push.ts index e34c78fb..efb53447 100644 --- a/src/utils/push.ts +++ b/src/utils/push.ts @@ -14,6 +14,9 @@ let webSocket: WebSocket | undefined; let reconnectTimer: number | undefined; let reconnectAttempts = 0; let pushClosed = true; +const KICKED_MESSAGE = 'kicked'; +let pushKicked = false; +let resumePushTimer: number | undefined; function messageEnabled() { return appEnv.messageEnabled; @@ -67,6 +70,15 @@ function appendNotice(raw: string) { notification.success({ message: title, description: notice.message, duration: 3 }); } +function handlePushMessage(raw: string) { + if (raw === KICKED_MESSAGE) { + pushKicked = true; + closePush(); + return; + } + appendNotice(raw); +} + function buildHttpUrl(path: string) { const base = appEnv.baseApi; const normalizedPath = path.startsWith('/') ? path : `/${path}`; @@ -108,7 +120,7 @@ function initSsePush(path: string) { reconnectAttempts = 0; }; eventSource.onmessage = event => { - if (event.data) appendNotice(event.data); + if (event.data) handlePushMessage(event.data); }; eventSource.onerror = () => { eventSource?.close(); @@ -128,7 +140,7 @@ function initWsPush(path: string) { }; webSocket.onmessage = event => { if (String(event.data) === 'pong') return; - appendNotice(String(event.data)); + handlePushMessage(String(event.data)); }; webSocket.onclose = () => scheduleReconnect(connect, 3, 1000); }; @@ -153,6 +165,7 @@ export async function initMessageBox() { export function initPush() { closePush(); if (!messageEnabled()) return; + pushKicked = false; pushClosed = false; if (appEnv.messageTransport === 'websocket') { initWsPush(appEnv.messagePath); @@ -182,6 +195,32 @@ export function closePush() { reconnectAttempts = 0; } +function resumePushIfNeeded() { + if (!pushKicked || !getToken() || document.visibilityState !== 'visible') { + return; + } + if (resumePushTimer) { + window.clearTimeout(resumePushTimer); + } + resumePushTimer = window.setTimeout(async () => { + resumePushTimer = undefined; + if (!pushKicked || !getToken() || document.visibilityState !== 'visible') { + return; + } + try { + await initMessageBox(); + } finally { + initPush(); + } + }, 300); +} + +if (typeof window !== 'undefined') { + window.addEventListener('focus', resumePushIfNeeded); + document.addEventListener('visibilitychange', resumePushIfNeeded); + window.addEventListener('online', resumePushIfNeeded); +} + export function pushHeaders() { return globalHeaders(); }