From 8083e94ecd99278da7c2197e5074176f6bc36b96 Mon Sep 17 00:00:00 2001 From: InfinityPacer <160988576+InfinityPacer@users.noreply.github.com> Date: Thu, 17 Oct 2024 16:59:51 +0800 Subject: [PATCH] fix(message): add delay to prevent 403 --- src/layouts/components/UserNotification.vue | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/src/layouts/components/UserNotification.vue b/src/layouts/components/UserNotification.vue index 48abca0a..a8e9170d 100644 --- a/src/layouts/components/UserNotification.vue +++ b/src/layouts/components/UserNotification.vue @@ -16,15 +16,18 @@ const appsMenu = ref(false) // SSE持续接收消息 function startSSEMessager() { - eventSource = new EventSource(`${import.meta.env.VITE_API_BASE_URL}system/message`) - eventSource.addEventListener('message', event => { - if (event.data) { - const noti: SystemNotification = JSON.parse(event.data) - notificationList.value.unshift(noti) - hasNewMessage.value = true - // TODO 在顶部显示消息汽泡 - } - }) + // 延迟 3 秒启动 SSE,避免相关认证信息尚未写入 Cookie 导致 403 + setTimeout(() => { + eventSource = new EventSource(`${import.meta.env.VITE_API_BASE_URL}system/message`) + eventSource.addEventListener('message', event => { + if (event.data) { + const noti: SystemNotification = JSON.parse(event.data) + notificationList.value.unshift(noti) + hasNewMessage.value = true + // TODO 在顶部显示消息汽泡 + } + }) + }, 3000) } // 页面加载时,加载当前用户数据