websocket优化
This commit is contained in:
@@ -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')
|
||||
)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user