feat: 重新实现SseClient
This commit is contained in:
@@ -9,7 +9,7 @@ import { watch } from 'vue'
|
||||
import { RouterView } from 'vue-router'
|
||||
import { ElConfigProvider } from 'element-plus'
|
||||
import zhCn from 'element-plus/es/locale/lang/zh-cn'
|
||||
import { useSse } from '@/common/hooks/useSse'
|
||||
import { useSse } from '@/common/sse'
|
||||
import { useAccountStore } from '@/stores/modules/account'
|
||||
|
||||
const accountStore = useAccountStore()
|
||||
|
||||
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'
|
||||
@@ -1,8 +1,8 @@
|
||||
import { ref } from 'vue'
|
||||
import { SseClient } from '@/common/sse/SseClient'
|
||||
import type { SseEvents } from '@/stores/sseBus'
|
||||
import { sseBus } from '@/stores/sseBus'
|
||||
import { useAccountStore } from '@/stores/modules/account'
|
||||
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'
|
||||
@@ -14,30 +14,26 @@ 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()
|
||||
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 刷新 ====================
|
||||
|
||||
@@ -52,7 +52,7 @@ import type { RoomItem } from '@/api/modules/game'
|
||||
import { useBaseLayoutStore } from '@/stores'
|
||||
import { storeToRefs } from 'pinia'
|
||||
import { getRoomList, joinRoom } from '@/api/modules/game'
|
||||
import { sseBus } from '@/stores/sseBus'
|
||||
import { sseBus } from '@/common/sse'
|
||||
import { ElMessage } from 'element-plus'
|
||||
|
||||
/** 状态筛选选项 */
|
||||
|
||||
Reference in New Issue
Block a user