From b7d8b4e426f366cd680fdd2fed26a96eaa588c7d 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:45 +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 | 41 +++++++++++++++++++++++++++++++++++++++-- 1 file changed, 39 insertions(+), 2 deletions(-) diff --git a/src/utils/push.ts b/src/utils/push.ts index 33404152..2fef2f56 100644 --- a/src/utils/push.ts +++ b/src/utils/push.ts @@ -9,6 +9,9 @@ import { parsePushMessage, resolveNoticeGroup, resolveNoticeTitle, shouldAppendN let closePushConnection: (() => void) | undefined; let stopPushWatchers: Array<() => void> = []; +const KICKED_MESSAGE = 'kicked'; +let pushKicked = false; +let resumePushTimer: ReturnType | undefined; const formatNoticeTime = (timestamp?: number | string) => { const time = timestamp ? new Date(timestamp) : new Date(); @@ -44,6 +47,15 @@ const appendNotice = (raw: string) => { }); }; +const handlePushMessage = (raw: string) => { + if (raw === KICKED_MESSAGE) { + pushKicked = true; + closePush(); + return; + } + appendNotice(raw); +}; + const toNoticeItem = (item: MessageVO) => { const userId = useUserStore().userId; const timestamp = item.createTime ? new Date(item.createTime).getTime() : Date.now(); @@ -91,7 +103,7 @@ const initSsePush = (url: string) => { const stopDataWatch = watch(data, () => { if (!data.value) return; - appendNotice(data.value); + handlePushMessage(data.value); data.value = null; }); stopPushWatchers.push(stopErrorWatch, stopDataWatch); @@ -115,7 +127,7 @@ const initWsPush = (url: string) => { if (String(e.data) === 'pong') { return; } - appendNotice(String(e.data)); + handlePushMessage(String(e.data)); } }); closePushConnection = close; @@ -126,6 +138,7 @@ export const initPush = () => { if (import.meta.env.VITE_APP_MESSAGE_ENABLED === 'false') { return; } + pushKicked = false; const path = import.meta.env.VITE_APP_MESSAGE_PATH || '/resource/message'; const transport = import.meta.env.VITE_APP_MESSAGE_TRANSPORT || 'sse'; if (transport.toLowerCase() === 'websocket') { @@ -153,3 +166,27 @@ export const closePush = () => { stopPushWatchers.forEach(stop => stop()); stopPushWatchers = []; }; + +const resumePushIfNeeded = () => { + if (!pushKicked || !getToken() || document.visibilityState !== 'visible') { + return; + } + if (resumePushTimer) { + clearTimeout(resumePushTimer); + } + resumePushTimer = setTimeout(async () => { + resumePushTimer = undefined; + if (!pushKicked || !getToken() || document.visibilityState !== 'visible') { + return; + } + try { + await initMessageBox(); + } finally { + initPush(); + } + }, 300); +}; + +window.addEventListener('focus', resumePushIfNeeded); +document.addEventListener('visibilitychange', resumePushIfNeeded); +window.addEventListener('online', resumePushIfNeeded);