perf: 优化sse连接
This commit is contained in:
parent
e4071f0029
commit
cba060d653
@ -5,7 +5,6 @@ import { generateUUID } from "ant-design-vue/lib/vc-select/util";
|
||||
|
||||
let eventSource = null;
|
||||
let timer = null;
|
||||
let lastHeartbeat = new Date().getTime();
|
||||
const uuid = generateUUID();
|
||||
|
||||
const throttledConnectSSE = _.throttle((username) => {
|
||||
@ -32,16 +31,11 @@ function connectToSSE(username) {
|
||||
|
||||
eventSource.addEventListener('message', handleMessage);
|
||||
eventSource.addEventListener('error', handleError.bind(null, username));
|
||||
startHeartbeatCheck(username);
|
||||
}
|
||||
|
||||
function handleMessage(event) {
|
||||
if (event.data === 'h') {
|
||||
lastHeartbeat = new Date().getTime();
|
||||
} else {
|
||||
const msg = JSON.parse(event.data);
|
||||
onMessage(msg);
|
||||
}
|
||||
const msg = JSON.parse(event.data);
|
||||
onMessage(msg);
|
||||
}
|
||||
|
||||
function handleError(username) {
|
||||
@ -55,18 +49,6 @@ function clearExistingTimer() {
|
||||
}
|
||||
}
|
||||
|
||||
function startHeartbeatCheck(username) {
|
||||
clearExistingTimer();
|
||||
timer = setInterval(() => {
|
||||
if (eventSource.readyState === 1 && new Date().getTime() - lastHeartbeat > 5000) {
|
||||
eventSource.close();
|
||||
}
|
||||
if (eventSource.readyState === 2) {
|
||||
throttledConnectSSE(username);
|
||||
}
|
||||
}, 3000);
|
||||
}
|
||||
|
||||
function onMessage(msg) {
|
||||
const { url, body } = msg;
|
||||
const eventMapping = {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user