2026-06-22 13:46:36 +08:00
|
|
|
|
import { ref } from 'vue'
|
2026-06-23 10:13:53 +08:00
|
|
|
|
import { fetchEventSource } from '@microsoft/fetch-event-source'
|
2026-06-22 13:46:36 +08:00
|
|
|
|
import { sseBus } from '@/stores/sseBus'
|
|
|
|
|
|
import { useAccountStore } from '@/stores/modules/account'
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2026-06-23 10:13:53 +08:00
|
|
|
|
* SSE 连接管理 composable(基于 @microsoft/fetch-event-source)
|
2026-06-22 13:46:36 +08:00
|
|
|
|
*
|
2026-06-23 14:32:00 +08:00
|
|
|
|
* 设计原则:
|
|
|
|
|
|
* - 全局唯一:同一时间只有一条活跃的 SSE 连接
|
|
|
|
|
|
* - token 驱动:仅 accessToken 变更时才断开重连
|
|
|
|
|
|
* - 防重复:已连接(相同 token)时 connect() 直接跳过
|
|
|
|
|
|
* - 所有业务变更通过 sseBus 事件总线推送,各组件自行订阅
|
2026-06-22 13:46:36 +08:00
|
|
|
|
*
|
|
|
|
|
|
* 使用示例:
|
|
|
|
|
|
* ```ts
|
|
|
|
|
|
* const { isConnected, connect, disconnect } = useSse()
|
2026-06-23 14:32:00 +08:00
|
|
|
|
* watch(() => accountStore.accessToken, (v) => v ? connect() : disconnect())
|
2026-06-22 13:46:36 +08:00
|
|
|
|
* ```
|
|
|
|
|
|
*/
|
|
|
|
|
|
export function useSse() {
|
|
|
|
|
|
/** SSE 连接是否已建立 */
|
|
|
|
|
|
const isConnected = ref(false)
|
2026-06-23 10:13:53 +08:00
|
|
|
|
/** 用于取消 inflight 连接的控制器 */
|
|
|
|
|
|
let abortController: AbortController | null = null
|
2026-06-23 14:32:00 +08:00
|
|
|
|
/** 当前连接使用的 token(用于判断是否需要重连) */
|
|
|
|
|
|
let currentToken: string | null = null
|
|
|
|
|
|
/** 是否正在建立连接(防并发) */
|
|
|
|
|
|
let connecting = false
|
2026-06-23 10:13:53 +08:00
|
|
|
|
/** 当前连接的重试计数 */
|
|
|
|
|
|
let retryCount = 0
|
|
|
|
|
|
/** 是否正在刷新 token(防并发) */
|
|
|
|
|
|
let isRefreshing = false
|
2026-06-22 13:46:36 +08:00
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 建立 SSE 连接
|
|
|
|
|
|
*
|
2026-06-23 10:13:53 +08:00
|
|
|
|
* 通过 fetch-event-source 发起,设置 saToken 请求头鉴权。
|
|
|
|
|
|
* 连接期间持续接收服务端推送事件,直到 abort 或服务端关闭。
|
2026-06-23 14:32:00 +08:00
|
|
|
|
*
|
|
|
|
|
|
* 防重复:如果已用相同 token 连接或正在连接中,直接跳过。
|
2026-06-22 13:46:36 +08:00
|
|
|
|
*/
|
2026-06-23 10:13:53 +08:00
|
|
|
|
async function connect(): Promise<void> {
|
2026-06-22 13:46:36 +08:00
|
|
|
|
const accountStore = useAccountStore()
|
|
|
|
|
|
const token = accountStore.accessToken
|
|
|
|
|
|
if (!token) {
|
|
|
|
|
|
console.warn('[SSE] 无 accessToken,跳过连接')
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-06-23 14:32:00 +08:00
|
|
|
|
// 相同 token 已连接或正在连接中,跳过
|
|
|
|
|
|
if (currentToken === token && (isConnected.value || connecting)) {
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 取消旧连接
|
2026-06-23 10:13:53 +08:00
|
|
|
|
abort()
|
|
|
|
|
|
|
2026-06-23 14:32:00 +08:00
|
|
|
|
connecting = true
|
|
|
|
|
|
currentToken = token
|
2026-06-23 10:13:53 +08:00
|
|
|
|
abortController = new AbortController()
|
2026-06-22 13:46:36 +08:00
|
|
|
|
|
|
|
|
|
|
// Vite 代理: /api/sse/subscribe → 后端 /sse/subscribe
|
2026-06-23 10:13:53 +08:00
|
|
|
|
fetchEventSource('/api/sse/subscribe', {
|
|
|
|
|
|
headers: { saToken: token },
|
|
|
|
|
|
signal: abortController.signal,
|
2026-06-22 13:46:36 +08:00
|
|
|
|
|
2026-06-23 10:13:53 +08:00
|
|
|
|
/** 连接建立 */
|
|
|
|
|
|
async onopen() {
|
2026-06-23 14:32:00 +08:00
|
|
|
|
connecting = false
|
2026-06-23 10:13:53 +08:00
|
|
|
|
isConnected.value = true
|
|
|
|
|
|
retryCount = 0
|
|
|
|
|
|
console.log('[SSE] 连接已建立')
|
|
|
|
|
|
},
|
2026-06-22 13:46:36 +08:00
|
|
|
|
|
2026-06-23 10:13:53 +08:00
|
|
|
|
/** 收到服务端事件 */
|
|
|
|
|
|
onmessage(msg) {
|
|
|
|
|
|
// 心跳和 connected 确认消息可能 data 为空
|
2026-06-22 13:46:36 +08:00
|
|
|
|
if (!msg.data) return
|
|
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
const data = JSON.parse(msg.data)
|
|
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
2026-06-23 10:13:53 +08:00
|
|
|
|
sseBus.emit(msg.event as any, data)
|
|
|
|
|
|
} catch {
|
|
|
|
|
|
console.warn('[SSE] 事件数据解析失败:', msg.event, msg.data)
|
2026-06-22 13:46:36 +08:00
|
|
|
|
}
|
2026-06-23 10:13:53 +08:00
|
|
|
|
},
|
2026-06-22 13:46:36 +08:00
|
|
|
|
|
2026-06-23 10:13:53 +08:00
|
|
|
|
/** 连接关闭 */
|
|
|
|
|
|
onclose() {
|
2026-06-23 14:32:00 +08:00
|
|
|
|
connecting = false
|
2026-06-23 10:13:53 +08:00
|
|
|
|
isConnected.value = false
|
|
|
|
|
|
console.log('[SSE] 连接已关闭')
|
|
|
|
|
|
},
|
2026-06-22 13:46:36 +08:00
|
|
|
|
|
2026-06-23 10:13:53 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 连接错误处理(必须同步,签名: (err) => number | void)
|
|
|
|
|
|
*
|
|
|
|
|
|
* 401 → fire-and-forget 刷新 token,阻止当前连接重连,
|
|
|
|
|
|
* 刷新成功后由 Vue watcher 触发 connect() 自动重连
|
|
|
|
|
|
* 其他错误 → 返回重试延迟(ms),库自动排队重连
|
|
|
|
|
|
*/
|
|
|
|
|
|
onerror(err) {
|
2026-06-23 14:32:00 +08:00
|
|
|
|
connecting = false
|
2026-06-23 10:13:53 +08:00
|
|
|
|
isConnected.value = false
|
2026-06-22 13:46:36 +08:00
|
|
|
|
|
2026-06-23 10:13:53 +08:00
|
|
|
|
// 鉴权失败 → 刷新 token(异步 fire-and-forget,onerror 本身保持同步)
|
|
|
|
|
|
if (err.response?.status === 401) {
|
|
|
|
|
|
if (isRefreshing) {
|
|
|
|
|
|
// 已有刷新在进行中,直接放弃本次连接
|
|
|
|
|
|
throw err
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
isRefreshing = true
|
|
|
|
|
|
const store = useAccountStore()
|
|
|
|
|
|
|
|
|
|
|
|
if (!store.refreshToken) {
|
|
|
|
|
|
sseBus.emit('error', { message: '登录已过期,请重新登录', retryCount: 0 })
|
|
|
|
|
|
throw err
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 异步刷新,不阻塞 onerror 返回
|
|
|
|
|
|
store
|
|
|
|
|
|
.refreshAccessToken()
|
|
|
|
|
|
.then((newToken) => {
|
|
|
|
|
|
if (newToken) {
|
|
|
|
|
|
console.log('[SSE] token 刷新成功,重新建立连接')
|
|
|
|
|
|
retryCount = 0
|
|
|
|
|
|
// connect() 由 App.vue 的 watch 触发(accessToken 变更)
|
|
|
|
|
|
}
|
|
|
|
|
|
})
|
|
|
|
|
|
.catch(() => {
|
|
|
|
|
|
console.warn('[SSE] token 刷新失败')
|
|
|
|
|
|
sseBus.emit('error', { message: '实时连接异常,请刷新页面', retryCount: 0 })
|
|
|
|
|
|
})
|
|
|
|
|
|
.finally(() => {
|
|
|
|
|
|
isRefreshing = false
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
throw err // 不自动重连
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 其他错误:指数退避重连
|
|
|
|
|
|
retryCount++
|
|
|
|
|
|
if (retryCount >= 5) {
|
|
|
|
|
|
sseBus.emit('error', {
|
|
|
|
|
|
message: '实时连接异常,请刷新页面',
|
|
|
|
|
|
retryCount
|
|
|
|
|
|
})
|
|
|
|
|
|
throw err // 超过阈值,停止重连
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-06-23 14:32:00 +08:00
|
|
|
|
// 返回重试间隔(ms),指数退避:3s, 6s, 12s, 24s,最大 30s
|
2026-06-23 10:13:53 +08:00
|
|
|
|
const delay = Math.min(3000 * Math.pow(2, retryCount - 1), 30000)
|
|
|
|
|
|
console.warn(`[SSE] 连接错误,${delay / 1000}s 后重试 (${retryCount}/5)`)
|
|
|
|
|
|
return delay
|
2026-06-22 13:46:36 +08:00
|
|
|
|
}
|
2026-06-23 10:13:53 +08:00
|
|
|
|
})
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 取消当前 SSE 连接
|
|
|
|
|
|
*/
|
|
|
|
|
|
function abort(): void {
|
|
|
|
|
|
if (abortController) {
|
|
|
|
|
|
abortController.abort()
|
|
|
|
|
|
abortController = null
|
2026-06-22 13:46:36 +08:00
|
|
|
|
}
|
2026-06-23 14:32:00 +08:00
|
|
|
|
connecting = false
|
2026-06-23 10:13:53 +08:00
|
|
|
|
isConnected.value = false
|
2026-06-23 14:32:00 +08:00
|
|
|
|
currentToken = null
|
2026-06-23 10:13:53 +08:00
|
|
|
|
retryCount = 0
|
2026-06-22 13:46:36 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 断开 SSE 连接
|
|
|
|
|
|
*
|
|
|
|
|
|
* 在用户登出时调用。
|
|
|
|
|
|
*/
|
|
|
|
|
|
function disconnect(): void {
|
2026-06-23 10:13:53 +08:00
|
|
|
|
abort()
|
2026-06-22 13:46:36 +08:00
|
|
|
|
console.log('[SSE] 连接已断开')
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
|
isConnected,
|
|
|
|
|
|
connect,
|
|
|
|
|
|
disconnect
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|