chore: Optimize SSE connections

This commit is contained in:
jmal 2024-03-26 14:19:02 +08:00
parent 6f91ef5430
commit c837c43c32

View File

@ -1,4 +1,5 @@
import Bus from "@/assets/js/bus"; import Bus from "@/assets/js/bus";
import _ from "lodash";
import store from "@/store"; import store from "@/store";
import {generateUUID} from "ant-design-vue/lib/vc-select/util"; import {generateUUID} from "ant-design-vue/lib/vc-select/util";
@ -6,9 +7,11 @@ let eventSource = null;
let timer = null; let timer = null;
let lastHeartbeat = new Date().getTime(); let lastHeartbeat = new Date().getTime();
export function connectToSSE(username) { const uuid = generateUUID();
const throttledConnectSSE = _.throttle(username => {
if (typeof (EventSource) !== 'undefined') { if (typeof (EventSource) !== 'undefined') {
const url = `/api/events?username=${username}&uuid=${generateUUID()}`; const url = `/api/events?username=${username}&uuid=${uuid}`;
eventSource = new EventSource(url); eventSource = new EventSource(url);
eventSource.addEventListener('message', function (event) { eventSource.addEventListener('message', function (event) {
if (event.data === 'h') { if (event.data === 'h') {
@ -39,10 +42,13 @@ export function connectToSSE(username) {
connectToSSE(username); connectToSSE(username);
} }
}, 3000); }, 3000);
} else { } else {
console.error('EventSource is not supported by the browser'); console.error('EventSource is not supported by the browser');
} }
}, 3000);
export function connectToSSE(username) {
throttledConnectSSE(username);
} }
function onMessage(msg) { function onMessage(msg) {