From f3e551fc4ab2c3e702a042e4737392a2fd2fa44e Mon Sep 17 00:00:00 2001 From: Carlos Santos <4a.santos@gmail.com> Date: Wed, 12 Nov 2025 17:51:26 +0100 Subject: [PATCH] ov-components: Refactor data handling in SessionComponent to improve payload processing and error logging --- .../components/session/session.component.ts | 29 ++++++++----------- 1 file changed, 12 insertions(+), 17 deletions(-) diff --git a/openvidu-components-angular/projects/openvidu-components-angular/src/lib/components/session/session.component.ts b/openvidu-components-angular/projects/openvidu-components-angular/src/lib/components/session/session.component.ts index 9c48b7d60..d27d105db 100644 --- a/openvidu-components-angular/projects/openvidu-components-angular/src/lib/components/session/session.component.ts +++ b/openvidu-components-angular/projects/openvidu-components-angular/src/lib/components/session/session.component.ts @@ -467,17 +467,8 @@ export class SessionComponent implements OnInit, OnDestroy { RoomEvent.DataReceived, async (payload: Uint8Array, participant?: RemoteParticipant, _?: DataPacket_Kind, topic?: string) => { try { - const rawText = new TextDecoder().decode(payload); - this.log.d('DataReceived (raw)', { topic }); - - const message = safeJsonParse(rawText); - if (!message) { - this.log.w('Discarding data: malformed JSON', rawText); - return; - } - + const decoder = new TextDecoder(); const fromServer = participant === undefined; - // Validate source and resolve participant info const storedParticipant = participant ? this.participantService.getRemoteParticipantBySid(participant.sid || '') @@ -494,19 +485,23 @@ export class SessionComponent implements OnInit, OnDestroy { const participantIdentity = storedParticipant?.identity || ''; const participantName = storedParticipant?.name || ''; - // Decrypt if required - const decryptedPayload = await this.decryptIfNeeded(topic, payload, participantIdentity); + if (this.e2eeService.isEnabled) { + payload = await this.decryptIfNeeded(topic, payload, participantIdentity); + } - // Parse event payload after possible decryption - const event = safeJsonParse(new TextDecoder().decode(decryptedPayload)); - if (!event) { - this.log.e('Error parsing data message after decryption'); + const rawText = decoder.decode(payload); + this.log.d('DataReceived (raw)', { topic }); + + const eventMessage = safeJsonParse(rawText); + if (!eventMessage) { + this.log.w('Discarding data: malformed JSON', rawText); return; } + this.log.d(`Data event received: ${topic}`); // Dispatch handling - this.handleDataEvent(topic, event, participantName || participantIdentity || 'Unknown'); + this.handleDataEvent(topic, eventMessage, participantName || participantIdentity || 'Unknown'); } catch (err) { this.log.e('Unhandled error processing DataReceived', err); }