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:
@@ -5,6 +5,7 @@
|
||||
*/
|
||||
|
||||
import { api } from './api';
|
||||
import { wsService } from './wsService';
|
||||
import {
|
||||
ConversationListResponse,
|
||||
ConversationResponse,
|
||||
@@ -344,6 +345,7 @@ class MessageService {
|
||||
|
||||
/**
|
||||
* 发送消息(新格式)
|
||||
* 优先使用WebSocket发送,失败时降级到HTTP
|
||||
* POST /api/v1/conversations/:id/messages
|
||||
* Body: { detail_type, segments }
|
||||
*/
|
||||
@@ -357,12 +359,25 @@ class MessageService {
|
||||
throw new Error('消息内容不能为空');
|
||||
}
|
||||
|
||||
return this.sendMessageByAction(
|
||||
detailType,
|
||||
conversationId,
|
||||
data.segments,
|
||||
data.reply_to_id
|
||||
);
|
||||
// 优先使用WebSocket发送
|
||||
try {
|
||||
const result = await wsService.sendMessage(
|
||||
conversationId,
|
||||
detailType,
|
||||
data.segments,
|
||||
data.reply_to_id
|
||||
);
|
||||
return result;
|
||||
} catch (error) {
|
||||
console.log('WebSocket发送失败,降级到HTTP:', error);
|
||||
// 降级到HTTP发送
|
||||
return this.sendMessageByAction(
|
||||
detailType,
|
||||
conversationId,
|
||||
data.segments,
|
||||
data.reply_to_id
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -494,13 +509,19 @@ class MessageService {
|
||||
|
||||
/**
|
||||
* 撤回/删除消息
|
||||
* 优先使用WebSocket,失败时降级到HTTP
|
||||
* POST /api/v1/messages/delete_msg
|
||||
* Body: { "message_id": "xxx" }
|
||||
*/
|
||||
async recallMessage(messageId: string): Promise<void> {
|
||||
await api.post('/messages/delete_msg', {
|
||||
message_id: messageId,
|
||||
});
|
||||
try {
|
||||
await wsService.recallMessage(messageId);
|
||||
} catch (error) {
|
||||
console.log('WebSocket撤回失败,降级到HTTP:', error);
|
||||
await api.post('/messages/delete_msg', {
|
||||
message_id: messageId,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -515,11 +536,16 @@ class MessageService {
|
||||
|
||||
/**
|
||||
* 标记已读
|
||||
* 优先使用WebSocket,失败时降级到HTTP
|
||||
* POST /api/v1/conversations/:id/read
|
||||
* @param conversationId 会话ID
|
||||
* @param seq 已读到的消息序号
|
||||
*/
|
||||
async markAsRead(conversationId: string, seq: number): Promise<void> {
|
||||
// 使用WebSocket发送已读标记(不等待响应)
|
||||
wsService.markRead(conversationId, seq).catch(() => {});
|
||||
|
||||
// 同时通过HTTP确保可靠性
|
||||
await api.post(`/conversations/${encodeURIComponent(conversationId)}/read`, {
|
||||
last_read_seq: Number(seq),
|
||||
});
|
||||
@@ -531,9 +557,14 @@ class MessageService {
|
||||
|
||||
/**
|
||||
* 上报输入状态
|
||||
* 优先使用WebSocket,失败时降级到HTTP
|
||||
* POST /api/v1/conversations/:id/typing
|
||||
*/
|
||||
async sendTyping(conversationId: string): Promise<void> {
|
||||
// 使用WebSocket发送输入状态
|
||||
wsService.sendTyping(conversationId, true);
|
||||
|
||||
// 同时通过HTTP确保可靠性
|
||||
await api.post(`/conversations/${encodeURIComponent(conversationId)}/typing`);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user