feat: 重新实现SseClient
This commit is contained in:
@@ -9,7 +9,7 @@ import { watch } from 'vue'
|
|||||||
import { RouterView } from 'vue-router'
|
import { RouterView } from 'vue-router'
|
||||||
import { ElConfigProvider } from 'element-plus'
|
import { ElConfigProvider } from 'element-plus'
|
||||||
import zhCn from 'element-plus/es/locale/lang/zh-cn'
|
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'
|
import { useAccountStore } from '@/stores/modules/account'
|
||||||
|
|
||||||
const accountStore = useAccountStore()
|
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 { ref } from 'vue'
|
||||||
import { SseClient } from '@/common/sse/SseClient'
|
import { SseClient } from './SseClient.ts'
|
||||||
import type { SseEvents } from '@/stores/sseBus'
|
import type { SseEvents } from './sseBus.ts'
|
||||||
import { sseBus } from '@/stores/sseBus'
|
import { sseBus } from './sseBus.ts'
|
||||||
import { useAccountStore } from '@/stores/modules/account'
|
import { useAccountStore } from '@/stores'
|
||||||
|
|
||||||
/** SSE 连接 URL */
|
/** SSE 连接 URL */
|
||||||
const SSE_URL = '/api/sse/subscribe'
|
const SSE_URL = '/api/sse/subscribe'
|
||||||
@@ -14,30 +14,26 @@ let isRefreshing = false
|
|||||||
|
|
||||||
const isConnected = ref(false)
|
const isConnected = ref(false)
|
||||||
|
|
||||||
const client = new SseClient(
|
const client = new SseClient(SSE_URL, () => useAccountStore().accessToken, {
|
||||||
SSE_URL,
|
onConnected() {
|
||||||
() => useAccountStore().accessToken,
|
isConnected.value = true
|
||||||
{
|
sseBus.emit('connected', undefined)
|
||||||
onConnected() {
|
},
|
||||||
isConnected.value = true
|
onMessage(event: string, data: string) {
|
||||||
sseBus.emit('connected', undefined)
|
try {
|
||||||
},
|
const payload = JSON.parse(data)
|
||||||
onMessage(event: string, data: string) {
|
sseBus.emit(event as keyof SseEvents, payload)
|
||||||
try {
|
} catch {
|
||||||
const payload = JSON.parse(data)
|
console.warn('[SSE] 事件数据解析失败:', event, 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()
|
|
||||||
}
|
}
|
||||||
|
},
|
||||||
|
onError(message: string) {
|
||||||
|
sseBus.emit('error', { message, retryCount: 0 })
|
||||||
|
},
|
||||||
|
onUnauthorized() {
|
||||||
|
void refreshTokenAndReconnect()
|
||||||
}
|
}
|
||||||
)
|
})
|
||||||
|
|
||||||
// ==================== Token 刷新 ====================
|
// ==================== Token 刷新 ====================
|
||||||
|
|
||||||
@@ -52,7 +52,7 @@ import type { RoomItem } from '@/api/modules/game'
|
|||||||
import { useBaseLayoutStore } from '@/stores'
|
import { useBaseLayoutStore } from '@/stores'
|
||||||
import { storeToRefs } from 'pinia'
|
import { storeToRefs } from 'pinia'
|
||||||
import { getRoomList, joinRoom } from '@/api/modules/game'
|
import { getRoomList, joinRoom } from '@/api/modules/game'
|
||||||
import { sseBus } from '@/stores/sseBus'
|
import { sseBus } from '@/common/sse'
|
||||||
import { ElMessage } from 'element-plus'
|
import { ElMessage } from 'element-plus'
|
||||||
|
|
||||||
/** 状态筛选选项 */
|
/** 状态筛选选项 */
|
||||||
|
|||||||
Reference in New Issue
Block a user