From 1606dbd76f64bb1dd2ea3e8876341528293c7ea5 Mon Sep 17 00:00:00 2001 From: 疯狂的狮子Li <15040126243@163.com> Date: 星期一, 26 八月 2024 11:45:16 +0800 Subject: [PATCH] !141 发布 vue 版本 5.2.2 与 cloud 版本 2.2.1 Merge pull request !141 from 疯狂的狮子Li/dev --- src/utils/sse.ts | 45 +++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 45 insertions(+), 0 deletions(-) diff --git a/src/utils/sse.ts b/src/utils/sse.ts new file mode 100644 index 0000000..a08f282 --- /dev/null +++ b/src/utils/sse.ts @@ -0,0 +1,45 @@ +import { getToken } from '@/utils/auth'; +import { ElNotification } from 'element-plus'; +import useNoticeStore from '@/store/modules/notice'; + +let message = ''; + +// 鍒濆鍖� +export const initSSE = (url: any) => { + url = url + '?Authorization=Bearer ' + getToken() + '&clientid=' + import.meta.env.VITE_APP_CLIENT_ID + const { + data, + error + } = useEventSource(url, [], { + autoReconnect: { + retries: 10, + delay: 3000, + onFailed() { + console.log('Failed to connect after 10 retries') + }, + } + }); + + watch(error, () => { + console.log('SSE connection error:', error.value) + error.value = null; + }); + + watch(data, () => { + if (!data.value) return; + useNoticeStore().addNotice({ + message: data.value, + read: false, + time: new Date().toLocaleString() + }); + ElNotification({ + title: '娑堟伅', + message: data.value, + type: 'success', + duration: 3000 + }); + data.value = null; + }); +}; + + -- Gitblit v1.9.3