websocket优化
This commit is contained in:
@@ -4,4 +4,4 @@ VITE_APP_TITLE = 智汇管理系统
|
||||
VITE_APP_ENV = 'development'
|
||||
|
||||
# 智汇管理系统/开发环境
|
||||
VITE_APP_BASE_API = 'http://192.168.1.5:8081'
|
||||
VITE_APP_BASE_API = 'http://192.168.1.6:8081'
|
||||
|
||||
@@ -32,6 +32,7 @@ function buildWsUrl() {
|
||||
}
|
||||
|
||||
export function connectWs(onMessage) {
|
||||
console.trace('connectWs called')
|
||||
if (!getToken()) {
|
||||
console.warn('[WebSocket] missing token')
|
||||
return
|
||||
@@ -73,10 +74,20 @@ export function connectWs(onMessage) {
|
||||
})
|
||||
}
|
||||
|
||||
ws.onclose = () => {
|
||||
console.log('[WebSocket] closed')
|
||||
ws.onclose = (event) => {
|
||||
console.log('[WebSocket] closed', { code: event.code, reason: event.reason })
|
||||
ws = null
|
||||
|
||||
if (isNonRetryableClose(event)) {
|
||||
console.warn('[WebSocket] stop reconnect because close reason is not retryable', {
|
||||
code: event.code,
|
||||
reason: event.reason
|
||||
})
|
||||
retryCount = 0
|
||||
clearReconnectTimer()
|
||||
return
|
||||
}
|
||||
|
||||
if (!manualClose && subscribers.size > 0) {
|
||||
reconnectTimer = setTimeout(() => {
|
||||
console.log('[WebSocket] reconnecting')
|
||||
@@ -153,3 +164,12 @@ function clearReconnectTimer() {
|
||||
reconnectTimer = null
|
||||
}
|
||||
}
|
||||
|
||||
function isNonRetryableClose(event) {
|
||||
const reason = (event?.reason || '').toLowerCase()
|
||||
return (
|
||||
reason.includes('invalid token') ||
|
||||
reason.includes('missing token') ||
|
||||
reason.includes('deptid is null')
|
||||
)
|
||||
}
|
||||
|
||||
@@ -184,6 +184,7 @@
|
||||
import { computed, onBeforeUnmount, onMounted, reactive, ref } from 'vue'
|
||||
import { useRouter } from 'vue-router'
|
||||
import { closeWs, connectWs } from '@/utils/ws'
|
||||
import { getToken } from '@/utils/auth'
|
||||
import {
|
||||
getHomeAlarmStat,
|
||||
getHomeAlarmTrend,
|
||||
@@ -752,6 +753,14 @@ function pushRealtimeAlarm(event) {
|
||||
|
||||
onMounted(() => {
|
||||
loadHomeData()
|
||||
|
||||
const token = getToken()
|
||||
|
||||
if (!token || token === 'undefined' || token === 'null' || token.length < 30) {
|
||||
console.warn('[Home] token无效,不连接WebSocket')
|
||||
return
|
||||
}
|
||||
|
||||
connectWs(handleWsMessage)
|
||||
})
|
||||
|
||||
|
||||
Reference in New Issue
Block a user