feat: 重新实现SseClient
This commit is contained in:
2
src/common/sse/index.ts
Normal file
2
src/common/sse/index.ts
Normal file
@@ -0,0 +1,2 @@
|
||||
export * from './useSse.ts'
|
||||
export * from './sseBus.ts'
|
||||
25
src/common/sse/sseBus.ts
Normal file
25
src/common/sse/sseBus.ts
Normal file
@@ -0,0 +1,25 @@
|
||||
import mitt from 'mitt'
|
||||
import type { RoomItem } from '@/api/modules/game'
|
||||
|
||||
export type SseEvents = {
|
||||
connected: undefined
|
||||
error: { message: string; retryCount: number }
|
||||
room_created: RoomItem
|
||||
room_updated: RoomItem
|
||||
room_removed: { roomId: string }
|
||||
kicked: { roomId: string }
|
||||
game_started: { roomId: string }
|
||||
balance_chg: { balance: number; delta: number }
|
||||
}
|
||||
|
||||
/**
|
||||
* 全局 SSE 事件总线(发布-订阅模式)
|
||||
*
|
||||
* 连接层只负责解析 SSE 消息并 emit 到总线,业务模块自行订阅处理。
|
||||
* 示例:
|
||||
* ```ts
|
||||
* import { sseBus } from '@/stores/sseBus'
|
||||
* sseBus.on('room_updated', (room) => { ... })
|
||||
* ```
|
||||
*/
|
||||
export const sseBus = mitt<SseEvents>()
|
||||
79
src/common/sse/useSse.ts
Normal file
79
src/common/sse/useSse.ts
Normal file
@@ -0,0 +1,79 @@
|
||||
import { ref } from 'vue'
|
||||
import { SseClient } from './SseClient.ts'
|
||||
import type { SseEvents } from './sseBus.ts'
|
||||
import { sseBus } from './sseBus.ts'
|
||||
import { useAccountStore } from '@/stores'
|
||||
|
||||
/** SSE 连接 URL */
|
||||
const SSE_URL = '/api/sse/subscribe'
|
||||
|
||||
/** 是否正在刷新 token(防并发) */
|
||||
let isRefreshing = false
|
||||
|
||||
// ==================== 模块级单例 ====================
|
||||
|
||||
const isConnected = ref(false)
|
||||
|
||||
const client = new SseClient(SSE_URL, () => useAccountStore().accessToken, {
|
||||
onConnected() {
|
||||
isConnected.value = true
|
||||
sseBus.emit('connected', undefined)
|
||||
},
|
||||
onMessage(event: string, data: string) {
|
||||
try {
|
||||
const payload = JSON.parse(data)
|
||||
sseBus.emit(event as keyof SseEvents, payload)
|
||||
} catch {
|
||||
console.warn('[SSE] 事件数据解析失败:', event, data)
|
||||
}
|
||||
},
|
||||
onError(message: string) {
|
||||
sseBus.emit('error', { message, retryCount: 0 })
|
||||
},
|
||||
onUnauthorized() {
|
||||
void refreshTokenAndReconnect()
|
||||
}
|
||||
})
|
||||
|
||||
// ==================== Token 刷新 ====================
|
||||
|
||||
async function refreshTokenAndReconnect(): Promise<void> {
|
||||
if (isRefreshing) return
|
||||
|
||||
const accountStore = useAccountStore()
|
||||
if (!accountStore.refreshToken) {
|
||||
sseBus.emit('error', { message: '登录已过期,请重新登录', retryCount: 0 })
|
||||
return
|
||||
}
|
||||
|
||||
isRefreshing = true
|
||||
try {
|
||||
const newToken = await accountStore.refreshAccessToken()
|
||||
if (newToken) {
|
||||
await client.connect()
|
||||
}
|
||||
} catch {
|
||||
sseBus.emit('error', { message: '实时连接异常,请刷新页面', retryCount: 0 })
|
||||
} finally {
|
||||
isRefreshing = false
|
||||
}
|
||||
}
|
||||
|
||||
// ==================== Composable ====================
|
||||
|
||||
/**
|
||||
* 全局 SSE 连接管理
|
||||
*
|
||||
* SseClient 只管连接生命周期,业务消息通过 sseBus 分发。
|
||||
* 组件只需 `useSse().connect()` / `useSse().disconnect()`。
|
||||
*/
|
||||
export function useSse() {
|
||||
return {
|
||||
isConnected,
|
||||
connect: () => client.connect(),
|
||||
disconnect: () => {
|
||||
client.disconnect()
|
||||
isConnected.value = false
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user