修改 字符串
This commit is contained in:
@@ -72,7 +72,15 @@ export class Signaling extends EventTarget {
|
|||||||
this.dispatchEvent(new CustomEvent('candidate', { detail: msg }));
|
this.dispatchEvent(new CustomEvent('candidate', { detail: msg }));
|
||||||
break;
|
break;
|
||||||
case "on-message":
|
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;
|
break;
|
||||||
default:
|
default:
|
||||||
break;
|
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 } }));
|
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;
|
break;
|
||||||
case "on-message":
|
case "on-message":
|
||||||
// 将participantId附加到消息数据中,以便Host识别消息发送者
|
{
|
||||||
if (msg.participantId) {
|
let parsed = msg.data;
|
||||||
msg.data.participantId = msg.participantId;
|
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;
|
break;
|
||||||
case "participant-left":
|
case "participant-left":
|
||||||
this.dispatchEvent(new CustomEvent('participant-left', { detail: msg }));
|
this.dispatchEvent(new CustomEvent('participant-left', { detail: msg }));
|
||||||
|
|||||||
@@ -416,7 +416,7 @@ function AddHeartbeat(ws: WebSocket, connectionId: string) {
|
|||||||
onDisconnect(ws, connectionId);
|
onDisconnect(ws, connectionId);
|
||||||
} else {
|
} else {
|
||||||
// 发送ping消息
|
// 发送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);
|
log(LogLevel.log, 'WebSocket connection heartbeat, lastActivity: ', (ws as any).lastActivity);
|
||||||
}
|
}
|
||||||
}, 3000);
|
}, 3000);
|
||||||
@@ -455,15 +455,15 @@ function onMessage(ws: WebSocket, message: any): void {
|
|||||||
if (group.host === ws) {
|
if (group.host === ws) {
|
||||||
// host发送消息,转发给所有participants
|
// host发送消息,转发给所有participants
|
||||||
group.participants.forEach(participantWs => {
|
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 {
|
} else {
|
||||||
// participant发送消息,转发给host(附带participantId)和其他participants
|
// 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(排除发送者自身)
|
// 同时转发给其他participants(排除发送者自身)
|
||||||
group.participants.forEach(participantWs => {
|
group.participants.forEach(participantWs => {
|
||||||
if (participantWs !== ws) {
|
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