perf: 优化websocket连接机制

This commit is contained in:
kuaifan 2022-04-02 08:19:13 +08:00
parent 813a49bf67
commit 9a0a04ed76
4 changed files with 18 additions and 8 deletions

View File

@ -668,6 +668,13 @@ export default {
immediate: true
},
userId: {
handler() {
this.$store.dispatch("websocketConnection")
},
immediate: true
},
wsMsg: {
handler(info) {
const {type, action} = info;

View File

@ -126,6 +126,7 @@ export default {
return true
}
}
this.$store.dispatch("websocketConnection")
}
},
@ -269,10 +270,6 @@ export default {
this.loadContent--;
this.contentDetail = data.content;
this.updateBak();
//
if (this.$isSubElectron) {
this.$store.dispatch("websocketConnection")
}
}).catch(({msg}) => {
$A.modalError(msg);
this.loadIng--;

View File

@ -277,7 +277,6 @@ export default {
state.userIsAdmin = $A.inArray('admin', userInfo.identity);
$A.setStorage("userInfo", state.userInfo);
dispatch("getBasicData");
dispatch("websocketConnection");
resolve()
});
},
@ -2170,6 +2169,9 @@ export default {
url = url.replace("http://", "ws://");
url += "?action=web&token=" + state.userToken;
//
const wsRandom = $A.randomString(16);
state.wsRandom = wsRandom;
//
state.ws = new WebSocket(url);
state.ws.onopen = (e) => {
// console.log("[WS] Open", $A.formatDate())
@ -2181,7 +2183,7 @@ export default {
//
clearTimeout(state.wsTimeout);
state.wsTimeout = setTimeout(() => {
dispatch('websocketConnection');
wsRandom === state.wsRandom && dispatch('websocketConnection');
}, 3000);
};
state.ws.onerror = (e) => {
@ -2190,7 +2192,7 @@ export default {
//
clearTimeout(state.wsTimeout);
state.wsTimeout = setTimeout(() => {
dispatch('websocketConnection');
wsRandom === state.wsRandom && dispatch('websocketConnection');
}, 3000);
};
state.ws.onmessage = (e) => {
@ -2453,6 +2455,9 @@ export default {
* @param state
*/
websocketClose({state}) {
state.ws && state.ws.close();
if (state.ws) {
state.ws.close();
state.ws = null;
}
}
}

View File

@ -43,6 +43,7 @@ const stateData = {
wsMsg: {},
wsCall: {},
wsTimeout: null,
wsRandom: 0,
wsOpenNum: 0,
wsListener: {},
wsReadTimeout: null,