From 9a0a04ed7659ba20be8f0ba8bb4574cd2dc1f55a Mon Sep 17 00:00:00 2001 From: kuaifan Date: Sat, 2 Apr 2022 08:19:13 +0800 Subject: [PATCH] =?UTF-8?q?perf:=20=E4=BC=98=E5=8C=96websocket=E8=BF=9E?= =?UTF-8?q?=E6=8E=A5=E6=9C=BA=E5=88=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- resources/assets/js/pages/manage.vue | 7 +++++++ .../js/pages/manage/components/FileContent.vue | 5 +---- resources/assets/js/store/actions.js | 13 +++++++++---- resources/assets/js/store/state.js | 1 + 4 files changed, 18 insertions(+), 8 deletions(-) diff --git a/resources/assets/js/pages/manage.vue b/resources/assets/js/pages/manage.vue index 1b68a2d19..b44e8b64a 100644 --- a/resources/assets/js/pages/manage.vue +++ b/resources/assets/js/pages/manage.vue @@ -668,6 +668,13 @@ export default { immediate: true }, + userId: { + handler() { + this.$store.dispatch("websocketConnection") + }, + immediate: true + }, + wsMsg: { handler(info) { const {type, action} = info; diff --git a/resources/assets/js/pages/manage/components/FileContent.vue b/resources/assets/js/pages/manage/components/FileContent.vue index 59a0c9371..b76f80917 100644 --- a/resources/assets/js/pages/manage/components/FileContent.vue +++ b/resources/assets/js/pages/manage/components/FileContent.vue @@ -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--; diff --git a/resources/assets/js/store/actions.js b/resources/assets/js/store/actions.js index ec69e43d6..71e09f19b 100644 --- a/resources/assets/js/store/actions.js +++ b/resources/assets/js/store/actions.js @@ -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; + } } } diff --git a/resources/assets/js/store/state.js b/resources/assets/js/store/state.js index 0b1a36f56..0ad4a6953 100644 --- a/resources/assets/js/store/state.js +++ b/resources/assets/js/store/state.js @@ -43,6 +43,7 @@ const stateData = { wsMsg: {}, wsCall: {}, wsTimeout: null, + wsRandom: 0, wsOpenNum: 0, wsListener: {}, wsReadTimeout: null,