ov-components: Refactor data handling in SessionComponent to improve payload processing and error logging

pull/856/head
Carlos Santos 2025-11-12 17:51:26 +01:00
parent ba80504c9e
commit f3e551fc4a
1 changed files with 12 additions and 17 deletions

View File

@ -467,17 +467,8 @@ export class SessionComponent implements OnInit, OnDestroy {
RoomEvent.DataReceived, RoomEvent.DataReceived,
async (payload: Uint8Array, participant?: RemoteParticipant, _?: DataPacket_Kind, topic?: string) => { async (payload: Uint8Array, participant?: RemoteParticipant, _?: DataPacket_Kind, topic?: string) => {
try { try {
const rawText = new TextDecoder().decode(payload); const decoder = new TextDecoder();
this.log.d('DataReceived (raw)', { topic });
const message = safeJsonParse(rawText);
if (!message) {
this.log.w('Discarding data: malformed JSON', rawText);
return;
}
const fromServer = participant === undefined; const fromServer = participant === undefined;
// Validate source and resolve participant info // Validate source and resolve participant info
const storedParticipant = participant const storedParticipant = participant
? this.participantService.getRemoteParticipantBySid(participant.sid || '') ? this.participantService.getRemoteParticipantBySid(participant.sid || '')
@ -494,19 +485,23 @@ export class SessionComponent implements OnInit, OnDestroy {
const participantIdentity = storedParticipant?.identity || ''; const participantIdentity = storedParticipant?.identity || '';
const participantName = storedParticipant?.name || ''; const participantName = storedParticipant?.name || '';
// Decrypt if required if (this.e2eeService.isEnabled) {
const decryptedPayload = await this.decryptIfNeeded(topic, payload, participantIdentity); payload = await this.decryptIfNeeded(topic, payload, participantIdentity);
}
// Parse event payload after possible decryption const rawText = decoder.decode(payload);
const event = safeJsonParse(new TextDecoder().decode(decryptedPayload)); this.log.d('DataReceived (raw)', { topic });
if (!event) {
this.log.e('Error parsing data message after decryption'); const eventMessage = safeJsonParse(rawText);
if (!eventMessage) {
this.log.w('Discarding data: malformed JSON', rawText);
return; return;
} }
this.log.d(`Data event received: ${topic}`); this.log.d(`Data event received: ${topic}`);
// Dispatch handling // Dispatch handling
this.handleDataEvent(topic, event, participantName || participantIdentity || 'Unknown'); this.handleDataEvent(topic, eventMessage, participantName || participantIdentity || 'Unknown');
} catch (err) { } catch (err) {
this.log.e('Unhandled error processing DataReceived', err); this.log.e('Unhandled error processing DataReceived', err);
} }