修改 通知详情样式 和 即时通讯
This commit is contained in:
+44
-14
@@ -1,5 +1,6 @@
|
||||
import mittBus from './mittBus'
|
||||
import { useUserStore } from '@/store/modules/user'
|
||||
import { ElNotification } from 'element-plus'
|
||||
|
||||
type JsonValue = Record<string, any> | any[]
|
||||
|
||||
@@ -13,6 +14,10 @@ let reconnectTimer: any = null
|
||||
let heartbeatTimer: any = null
|
||||
let reconnectAttempts = 0
|
||||
let isInitialConnection = true
|
||||
let isConnecting = false
|
||||
let lastInitToken: string | null = null
|
||||
let lastInitAt = 0
|
||||
const INIT_DEBOUNCE_MS = 1200
|
||||
|
||||
// 生成唯一ID
|
||||
const generateTraceId = (): string => {
|
||||
@@ -108,6 +113,7 @@ const handleMessage = (event: MessageEvent) => {
|
||||
const setupWebSocketHandlers = (ws: WebSocket) => {
|
||||
ws.onopen = () => {
|
||||
isInitialConnection = false
|
||||
isConnecting = false
|
||||
startHeartbeat()
|
||||
mittBus.emit('ws:open')
|
||||
sendPing()
|
||||
@@ -117,10 +123,19 @@ const setupWebSocketHandlers = (ws: WebSocket) => {
|
||||
|
||||
ws.onerror = (err) => {
|
||||
mittBus.emit('ws:error', err)
|
||||
isConnecting = false
|
||||
try {
|
||||
if (ws.readyState === WebSocket.OPEN || ws.readyState === WebSocket.CONNECTING) {
|
||||
ws.close()
|
||||
}
|
||||
} catch {
|
||||
// 忽略关闭错误
|
||||
}
|
||||
}
|
||||
|
||||
ws.onclose = () => {
|
||||
mittBus.emit('ws:close')
|
||||
isConnecting = false
|
||||
if (reconnectAttempts < MAX_RECONNECT_ATTEMPTS) {
|
||||
scheduleReconnect()
|
||||
}
|
||||
@@ -129,11 +144,10 @@ const setupWebSocketHandlers = (ws: WebSocket) => {
|
||||
|
||||
// 安排重连
|
||||
const scheduleReconnect = () => {
|
||||
if (!isInitialConnection) {
|
||||
reconnectAttempts += 1
|
||||
}
|
||||
// 每次安排重连都递增(即使从未成功连接过)
|
||||
reconnectAttempts += 1
|
||||
|
||||
if (reconnectAttempts > MAX_RECONNECT_ATTEMPTS) {
|
||||
if (reconnectAttempts >= MAX_RECONNECT_ATTEMPTS) {
|
||||
console.warn(`WebSocket重连失败,已达到最大重连次数(${MAX_RECONNECT_ATTEMPTS}),停止重连`)
|
||||
mittBus.emit('ws:max-reconnect-reached')
|
||||
return
|
||||
@@ -144,7 +158,7 @@ const scheduleReconnect = () => {
|
||||
console.log(`WebSocket重连尝试 ${reconnectAttempts}/${MAX_RECONNECT_ATTEMPTS},延迟: ${delay}ms`)
|
||||
reconnectTimer = setTimeout(() => {
|
||||
const { accessToken } = useUserStore()
|
||||
if (accessToken) {
|
||||
if (accessToken && !isConnecting) {
|
||||
try {
|
||||
if (socket) {
|
||||
try {
|
||||
@@ -156,11 +170,13 @@ const scheduleReconnect = () => {
|
||||
}
|
||||
|
||||
const url = `${WS_BASE}${accessToken}`
|
||||
isConnecting = true
|
||||
socket = new WebSocket(url)
|
||||
setupWebSocketHandlers(socket)
|
||||
} catch (e) {
|
||||
mittBus.emit('ws:error', e)
|
||||
if (reconnectAttempts <= MAX_RECONNECT_ATTEMPTS) {
|
||||
isConnecting = false
|
||||
if (reconnectAttempts < MAX_RECONNECT_ATTEMPTS) {
|
||||
scheduleReconnect()
|
||||
}
|
||||
}
|
||||
@@ -172,23 +188,33 @@ const scheduleReconnect = () => {
|
||||
export const initWebSocket = (token: string) => {
|
||||
try {
|
||||
if (!token) return
|
||||
if (socket) {
|
||||
try {
|
||||
socket.close()
|
||||
} catch {
|
||||
// 忽略关闭错误
|
||||
const now = Date.now()
|
||||
// 简单防抖:在短时间内重复触发则跳过
|
||||
if (now - lastInitAt < INIT_DEBOUNCE_MS) {
|
||||
return
|
||||
}
|
||||
// 避免并发初始化:已在连接中或已连接且 token 未变化则跳过
|
||||
if (isConnecting) return
|
||||
if (socket && (socket.readyState === WebSocket.OPEN || socket.readyState === WebSocket.CONNECTING)) {
|
||||
if (lastInitToken === token) {
|
||||
return
|
||||
}
|
||||
// token 变更:需要切换连接
|
||||
try { socket.close() } catch {}
|
||||
socket = null
|
||||
}
|
||||
|
||||
cleanup()
|
||||
reconnectAttempts = 0
|
||||
isInitialConnection = true
|
||||
|
||||
const url = `${WS_BASE}${token}`
|
||||
isConnecting = true
|
||||
lastInitToken = token
|
||||
lastInitAt = now
|
||||
socket = new WebSocket(url)
|
||||
setupWebSocketHandlers(socket)
|
||||
} catch (e) {
|
||||
mittBus.emit('ws:error', e)
|
||||
isConnecting = false
|
||||
scheduleReconnect()
|
||||
}
|
||||
}
|
||||
@@ -204,6 +230,8 @@ export const closeWebSocket = () => {
|
||||
socket = null
|
||||
reconnectAttempts = 0
|
||||
isInitialConnection = true
|
||||
isConnecting = false
|
||||
lastInitToken = null
|
||||
}
|
||||
|
||||
// 发送WebSocket消息
|
||||
@@ -239,5 +267,7 @@ export const getWebSocketStatus = () => ({
|
||||
maxReconnectAttempts: MAX_RECONNECT_ATTEMPTS,
|
||||
isReconnecting: reconnectTimer !== null,
|
||||
isHeartbeating: heartbeatTimer !== null,
|
||||
isInitialConnection
|
||||
isInitialConnection,
|
||||
isConnecting,
|
||||
lastInitToken
|
||||
})
|
||||
Reference in New Issue
Block a user