From d8ce736cc0094f1afa2409261e0d66e1d321e56e Mon Sep 17 00:00:00 2001 From: Carlos Santos <4a.santos@gmail.com> Date: Mon, 12 May 2025 13:39:45 +0200 Subject: [PATCH] ov-components: improve disconnect handling and manage client-initiated disconnect events --- .../components/session/session.component.ts | 23 +++++++++++-------- 1 file changed, 14 insertions(+), 9 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 0ebb8b26..efc01379 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 @@ -104,6 +104,7 @@ export class SessionComponent implements OnInit, OnDestroy { loading: boolean = true; private shouldDisconnectRoomWhenComponentIsDestroyed: boolean = true; + private shouldListenClientInitiatedDisconnectEvent: boolean = true; private readonly SIDENAV_WIDTH_LIMIT_MODE = 790; private menuSubscription: Subscription; private layoutWidthSubscription: Subscription; @@ -243,6 +244,7 @@ export class SessionComponent implements OnInit, OnDestroy { async disconnectRoom(reason: ParticipantLeftReason) { // Mark session as disconnected for avoiding to do it again in ngOnDestroy this.shouldDisconnectRoomWhenComponentIsDestroyed = false; + this.shouldListenClientInitiatedDisconnectEvent = false; await this.openviduService.disconnectRoom(() => { this.onParticipantLeft.emit({ roomName: this.openviduService.getRoomName(), @@ -487,18 +489,20 @@ export class SessionComponent implements OnInit, OnDestroy { }); this.room.on(RoomEvent.Disconnected, async (reason: DisconnectReason | undefined) => { + this.shouldDisconnectRoomWhenComponentIsDestroyed = false; const participantLeftEvent: ParticipantLeftEvent = { roomName: this.openviduService.getRoomName(), participantName: this.participantService.getLocalParticipant()?.identity || '', reason: ParticipantLeftReason.NETWORK_DISCONNECT }; const messageErrorKey = 'ERRORS.DISCONNECT'; - let descriptionErrorKey = 'ERRORS.NETWORK_DISCONNECT'; - + let descriptionErrorKey = ''; switch (reason) { case DisconnectReason.CLIENT_INITIATED: - // Skip disconnect reason if the user has left the room - return; + // Skip disconnect reason if a default disconnect method has been called + if (!this.shouldListenClientInitiatedDisconnectEvent) return; + participantLeftEvent.reason = ParticipantLeftReason.LEAVE; + break; case DisconnectReason.DUPLICATE_IDENTITY: participantLeftEvent.reason = ParticipantLeftReason.DUPLICATE_IDENTITY; descriptionErrorKey = 'ERRORS.DUPLICATE_IDENTITY'; @@ -527,11 +531,12 @@ export class SessionComponent implements OnInit, OnDestroy { this.log.e('Room Disconnected', participantLeftEvent.reason); this.onParticipantLeft.emit(participantLeftEvent); this.onRoomDisconnected.emit(); - this.actionService.openDialog( - this.translateService.translate(messageErrorKey), - this.translateService.translate(descriptionErrorKey) - ); - // await this.disconnectRoom(); + if (descriptionErrorKey) { + this.actionService.openDialog( + this.translateService.translate(messageErrorKey), + this.translateService.translate(descriptionErrorKey) + ); + } }); }