perf: 优化sse连接
This commit is contained in:
parent
fe204797dd
commit
abcbe39d4f
@ -1351,7 +1351,6 @@ export default {
|
||||
keydown(event) {
|
||||
const { keyCode } = event
|
||||
const isCmd = this.checkCmdKey(event)
|
||||
console.log('keydown', 'keyCode', keyCode, 'isCmd', isCmd)
|
||||
|
||||
const checkPreviewVisible = this.checkPreviewVisible()
|
||||
// space
|
||||
|
||||
@ -5,6 +5,7 @@ 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) => {
|
||||
@ -31,11 +32,16 @@ function connectToSSE(username) {
|
||||
|
||||
eventSource.addEventListener('message', handleMessage);
|
||||
eventSource.addEventListener('error', handleError.bind(null, username));
|
||||
startHeartbeatCheck(username);
|
||||
}
|
||||
|
||||
function handleMessage(event) {
|
||||
const msg = JSON.parse(event.data);
|
||||
onMessage(msg);
|
||||
if (event.data === 'h') {
|
||||
lastHeartbeat = new Date().getTime();
|
||||
} else {
|
||||
const msg = JSON.parse(event.data);
|
||||
onMessage(msg);
|
||||
}
|
||||
}
|
||||
|
||||
function handleError(username) {
|
||||
@ -49,6 +55,18 @@ 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 = {
|
||||
|
||||
@ -209,7 +209,6 @@ const actions = {
|
||||
// update token
|
||||
setToken({ commit }, jmalToken) {
|
||||
commit('SET_TOKEN', jmalToken)
|
||||
console.log('jmalToken', jmalToken)
|
||||
},
|
||||
|
||||
// remove share token
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user