From 6f0b573335f68712f2feab85c664d8b4e372f143 Mon Sep 17 00:00:00 2001 From: stary <834207172@qq.com> Date: Tue, 12 May 2026 22:12:19 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=20=E5=AD=97=E7=AC=A6?= =?UTF-8?q?=E4=B8=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- client/src/signaling.js | 23 ++++++++++++++++++----- src/class/websockethandler.ts | 8 ++++---- 2 files changed, 22 insertions(+), 9 deletions(-) diff --git a/client/src/signaling.js b/client/src/signaling.js index f989113..88001e2 100644 --- a/client/src/signaling.js +++ b/client/src/signaling.js @@ -72,7 +72,15 @@ export class Signaling extends EventTarget { this.dispatchEvent(new CustomEvent('candidate', { detail: msg })); break; case "on-message": - this.dispatchEvent(new CustomEvent('on-message', { detail: msg.data })); + { + let parsed = msg.data; + if (typeof msg.data === 'string') { + try { parsed = JSON.parse(msg.data); } catch(e) { + Logger.error(`Signaling: on-message, error: ${e}`); + } + } + this.dispatchEvent(new CustomEvent('on-message', { detail: parsed })); + } break; default: break; @@ -191,11 +199,16 @@ export class WebSocketSignaling extends EventTarget { this.dispatchEvent(new CustomEvent('candidate', { detail: { connectionId: msg.from, candidate: msg.data.candidate, sdpMLineIndex: msg.data.sdpMLineIndex, sdpMid: msg.data.sdpMid, participantId: msg.participantId } })); break; case "on-message": - // 将participantId附加到消息数据中,以便Host识别消息发送者 - if (msg.participantId) { - msg.data.participantId = msg.participantId; + { + let parsed = msg.data; + if (typeof msg.data === 'string') { + try { parsed = JSON.parse(msg.data); } catch(e) {} + } + if (msg.participantId) { + parsed.participantId = msg.participantId; + } + this.dispatchEvent(new CustomEvent('on-message', { detail: parsed })); } - this.dispatchEvent(new CustomEvent('on-message', { detail: msg.data })); break; case "participant-left": this.dispatchEvent(new CustomEvent('participant-left', { detail: msg })); diff --git a/src/class/websockethandler.ts b/src/class/websockethandler.ts index 87470d8..cf52503 100644 --- a/src/class/websockethandler.ts +++ b/src/class/websockethandler.ts @@ -416,7 +416,7 @@ function AddHeartbeat(ws: WebSocket, connectionId: string) { onDisconnect(ws, connectionId); } else { // 发送ping消息 - ws.send(JSON.stringify({ from: connectionId, to: "", type: "on-message", data: { type: "ping"} })); + ws.send(JSON.stringify({ from: connectionId, to: "", type: "on-message", data: { type: "ping" }.toString() })); log(LogLevel.log, 'WebSocket connection heartbeat, lastActivity: ', (ws as any).lastActivity); } }, 3000); @@ -455,15 +455,15 @@ function onMessage(ws: WebSocket, message: any): void { if (group.host === ws) { // host发送消息,转发给所有participants group.participants.forEach(participantWs => { - participantWs.send(JSON.stringify({ from: connectionId, to: "", type: "on-message", data: chatMessage })); + participantWs.send(JSON.stringify({ from: connectionId, to: "", type: "on-message", data: JSON.stringify(chatMessage) })); }); } else { // participant发送消息,转发给host(附带participantId)和其他participants - group.host.send(JSON.stringify({ from: connectionId, to: "", type: "on-message", data: chatMessage, participantId: senderParticipantId })); + group.host.send(JSON.stringify({ from: connectionId, to: "", type: "on-message", data: JSON.stringify(chatMessage), participantId: senderParticipantId })); // 同时转发给其他participants(排除发送者自身) group.participants.forEach(participantWs => { if (participantWs !== ws) { - participantWs.send(JSON.stringify({ from: connectionId, to: "", type: "on-message", data: chatMessage, participantId: senderParticipantId })); + participantWs.send(JSON.stringify({ from: connectionId, to: "", type: "on-message", data: JSON.stringify(chatMessage), participantId: senderParticipantId })); } }); }