优化websocket重连后的逻辑

This commit is contained in:
kuaifan 2022-01-14 13:13:26 +08:00
parent ac26713f86
commit 59b98209ac
6 changed files with 93 additions and 69 deletions

View File

@ -16,6 +16,7 @@ import axios from "axios";
Vue.component('MarkdownPreview', MarkdownPreview) Vue.component('MarkdownPreview', MarkdownPreview)
import {Store} from "le5le-store"; import {Store} from "le5le-store";
import {mapState} from "vuex";
export default { export default {
name: 'AppDown', name: 'AppDown',
@ -27,14 +28,13 @@ export default {
repoData: {}, repoData: {},
status: 0, // 0 12 status: 0, // 0 12
downInfo: {},
releases: {}, releases: {},
releaseTimeout: null, downInfo: {},
websocketOpenSubscribe: null
} }
}, },
mounted() { mounted() {
this.getReleases();
//
if (this.$Electron) { if (this.$Electron) {
this.$Electron.ipcRenderer.on('downloadDone', (event, args) => { this.$Electron.ipcRenderer.on('downloadDone', (event, args) => {
if (args.name == this.repoData.name) { if (args.name == this.repoData.name) {
@ -43,17 +43,11 @@ export default {
} }
}) })
} }
//
this.getReleases();
this.websocketOpenSubscribe = Store.subscribe('websocketOpen', this.getReleaseBefore);
},
destroyed() {
if (this.websocketOpenSubscribe) {
this.websocketOpenSubscribe.unsubscribe();
this.websocketOpenSubscribe = null;
}
}, },
computed: { computed: {
...mapState([
'wsOpenNum',
]),
repoTitle() { repoTitle() {
return this.status == 2 ? '更新客户端' : '客户端下载'; return this.status == 2 ? '更新客户端' : '客户端下载';
}, },
@ -61,6 +55,13 @@ export default {
return this.status && !this.$store.state.windowMax768 && ['login', 'manage-dashboard'].includes(this.$route.name) return this.status && !this.$store.state.windowMax768 && ['login', 'manage-dashboard'].includes(this.$route.name)
} }
}, },
watch: {
wsOpenNum(num) {
if (num <= 1) return
this.wsOpenTimeout && clearTimeout(this.wsOpenTimeout)
this.wsOpenTimeout = setTimeout(this.getReleases, 5000)
},
},
methods: { methods: {
compareVersion(version1, version2) { compareVersion(version1, version2) {
let pA = 0, pB = 0; let pA = 0, pB = 0;
@ -106,11 +107,6 @@ export default {
return 0; return 0;
}, },
getReleaseBefore() {
clearTimeout(this.releaseTimeout)
this.releaseTimeout = setTimeout(this.getReleases, 5000)
},
getReleases() { getReleases() {
if (this.status > 0) { if (this.status > 0) {
return; return;

View File

@ -231,10 +231,6 @@ export default {
dialogMsgSubscribe: null, dialogMsgSubscribe: null,
refreshTimeout: null,
websocketOpenSubscribe: null,
columns: [], columns: [],
projectKeyValue: '', projectKeyValue: '',
@ -270,7 +266,6 @@ export default {
// //
this.addTaskSubscribe = Store.subscribe('addTask', this.onAddTask); this.addTaskSubscribe = Store.subscribe('addTask', this.onAddTask);
this.dialogMsgSubscribe = Store.subscribe('dialogMsgPush', this.addDialogMsg); this.dialogMsgSubscribe = Store.subscribe('dialogMsgPush', this.addDialogMsg);
this.websocketOpenSubscribe = Store.subscribe('websocketOpen', this.refreshBasic);
// //
document.addEventListener('keydown', this.shortcutEvent); document.addEventListener('keydown', this.shortcutEvent);
window.addEventListener('resize', this.innerHeightListener); window.addEventListener('resize', this.innerHeightListener);
@ -289,10 +284,6 @@ export default {
this.dialogMsgSubscribe.unsubscribe(); this.dialogMsgSubscribe.unsubscribe();
this.dialogMsgSubscribe = null; this.dialogMsgSubscribe = null;
} }
if (this.websocketOpenSubscribe) {
this.websocketOpenSubscribe.unsubscribe();
this.websocketOpenSubscribe = null;
}
// //
document.removeEventListener('keydown', this.shortcutEvent); document.removeEventListener('keydown', this.shortcutEvent);
window.removeEventListener('resize', this.innerHeightListener); window.removeEventListener('resize', this.innerHeightListener);
@ -311,6 +302,7 @@ export default {
'cacheProjects', 'cacheProjects',
'projectTotal', 'projectTotal',
'taskId', 'taskId',
'wsOpenNum'
]), ]),
...mapGetters(['taskData', 'dashboardTask']), ...mapGetters(['taskData', 'dashboardTask']),
@ -421,6 +413,14 @@ export default {
this.notificationClass.close(); this.notificationClass.close();
}, 6000); }, 6000);
} }
},
wsOpenNum(num) {
if (num <= 1) return
this.wsOpenTimeout && clearTimeout(this.wsOpenTimeout)
this.wsOpenTimeout = setTimeout(() => {
this.$store.dispatch("getBasicData")
}, 5000)
} }
}, },
@ -624,15 +624,6 @@ export default {
} }
}, },
refreshBasic(num) {
if (num > 1) {
clearTimeout(this.refreshTimeout)
this.refreshTimeout = setTimeout(() => {
this.$store.dispatch("refreshBasicData")
}, 5000)
}
},
notificationInit() { notificationInit() {
this.notificationClass = new notificationKoro(this.$L("打开通知成功")); this.notificationClass = new notificationKoro(this.$L("打开通知成功"));
if (this.notificationClass.support) { if (this.notificationClass.support) {

View File

@ -88,7 +88,7 @@ export default {
}, },
computed: { computed: {
...mapState(['userId', 'cacheTasks', 'taskCompleteTemps']), ...mapState(['userId', 'cacheTasks', 'taskCompleteTemps', 'wsOpenNum']),
...mapGetters(['transforTasks']), ...mapGetters(['transforTasks']),
@ -167,6 +167,16 @@ export default {
rangeTime(time) { rangeTime(time) {
this.getTask(time); this.getTask(time);
}, },
wsOpenNum(num) {
if (num <= 1) return
this.wsOpenTimeout && clearTimeout(this.wsOpenTimeout)
this.wsOpenTimeout = setTimeout(() => {
if (this.$route.name == 'manage-calendar') {
this.setRenderRange();
}
}, 5000)
}
}, },
methods: { methods: {

View File

@ -430,7 +430,7 @@ export default {
}, },
computed: { computed: {
...mapState(['userId', 'userToken', 'userIsAdmin', 'userInfo', 'files']), ...mapState(['userId', 'userToken', 'userIsAdmin', 'userInfo', 'files', 'wsOpenNum']),
actionUrl() { actionUrl() {
return $A.apiUrl('file/content/upload?pid=' + this.pid) return $A.apiUrl('file/content/upload?pid=' + this.pid)
@ -507,6 +507,16 @@ export default {
this.getFileList(); this.getFileList();
} }
}, },
wsOpenNum(num) {
if (num <= 1) return
this.wsOpenTimeout && clearTimeout(this.wsOpenTimeout)
this.wsOpenTimeout = setTimeout(() => {
if (this.$route.name == 'manage-file') {
this.getFileList();
}
}, 5000)
}
}, },
methods: { methods: {

View File

@ -18,7 +18,7 @@ export default {
}, },
mounted() { mounted() {
this.project_id = this.$route.params.id; this.project_id = $A.runNum(this.$route.params.id);
}, },
deactivated() { deactivated() {
@ -26,38 +26,55 @@ export default {
}, },
computed: { computed: {
...mapState(['cacheProjects']), ...mapState(['cacheProjects', 'wsOpenNum']),
...mapGetters(['projectParameter']), ...mapGetters(['projectParameter']),
}, },
watch: { watch: {
'$route' (route) { '$route' ({params}) {
this.project_id = route.params.id; this.project_id = $A.runNum(params.id);
}, },
project_id(id) {
if (id > 0) { project_id() {
setTimeout(() => { this.getProjectData();
this.$store.state.projectId = $A.runNum(id); },
this.$store.dispatch("getProjectOne", id).then(() => {
this.$store.dispatch("getColumns", id); wsOpenNum(num) {
this.$store.dispatch("getTaskForProject", id); if (num <= 1) return
}).catch(({msg}) => { this.wsOpenTimeout && clearTimeout(this.wsOpenTimeout)
$A.modalWarning({ this.wsOpenTimeout = setTimeout(() => {
content: msg, if (this.$route.name == 'manage-project') {
onOk: () => { this.getProjectData();
const project = this.cacheProjects.find(({id}) => id); }
if (project) { }, 5000)
$A.goForward({path: '/manage/project/' + project.id});
} else {
$A.goForward({path: '/manage/dashboard'});
}
}
});
});
this.$store.dispatch("forgetTaskCompleteTemp", true);
});
}
} }
}, },
methods: {
getProjectData() {
let id = this.project_id;
if (id <= 0) return;
setTimeout(() => {
this.$store.state.projectId = $A.runNum(id);
this.$store.dispatch("getProjectOne", id).then(() => {
this.$store.dispatch("getColumns", id);
this.$store.dispatch("getTaskForProject", id);
}).catch(({msg}) => {
$A.modalWarning({
content: msg,
onOk: () => {
const project = this.cacheProjects.find(({id}) => id);
if (project) {
$A.goForward({path: '/manage/project/' + project.id});
} else {
$A.goForward({path: '/manage/dashboard'});
}
}
});
});
this.$store.dispatch("forgetTaskCompleteTemp", true);
});
}
}
} }
</script> </script>

View File

@ -167,10 +167,10 @@ export default {
}, },
/** /**
* 刷新基本数据项目对话仪表盘任务 * 获取基本数据项目对话仪表盘任务
* @param dispatch * @param dispatch
*/ */
refreshBasicData({dispatch}) { getBasicData({dispatch}) {
dispatch("getProjects"); dispatch("getProjects");
dispatch("getDialogs"); dispatch("getDialogs");
dispatch("getTaskForDashboard"); dispatch("getTaskForDashboard");
@ -212,7 +212,7 @@ export default {
state.userToken = userInfo.token; state.userToken = userInfo.token;
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("refreshBasicData"); dispatch("getBasicData");
dispatch("websocketConnection"); dispatch("websocketConnection");
resolve() resolve()
}); });
@ -1967,7 +1967,7 @@ export default {
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())
Store.set('websocketOpen', ++state.wsOpenNum); state.wsOpenNum++;
}; };
state.ws.onclose = (e) => { state.ws.onclose = (e) => {
console.log("[WS] Close", $A.formatDate()) console.log("[WS] Close", $A.formatDate())