From 3c72bc6c4d8bb7da92e8e948dae532dbf16e004a Mon Sep 17 00:00:00 2001 From: Carlos Santos <4a.santos@gmail.com> Date: Tue, 20 May 2025 13:17:00 +0200 Subject: [PATCH] ov-components: update disconnect logic to handle client-initiated events in OpenViduService --- .../src/lib/components/session/session.component.ts | 8 +++----- .../src/lib/components/toolbar/toolbar.component.ts | 8 +++++++- .../src/lib/services/openvidu/openvidu.service.ts | 10 +++++++++- 3 files changed, 19 insertions(+), 7 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 b7750f52..85ba2041 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,7 +104,6 @@ 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; @@ -244,14 +243,13 @@ 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(), participantName: this.participantService.getLocalParticipant()?.identity || '', reason }); - }); + }, false); } private subscribeToTogglingMenu() { @@ -473,7 +471,7 @@ export class SessionComponent implements OnInit, OnDestroy { ); } - subscribeToReconnection() { + private subscribeToReconnection() { this.room.on(RoomEvent.Reconnecting, () => { this.log.w('Connection lost: Reconnecting'); this.actionService.openConnectionDialog( @@ -500,7 +498,7 @@ export class SessionComponent implements OnInit, OnDestroy { switch (reason) { case DisconnectReason.CLIENT_INITIATED: // Skip disconnect reason if a default disconnect method has been called - if (!this.shouldListenClientInitiatedDisconnectEvent) return; + if (!this.openviduService.shouldHandleClientInitiatedDisconnectEvent) return; participantLeftEvent.reason = ParticipantLeftReason.LEAVE; break; case DisconnectReason.DUPLICATE_IDENTITY: diff --git a/openvidu-components-angular/projects/openvidu-components-angular/src/lib/components/toolbar/toolbar.component.ts b/openvidu-components-angular/projects/openvidu-components-angular/src/lib/components/toolbar/toolbar.component.ts index a2b6e8c9..d21025a5 100644 --- a/openvidu-components-angular/projects/openvidu-components-angular/src/lib/components/toolbar/toolbar.component.ts +++ b/openvidu-components-angular/projects/openvidu-components-angular/src/lib/components/toolbar/toolbar.component.ts @@ -518,7 +518,13 @@ export class ToolbarComponent implements OnInit, OnDestroy, AfterViewInit { */ async disconnect() { try { - await this.openviduService.disconnectRoom(); + await this.openviduService.disconnectRoom(() => { + this.onParticipantLeft.emit({ + roomName: this.openviduService.getRoomName(), + participantName: this.participantService.getLocalParticipant()?.identity || '', + reason: ParticipantLeftReason.LEAVE + }); + }, false); } catch (error) { this.log.e('There was an error disconnecting:', error.code, error.message); this.actionService.openDialog(this.translateService.translate('ERRORS.DISCONNECT'), error?.error || error?.message || error); diff --git a/openvidu-components-angular/projects/openvidu-components-angular/src/lib/services/openvidu/openvidu.service.ts b/openvidu-components-angular/projects/openvidu-components-angular/src/lib/services/openvidu/openvidu.service.ts index 76b3e0ce..0405f86e 100644 --- a/openvidu-components-angular/projects/openvidu-components-angular/src/lib/services/openvidu/openvidu.service.ts +++ b/openvidu-components-angular/projects/openvidu-components-angular/src/lib/services/openvidu/openvidu.service.ts @@ -32,6 +32,13 @@ export class OpenViduService { private room: Room; + /** + * @internal + * Indicates whether the client initiated disconnect event should be handled. + * This is used to determine if the disconnect event should be emitted when the 'Disconnect' event is triggered + */ + shouldHandleClientInitiatedDisconnectEvent = true; + /* * Tracks used in the prejoin component. They are created when the room is not yet created. */ @@ -109,7 +116,8 @@ export class OpenViduService { * @param callback - Optional function to be executed after a successful disconnection * @returns A Promise that resolves once the disconnection is complete */ - async disconnectRoom(callback?: () => void): Promise { + async disconnectRoom(callback?: () => void, shouldHandleClientInitiatedDisconnectEvent: boolean = true): Promise { + this.shouldHandleClientInitiatedDisconnectEvent = shouldHandleClientInitiatedDisconnectEvent; if (this.isRoomConnected()) { this.log.d('Disconnecting from room'); await this.room.disconnect();