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

@@ -8,7 +8,7 @@
*
* 架构特点:
* - 管理conversations和messages状态
* - 统一处理WebSocket消息
* - 统一处理SSE消息
* - 统一处理本地数据库读写
* - 提供订阅机制供React组件使用
*/
@@ -83,7 +83,7 @@ interface MessageManagerState {
systemUnreadCount: number;
// 连接状态
isWebSocketConnected: boolean;
isSSEConnected: boolean;
// 当前活动会话ID用户正在查看的会话
currentConversationId: string | null;
@@ -131,7 +131,7 @@ interface ReadStateRecord {
class MessageManager {
private state: MessageManagerState;
private wsUnsubscribe: (() => void) | null = null;
private sseUnsubscribe: (() => void) | null = null;
private currentUserId: string | null = null;
private authUnsubscribe: (() => void) | null = null;
private lastReconnectSyncAt: number = 0;
@@ -158,7 +158,7 @@ class MessageManager {
messagesMap: new Map(),
totalUnreadCount: 0,
systemUnreadCount: 0,
isWebSocketConnected: false,
isSSEConnected: false,
currentConversationId: null,
isLoadingConversations: false,
loadingMessagesSet: new Set(),
@@ -281,8 +281,8 @@ class MessageManager {
return;
}
// 2. 初始化WebSocket监听
this.initWebSocketListeners();
// 2. 初始化SSE监听
this.initSSEListeners();
// 3. 加载会话列表
await this.fetchConversations();
@@ -320,9 +320,9 @@ class MessageManager {
}
destroy(): void {
if (this.wsUnsubscribe) {
this.wsUnsubscribe();
this.wsUnsubscribe = null;
if (this.sseUnsubscribe) {
this.sseUnsubscribe();
this.sseUnsubscribe = null;
}
this.state.subscribers.clear();
this.state.isInitialized = false;
@@ -330,11 +330,11 @@ class MessageManager {
this.activatingConversationTasks.clear();
}
// ==================== WebSocket 处理 ====================
// ==================== SSE 处理 ====================
private initWebSocketListeners(): void {
if (this.wsUnsubscribe) {
this.wsUnsubscribe();
private initSSEListeners(): void {
if (this.sseUnsubscribe) {
this.sseUnsubscribe();
}
@@ -380,7 +380,7 @@ class MessageManager {
// 监听连接状态
sseService.onConnect(() => {
this.state.isWebSocketConnected = true;
this.state.isSSEConnected = true;
this.notifySubscribers({
type: 'connection_changed',
payload: { connected: true },
@@ -403,7 +403,7 @@ class MessageManager {
});
sseService.onDisconnect(() => {
this.state.isWebSocketConnected = false;
this.state.isSSEConnected = false;
this.notifySubscribers({
type: 'connection_changed',
payload: { connected: false },
@@ -585,7 +585,7 @@ class MessageManager {
}
/**
* 处理新消息(WebSocket推送)
* 处理新消息(SSE推送)
* 这是核心方法,必须立即处理并同步到所有订阅者
*/
private async handleNewMessage(message: (WSChatMessage | WSGroupChatMessage) & { _isAck?: boolean }): Promise<void> {
@@ -2014,10 +2014,10 @@ class MessageManager {
}
/**
* 获取WebSocket连接状态
* 获取SSE连接状态
*/
isConnected(): boolean {
return this.state.isWebSocketConnected;
return this.state.isSSEConnected;
}
/**
@@ -2122,7 +2122,7 @@ class MessageManager {
subscriber({
type: 'connection_changed',
payload: { connected: this.state.isWebSocketConnected },
payload: { connected: this.state.isSSEConnected },
timestamp: Date.now(),
});