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 immediate: true
}, },
userId: {
handler() {
this.$store.dispatch("websocketConnection")
},
immediate: true
},
wsMsg: { wsMsg: {
handler(info) { handler(info) {
const {type, action} = info; const {type, action} = info;

View File

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

View File

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