ov-components: add room initialization checks and error handling in SessionComponent

master
Carlos Santos 2025-07-17 17:03:18 +02:00
parent c304c9c761
commit 637142cec6
2 changed files with 47 additions and 3 deletions

View File

@ -189,7 +189,29 @@ export class SessionComponent implements OnInit, OnDestroy {
async ngOnInit() { async ngOnInit() {
this.shouldDisconnectRoomWhenComponentIsDestroyed = true; this.shouldDisconnectRoomWhenComponentIsDestroyed = true;
this.room = this.openviduService.getRoom();
// Check if room is available before proceeding
if (!this.openviduService.isRoomInitialized()) {
this.log.e('Room is not initialized when SessionComponent starts. This indicates a timing issue.');
this.actionService.openDialog(
this.translateService.translate('ERRORS.SESSION'),
'Room is not ready. Please ensure the token is properly configured.'
);
return;
}
// Get room instance
try {
this.room = this.openviduService.getRoom();
this.log.d('Room successfully obtained for SessionComponent');
} catch (error) {
this.log.e('Unexpected error getting room:', error);
this.actionService.openDialog(
this.translateService.translate('ERRORS.SESSION'),
'Failed to get room instance: ' + (error?.message || error)
);
return;
}
// this.subscribeToCaptionLanguage(); // this.subscribeToCaptionLanguage();
this.subcribeToActiveSpeakersChanged(); this.subcribeToActiveSpeakersChanged();

View File

@ -64,6 +64,12 @@ export class OpenViduService {
* @internal * @internal
*/ */
initRoom(): void { initRoom(): void {
// If room already exists, don't recreate it
if (this.room) {
this.log.d('Room already initialized, skipping re-initialization');
return;
}
const videoDeviceId = this.deviceService.getCameraSelected()?.device ?? undefined; const videoDeviceId = this.deviceService.getCameraSelected()?.device ?? undefined;
const audioDeviceId = this.deviceService.getMicrophoneSelected()?.device ?? undefined; const audioDeviceId = this.deviceService.getMicrophoneSelected()?.device ?? undefined;
const roomOptions: RoomOptions = { const roomOptions: RoomOptions = {
@ -88,6 +94,7 @@ export class OpenViduService {
disconnectOnPageLeave: true disconnectOnPageLeave: true
}; };
this.room = new Room(roomOptions); this.room = new Room(roomOptions);
this.log.d('Room initialized successfully');
} }
/** /**
@ -130,12 +137,20 @@ export class OpenViduService {
*/ */
getRoom(): Room { getRoom(): Room {
if (!this.room) { if (!this.room) {
this.log.e('Room is not initialized'); this.log.e('Room is not initialized. Make sure token is set before accessing the room.');
throw new Error('Room is not initialized'); throw new Error('Room is not initialized. Make sure token is set before accessing the room.');
} }
return this.room; return this.room;
} }
/**
* Checks if room is initialized without throwing an error
* @returns true if room is initialized, false otherwise
*/
isRoomInitialized(): boolean {
return !!this.room;
}
/** /**
* Returns the room name * Returns the room name
*/ */
@ -171,6 +186,13 @@ export class OpenViduService {
this.log.e('LiveKit URL is not defined. Please, check the livekitUrl parameter of the VideoConferenceComponent'); this.log.e('LiveKit URL is not defined. Please, check the livekitUrl parameter of the VideoConferenceComponent');
throw new Error('Livekit URL is not defined'); throw new Error('Livekit URL is not defined');
} }
// Initialize room if it doesn't exist yet
// This ensures that getRoom() won't fail if token is set before onTokenRequested
if (!this.room) {
this.log.d('Room not initialized yet, initializing room due to token assignment');
this.initRoom();
}
// return this.room.prepareConnection(this.livekitUrl, this.livekitToken); // return this.room.prepareConnection(this.livekitUrl, this.livekitToken);
} }