【m】聊天相关逻辑迁移

This commit is contained in:
zhangzheng
2026-03-12 14:41:00 +08:00
parent 873ce3d940
commit 80ff58ba15
5 changed files with 296 additions and 283 deletions

View File

@@ -2,18 +2,16 @@
* 状态管理
* 使用简单的 Observable 模式,可替换为 Redux/Vuex/Pinia
*/
import { mockCallSession, mockMessages } from './models.js';
import { mockCallSession } from './models.js';
import { Signaling, WebSocketSignaling } from "../../module/signaling.js";// 信令管理
import { RenderStreaming } from "../../module/renderstreaming.js"; // WebRTC连接管理
import { getServerConfig, getRTCConfiguration } from "../js/config.js";//服务器配置和RTC配置
import { showNotification, generateId } from './utils.js'; // 导入通知函数
import chatMessage from './chatmessage.js';
// 默认视频流尺寸
const defaultStreamWidth = 1280;
const defaultStreamHeight = 720;
class CallStateManager {
constructor() {
const renderstreaming = null; // WebRTC连接管理实例
@@ -26,9 +24,6 @@ class CallStateManager {
...mockCallSession,
status: 'idle' // 初始状态为空闲
},
messages: [...mockMessages],
isSidebarOpen: false,
unreadCount: 0,
localStream: null, // MediaStream 对象
remoteStream: null // MediaStream 对象
};
@@ -372,7 +367,7 @@ class CallStateManager {
// 处理聊天
// 添加到列表并更新UI
this.handleChatMessage(data.message);
chatMessage.handleChatMessage(data.message);
} else if (data.type === 'on-message') {
}
@@ -457,34 +452,19 @@ class CallStateManager {
this.notify({ type: 'REMOTE_MEDIA_CHANGE', mediaState });
}
// 添加
// 添加消息
addMessage(message) {
this.state.messages.push(message);
// 如果侧边栏关闭且不是自己发的,增加未读
if (!this.state.isSidebarOpen && !message.isSelf) {
this.state.unreadCount++;
this.notify({ type: 'SIDEBAR_TOGGLE', unreadCount: this.state.unreadCount });
}
this.notify({ type: 'NEW_MESSAGE', message, unreadCount: this.state.unreadCount });
chatMessage.addMessage(message);
}
// 发送聊天消息
sendChatMessage(message) {
if (this.renderstreaming) {
this.renderstreaming.sendMessage({
type: 'chat-message',
message: message,
});
}
chatMessage.sendChatMessage(message, this.renderstreaming);
}
// 切换侧边栏
toggleSidebar() {
this.state.isSidebarOpen = !this.state.isSidebarOpen;
if (this.state.isSidebarOpen) {
this.state.unreadCount = 0;
}
this.notify({ type: 'SIDEBAR_TOGGLE', isOpen: this.state.isSidebarOpen });
chatMessage.toggleSidebar();
}
// 结束通话
@@ -812,18 +792,8 @@ class CallStateManager {
}
}
// 处理接收到的聊天
handleChatMessage(data) {
console.log('处理聊天:', data);
store.addMessage(data);
const isImage = data.message && data.message.startsWith('data:image/');
const messageType = isImage ? 'file' : 'text';
// 显示通知
showNotification(`${senderName}: ${data.message.substring(0, 20)}${data.message.length > 20 ? '...' : ''}`);
}
// 发送媒体状态到服务器
emitMediaStateChange() {
const payload = {
@@ -939,8 +909,10 @@ class CallStateManager {
getState() { return this.state; }
getLocalUser() { return this.state.session.localUser; }
getRemoteUser() { return this.state.session.remoteUser; }
getMessages() { return this.state.messages; }
getConnectionId() { return this.connectionId; }
getRenderStreaming() { return this.renderstreaming; }
}
// 创建单例实例