diff --git a/WebApp/client/public/onebyone/main.js b/WebApp/client/public/onebyone/main.js index 823a698..19e8512 100644 --- a/WebApp/client/public/onebyone/main.js +++ b/WebApp/client/public/onebyone/main.js @@ -15,25 +15,25 @@ let connectionId = ""; /** * 初始化应用 */ -function initApp() { - // 初始化渲染器 - renderer = new UIRenderer(store); +// function initApp() { +// // 初始化渲染器 +// renderer = new UIRenderer(store); - // 初始化WebSocket连接 - wsManager.connect(); +// // 初始化WebSocket连接 +// wsManager.connect(); - // 绑定WebSocket事件 - bindWebSocketEvents(); +// // 绑定WebSocket事件 +// bindWebSocketEvents(); - // 绑定DOM事件 - bindDomEvents(); +// // 绑定DOM事件 +// bindDomEvents(); - // 初始化WebRTC (如果需要) - // initWebRTC(); +// // 初始化WebRTC (如果需要) +// // initWebRTC(); - console.log('App initialized'); -} +// console.log('App initialized'); +// } /** * 绑定WebSocket事件 @@ -92,14 +92,22 @@ function bindWebSocketEvents() { console.log('Call request received:', data); // 显示通话请求弹窗 if (window.showCallRequest) { - const caller = { - name: mockCallSession.remoteUser.name, - avatar:mockCallSession.remoteUser.avatar - }; + const caller = { + name: mockCallSession.remoteUser.name, + avatar: mockCallSession.remoteUser.avatar + }; window.showCallRequest(caller); - connectionId =data.connectionId; + connectionId = data.connectionId; } }); + //处理发送消息响应 + wsManager.on('chat-message', (data) => { + console.log('chat-message:', data); + // 显示消息 + store.addMessage({ + data: data.message, + }); + }); } /** @@ -221,9 +229,12 @@ function bindDomEvents() { store.addMessage(newMessage); chatInput.value = ''; - + const message = { + connectionId: connectionId, + message: newMessage + }; // 发送消息到服务器 - // wsManager.send('send-message', newMessage); + wsManager.send('chat-message', message); } }; diff --git a/WebApp/client/public/onebyone/websocket.js b/WebApp/client/public/onebyone/websocket.js index cdf51fe..837a882 100644 --- a/WebApp/client/public/onebyone/websocket.js +++ b/WebApp/client/public/onebyone/websocket.js @@ -2,18 +2,11 @@ * WebSocket管理 * 管理WebSocket连接,处理WebSocket事件 */ +import { RenderStreaming } from "../../module/renderstreaming.js"; // WebRTC连接管理 class WebSocketManager { - constructor(url = null) { - this.url = url || this.getDefaultWebSocketUrl(); - this.socket = null; + constructor() { this.isConnected = false; - this.listeners = new Map(); - this.reconnectAttempts = 0; - this.maxReconnectAttempts = 5; - this.reconnectDelay = 1000; - this.connectionId = null; - this.heartbeatInterval = null; } /** @@ -28,36 +21,33 @@ class WebSocketManager { /** * 连接WebSocket */ - connect() { + init() { try { - this.socket = new WebSocket(this.url); - this.socket.onopen = () => { + RenderStreaming.onConnect = () => { console.log('WebSocket connected'); this.isConnected = true; - this.reconnectAttempts = 0; - // 生成连接ID - this.connectionId = this.generateConnectionId(); - // 发送连接消息 - this.sendConnectMessage(); - // 启动心跳 - this.startHeartbeat(); + // // 发送连接消息 + // this.sendConnectMessage(); - this.emit('connect'); + // // 启动心跳 + this.startHeartbeat(); + + // this.emit('connect'); }; - this.socket.onclose = () => { + RenderStreaming.onDisconnect = () => { console.log('WebSocket disconnected'); this.isConnected = false; this.stopHeartbeat(); - this.emit('disconnect'); - this.attemptReconnect(); + //this.emit('disconnect'); + //this.attemptReconnect(); }; - this.socket.onmessage = (event) => { + RenderStreaming.onmessage = (event) => { try { const message = JSON.parse(event.data); this.handleMessage(message); @@ -126,6 +116,7 @@ class WebSocketManager { */ handleMessage(message) { if (message.type) { + console.log('Received message:', message); switch (message.type) { case 'user-joined': this.emit('user-joined', message.data); @@ -165,6 +156,9 @@ class WebSocketManager { case 'candidate': this.emit('candidate', message.data); break; + case 'chat-message': + this.emit('chat-message', message.data); + break; default: // 处理旧格式消息 if (message.event) { diff --git a/WebApp/src/class/websockethandler.ts b/WebApp/src/class/websockethandler.ts index 5ffc5dc..c70029f 100644 --- a/WebApp/src/class/websockethandler.ts +++ b/WebApp/src/class/websockethandler.ts @@ -361,8 +361,26 @@ function onCandidate(ws: WebSocket, message: any): void { // })); return connectionIds; } - + /** + * 处理chat-message信令 + * @param ws WebSocket连接实例 + * @param message 消息数据 + */ + function onChatMessage(ws: WebSocket, message: any): void { + // 获取连接ID + const connectionId = message.connectionId; + const chatMessage = message.message; + if (connectionPair.has(connectionId)) { + const pair = connectionPair.get(connectionId); + // 找到另一个WebSocket实例 + const otherSessionWs = pair[0] == ws ? pair[1] : pair[0]; + if (otherSessionWs) { + // 发送chat-message消息 + otherSessionWs.send(JSON.stringify({ from: connectionId, to: "", type: "chat-message", data: chatMessage })); + } + } + } /** * 导出WebSocket处理器函数 */ - export { reset, add, remove, onConnect, onDisconnect, onOffer, onAnswer, onCandidate, onCallConnectionId, onBroadcast, onGetAllConnectionIds, AddHeartbeat, RemoveHeartbeat }; + export { reset, add, remove, onConnect, onDisconnect, onOffer, onAnswer, onCandidate, onCallConnectionId, onBroadcast, onGetAllConnectionIds, AddHeartbeat, RemoveHeartbeat ,onChatMessage}; diff --git a/WebApp/src/websocket.ts b/WebApp/src/websocket.ts index 89e2172..c92eab9 100644 --- a/WebApp/src/websocket.ts +++ b/WebApp/src/websocket.ts @@ -108,6 +108,10 @@ export default class WSSignaling { // 处理callConnectionId信令 handler.onCallConnectionId(ws, msg.data); break; + case 'chat-message'://接受连接ConnectionId + // 处理chat-message信令 + handler.onChatMessage(ws, msg.data); + break; default: // 忽略未知消息类型 break;