diff --git a/src/views/main-navbar.vue b/src/views/main-navbar.vue index 7db7c7ce827add4bfa4de158bd8b551d2c31ad17..c4997f8a99cca6b0dd1afd74d222cd74ab2a00d2 100644 --- a/src/views/main-navbar.vue +++ b/src/views/main-navbar.vue @@ -119,17 +119,13 @@ }, destroyed () { - this.$store.commit('user/updateWebSocketConnectState', 0) + }, mounted () { - let value = this.$store.state.user.webSocketConnectState - // webSocket 连接不存在或者关闭状态 - if (!value || parseInt(value) === 0) { - setTimeout(() => { - this.initWebSocket() - }, 2000) - } + setTimeout(() => { + this.initWebSocket() + }, 2000) this.userInfo = JSON.parse(localStorage.getItem('userInfo')) }, @@ -153,6 +149,9 @@ methods: { initWebSocket () { + if (this.webSocket != null && this.webSocket.readyState === 1) { // websocket 已连接 + return false + } if ('WebSocket' in window) { let isHttps = document.location.protocol === 'https:' let hostName = this.$http.getWebSocketHost() @@ -180,7 +179,6 @@ }, onOpen () { - this.$store.commit('user/updateWebSocketConnectState', 1) let message = { userId: this.userInfo.id, token: localStorage.getItem('Authorization'), @@ -207,7 +205,7 @@ }, onError () { - this.$store.commit('user/updateWebSocketConnectState', 0) + this.webSocket = null }, // 接收消息 @@ -228,10 +226,10 @@ }, onClose (event) { // 关闭 - this.$store.commit('user/updateWebSocketConnectState', 0) + this.webSocket = null // webSocket 后台服务断开,每隔5秒尝试重连一次 this.webSocketTimer = setInterval(() => { - if (this.webSocketConnectSuccess) { + if (this.webSocket != null) { clearInterval(this.webSocketTimer) } else { this.tryConnection() @@ -242,7 +240,7 @@ /** * 尝试重新连接 */ - tryConnection() { + tryConnection () { this.initWebSocket() }, @@ -294,7 +292,6 @@ }) }, - logoutCommon (that) { that.axios.post(that.$http.httpUrl('/system/logout')).then(function (response) { if (response.data.code === 1) {