refactor(messaging): replace WebSocket with SSE for real-time message handling
Some checks failed
Frontend CI / build-android-apk (push) Has been cancelled
Frontend CI / build-and-push-web (push) Has been cancelled
Frontend CI / ota-android (push) Has been cancelled
Frontend CI / ota-android (pull_request) Has been skipped
Frontend CI / build-and-push-web (pull_request) Has been cancelled
Frontend CI / build-android-apk (pull_request) Has been cancelled

Replace WebSocket-based real-time communication with Server-Sent Events (SSE) across the messaging infrastructure. This includes:

- Create new SSEClient datasource to manage SSE connections
- Create new SSEMessageHandler to process SSE events
- Update ProcessMessageUseCase to use SSEClient instead of WebSocketClient
- Update MessageManager and MessageStateManager to work with SSE handlers
- Rename connection state variables from `isWebSocketConnected` to `isSSEConnected`
- Update type definitions in dto.ts (WSEventType → SSEEventType, etc.)
- Delete obsolete WebSocketClient and WebSocketMessageHandler files
- Update comments and documentation to reflect SSE terminology

This refactoring aligns with the backend's SSE implementation for better compatibility with React Native's networking capabilities.
This commit is contained in:
2026-03-19 00:56:41 +08:00
parent 62b55aec31
commit a91637466c
11 changed files with 249 additions and 237 deletions

View File

@@ -4,7 +4,7 @@
*
* 功能:
* - 定期后台任务保持 App 在内存中
* - 配合 WebSocket 服务保持长连接
* - 配合 SSE 服务保持长连接
* - 收到消息时触发震动提示
*/
@@ -47,7 +47,7 @@ let appStateSubscription: any = null;
// 定义后台任务
TaskManager.defineTask(BACKGROUND_FETCH_TASK, async () => {
try {
// 检查 WebSocket 连接状态
// 检查 SSE 连接状态
if (!sseService.isConnected()) {
await sseService.connect();
}
@@ -60,7 +60,7 @@ TaskManager.defineTask(BACKGROUND_FETCH_TASK, async () => {
}
});
// WebSocket 保活任务
// SSE 保活任务
TaskManager.defineTask(REALTIME_KEEPALIVE_TASK, async () => {
try {
if (!sseService.isConnected()) {
@@ -174,9 +174,9 @@ async function registerBackgroundTasks(): Promise<void> {
});
}
// 注册 WebSocket 保活任务
const isWsKeepaliveRegistered = await TaskManager.isTaskRegisteredAsync(REALTIME_KEEPALIVE_TASK);
if (!isWsKeepaliveRegistered) {
// 注册 SSE 保活任务
const isSseKeepaliveRegistered = await TaskManager.isTaskRegisteredAsync(REALTIME_KEEPALIVE_TASK);
if (!isSseKeepaliveRegistered) {
await BackgroundFetch.registerTaskAsync(REALTIME_KEEPALIVE_TASK, {
minimumInterval: 60, // 1 分钟检查一次
stopOnTerminate: false,