【m】聊天消息接入

This commit is contained in:
zhangzheng
2026-03-12 12:10:40 +08:00
parent b4c41e42c9
commit e9f7b3ad5c
7 changed files with 425 additions and 359 deletions

View File

@@ -6,13 +6,14 @@ import { mockCallSession, mockMessages } 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 } from './utils.js'; // 导入通知函数
import { showNotification, generateId } from './utils.js'; // 导入通知函数
// 默认视频流尺寸
const defaultStreamWidth = 1280;
const defaultStreamHeight = 720;
class CallStateManager {
constructor() {
const renderstreaming = null; // WebRTC连接管理实例
@@ -20,6 +21,7 @@ class CallStateManager {
const connectionId = null; // 连接ID
// 核心状态
this.state = {
id: generateId(),
session: {
...mockCallSession,
status: 'idle' // 初始状态为空闲
@@ -255,6 +257,7 @@ class CallStateManager {
}
/**
* 设置WebRTC连接
* @async
@@ -362,7 +365,17 @@ class CallStateManager {
}
}
};
// 初始化 RenderStreaming 实例后
this.renderstreaming.onMessage = (data) => {
console.log('收到聊天消息:', data);
if (data.type === 'chat-message') {
// 处理聊天消息
// 添加到消息列表并更新UI
store.addMessage(data.message);
} else if (data.type === 'on-message') {
}
};
// 启动WebRTC连接
await this.renderstreaming.start();
await this.renderstreaming.createConnection(connectionId);
@@ -454,7 +467,15 @@ class CallStateManager {
this.notify({ type: 'NEW_MESSAGE', message, unreadCount: this.state.unreadCount });
}
sendChatMessage(message) {
if (this.renderstreaming) {
this.renderstreaming.sendMessage({
type: 'chat-message',
message: message,
});
}
}
// 切换侧边栏
toggleSidebar() {
this.state.isSidebarOpen = !this.state.isSidebarOpen;
@@ -595,7 +616,7 @@ class CallStateManager {
console.error('Error detecting network quality:', error);
}
}
// 真实音频活动检测 - 远端
// 真实音频活动检测 - 远端
startRemoteActivityDetection() {
// 检查是否有远端音频流
if (!this.state.remoteStream) {