Skip to content

Commit

Permalink
perf: 优化websocket连接机制
Browse files Browse the repository at this point in the history
  • Loading branch information
kuaifan committed Apr 2, 2022
1 parent 813a49b commit 9a0a04e
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 8 deletions.
7 changes: 7 additions & 0 deletions resources/assets/js/pages/manage.vue
Original file line number Diff line number Diff line change
Expand Up @@ -668,6 +668,13 @@ export default {
immediate: true
},
userId: {
handler() {
this.$store.dispatch("websocketConnection")
},
immediate: true
},
wsMsg: {
handler(info) {
const {type, action} = info;
Expand Down
5 changes: 1 addition & 4 deletions resources/assets/js/pages/manage/components/FileContent.vue
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ export default {
return true
}
}
this.$store.dispatch("websocketConnection")
}
},
Expand Down Expand Up @@ -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--;
Expand Down
13 changes: 9 additions & 4 deletions resources/assets/js/store/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,6 @@ export default {
state.userIsAdmin = $A.inArray('admin', userInfo.identity);
$A.setStorage("userInfo", state.userInfo);
dispatch("getBasicData");
dispatch("websocketConnection");
resolve()
});
},
Expand Down Expand Up @@ -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())
Expand All @@ -2181,7 +2183,7 @@ export default {
//
clearTimeout(state.wsTimeout);
state.wsTimeout = setTimeout(() => {
dispatch('websocketConnection');
wsRandom === state.wsRandom && dispatch('websocketConnection');
}, 3000);
};
state.ws.onerror = (e) => {
Expand All @@ -2190,7 +2192,7 @@ export default {
//
clearTimeout(state.wsTimeout);
state.wsTimeout = setTimeout(() => {
dispatch('websocketConnection');
wsRandom === state.wsRandom && dispatch('websocketConnection');
}, 3000);
};
state.ws.onmessage = (e) => {
Expand Down Expand Up @@ -2453,6 +2455,9 @@ export default {
* @param state
*/
websocketClose({state}) {
state.ws && state.ws.close();
if (state.ws) {
state.ws.close();
state.ws = null;
}
}
}
1 change: 1 addition & 0 deletions resources/assets/js/store/state.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ const stateData = {
wsMsg: {},
wsCall: {},
wsTimeout: null,
wsRandom: 0,
wsOpenNum: 0,
wsListener: {},
wsReadTimeout: null,
Expand Down

0 comments on commit 9a0a04e

Please sign in to comment.