refactor(WebSocket): migrate from SSE to WebSocket for real-time messaging

- Replaced SSEClient with WSClient for handling real-time messaging.
- Updated ProcessMessageUseCase to initialize WebSocket listeners.
- Refactored messageService to prioritize WebSocket for sending messages, with fallback to HTTP.
- Removed sseService and adjusted imports across the application to utilize wsService.
- Enhanced message handling and connection management for improved performance and reliability.
This commit is contained in:
lafay
2026-03-26 22:04:46 +08:00
parent 4b89b50006
commit ba99900624
16 changed files with 986 additions and 659 deletions

View File

@@ -11,7 +11,7 @@
import { AppState, AppStateStatus, Platform } from 'react-native';
import * as BackgroundFetch from 'expo-background-fetch';
import * as TaskManager from 'expo-task-manager';
import { sseService } from './sseService';
import { wsService } from './wsService';
import {
triggerVibration,
vibrateOnMessage,
@@ -36,8 +36,8 @@ let appStateSubscription: any = null;
TaskManager.defineTask(BACKGROUND_FETCH_TASK, async () => {
try {
// 检查 SSE 连接状态
if (!sseService.isConnected()) {
await sseService.connect();
if (!wsService.isConnected()) {
await wsService.connect();
}
// 返回收到新数据
@@ -51,8 +51,8 @@ TaskManager.defineTask(BACKGROUND_FETCH_TASK, async () => {
// SSE 保活任务
TaskManager.defineTask(REALTIME_KEEPALIVE_TASK, async () => {
try {
if (!sseService.isConnected()) {
await sseService.connect();
if (!wsService.isConnected()) {
await wsService.connect();
}
return BackgroundFetch.BackgroundFetchResult.NewData;
} catch (error) {
@@ -120,8 +120,8 @@ function setupAppStateListener(): void {
void nextAppState;
if (nextAppState === 'active') {
// App 回到前台,确保连接
if (!sseService.isConnected()) {
sseService.connect();
if (!wsService.isConnected()) {
wsService.connect();
}
}
});