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
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:
@@ -1,6 +1,6 @@
|
||||
/**
|
||||
* WebSocketClient - WebSocket连接管理
|
||||
* 只负责WebSocket连接管理,提供事件驱动架构
|
||||
* SSEClient - SSE连接管理
|
||||
* 只负责SSE连接管理,提供事件驱动架构
|
||||
*/
|
||||
|
||||
import {
|
||||
@@ -19,8 +19,8 @@ import {
|
||||
// 事件处理器类型
|
||||
type EventHandler<T> = (data: T) => void;
|
||||
|
||||
// WebSocket事件类型
|
||||
export interface WebSocketEvents {
|
||||
// SSE事件类型
|
||||
export interface SSEEvents {
|
||||
'chat': WSChatMessage;
|
||||
'group_message': WSGroupChatMessage;
|
||||
'read': WSReadMessage;
|
||||
@@ -34,15 +34,15 @@ export interface WebSocketEvents {
|
||||
'error': Error;
|
||||
}
|
||||
|
||||
export type WebSocketEventType = keyof WebSocketEvents;
|
||||
export type SSEEventType = keyof SSEEvents;
|
||||
|
||||
class WebSocketClient {
|
||||
private handlers: Map<WebSocketEventType, Set<EventHandler<any>>> = new Map();
|
||||
class SSEClient {
|
||||
private handlers: Map<SSEEventType, Set<EventHandler<any>>> = new Map();
|
||||
private unsubscribeFns: Array<() => void> = [];
|
||||
private isInitialized = false;
|
||||
|
||||
/**
|
||||
* 初始化WebSocket监听
|
||||
* 初始化SSE监听
|
||||
*/
|
||||
initialize(): void {
|
||||
if (this.isInitialized) return;
|
||||
@@ -113,7 +113,7 @@ class WebSocketClient {
|
||||
}
|
||||
|
||||
/**
|
||||
* 销毁WebSocket监听
|
||||
* 销毁SSE监听
|
||||
*/
|
||||
destroy(): void {
|
||||
this.unsubscribeFns.forEach((fn) => fn());
|
||||
@@ -125,9 +125,9 @@ class WebSocketClient {
|
||||
/**
|
||||
* 订阅事件
|
||||
*/
|
||||
on<T extends WebSocketEventType>(
|
||||
on<T extends SSEEventType>(
|
||||
event: T,
|
||||
handler: EventHandler<WebSocketEvents[T]>
|
||||
handler: EventHandler<SSEEvents[T]>
|
||||
): () => void {
|
||||
if (!this.handlers.has(event)) {
|
||||
this.handlers.set(event, new Set());
|
||||
@@ -142,9 +142,9 @@ class WebSocketClient {
|
||||
/**
|
||||
* 触发事件
|
||||
*/
|
||||
private emit<T extends WebSocketEventType>(
|
||||
private emit<T extends SSEEventType>(
|
||||
event: T,
|
||||
data: WebSocketEvents[T]
|
||||
data: SSEEvents[T]
|
||||
): void {
|
||||
const handlers = this.handlers.get(event);
|
||||
if (handlers) {
|
||||
@@ -152,7 +152,7 @@ class WebSocketClient {
|
||||
try {
|
||||
handler(data);
|
||||
} catch (error) {
|
||||
console.error(`[WebSocketClient] 事件处理器执行失败: ${event}`, error);
|
||||
console.error(`[SSEClient] 事件处理器执行失败: ${event}`, error);
|
||||
}
|
||||
});
|
||||
}
|
||||
@@ -166,19 +166,19 @@ class WebSocketClient {
|
||||
}
|
||||
|
||||
/**
|
||||
* 启动WebSocket连接
|
||||
* 启动SSE连接
|
||||
*/
|
||||
async connect(): Promise<boolean> {
|
||||
return sseService.start();
|
||||
}
|
||||
|
||||
/**
|
||||
* 断开WebSocket连接
|
||||
* 断开SSE连接
|
||||
*/
|
||||
disconnect(): void {
|
||||
sseService.stop();
|
||||
}
|
||||
}
|
||||
|
||||
export const webSocketClient = new WebSocketClient();
|
||||
export default webSocketClient;
|
||||
export const sseClient = new SSEClient();
|
||||
export default sseClient;
|
||||
Reference in New Issue
Block a user