修改 字符串
This commit is contained in:
@@ -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 }));
|
||||
|
||||
@@ -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 }));
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user