Migrate frontend realtime messaging to SSE.

Switch service integrations and screen/store consumers from websocket events to SSE, and ignore generated dist-web artifacts.

Made-with: Cursor
This commit is contained in:
2026-03-10 12:58:23 +08:00
parent 63e32b15a3
commit be84c01abd
25 changed files with 974 additions and 1305 deletions

View File

@@ -13,7 +13,8 @@
import AsyncStorage from '@react-native-async-storage/async-storage';
import { create } from 'zustand';
import { User } from '../types';
import { authService, resolveAuthApiError, websocketService, LoginRequest, RegisterRequest } from '../services';
import { authService, resolveAuthApiError, LoginRequest, RegisterRequest } from '../services';
import { sseService } from '../services/sseService';
import {
initDatabase,
closeDatabase,
@@ -91,12 +92,12 @@ function resolveLoginError(error: any): string {
return '登录失败,请稍后重试';
}
// ── 启动 WebSocket 服务 ──
async function startWebSocket(): Promise<void> {
// ── 启动 SSE 实时服务 ──
async function startRealtime(): Promise<void> {
try {
await websocketService.start();
await sseService.start();
} catch (error) {
console.error('[AuthStore] 启动 WebSocket 服务失败:', error);
console.error('[AuthStore] 启动 SSE 服务失败:', error);
}
}
@@ -153,8 +154,8 @@ export const useAuthStore = create<AuthState>((set) => ({
error: null,
});
// 5. 启动 WebSocket
await startWebSocket();
// 5. 启动 SSE
await startRealtime();
return true;
} catch (error: any) {
@@ -191,7 +192,7 @@ export const useAuthStore = create<AuthState>((set) => ({
error: null,
});
await startWebSocket();
await startRealtime();
return true;
} catch (error: any) {
@@ -210,8 +211,8 @@ export const useAuthStore = create<AuthState>((set) => ({
try {
// 1. 通知服务端Token 清理在 authService 内部完成)
await authService.logout();
// 2. 停止 WebSocket
websocketService.stop();
// 2. 停止 SSE
sseService.stop();
// 3. 清除 DB 中的用户缓存DB 此时一定已初始化)
await clearCurrentUserCache().catch(() => {});
// 4. 关闭数据库连接
@@ -270,8 +271,8 @@ export const useAuthStore = create<AuthState>((set) => ({
isLoading: false,
});
// 6. 启动 WebSocket
await startWebSocket();
// 6. 启动 SSE
await startRealtime();
} else {
// Token 已失效或不存在
await clearUserId();