import { HttpClient } from "@angular/common/http";
import { Component } from "@angular/core";
import { lastValueFrom } from "rxjs";
import { PanelEvent, PanelService } from "openvidu-angular";
import { environment } from 'src/environments/environment';
@Component({
selector: "app-root",
template: `
NEW PANEL 1
This is my new additional panel
NEW PANEL 2
This is another new panel
`,
styleUrls: ["./app.component.scss"]
})
export class AppComponent {
// Define the URL of the application server
APPLICATION_SERVER_URL = environment.applicationServerUrl;
// Define the name of the room and initialize the token variable
roomName = 'additional-panels';
token!: string;
// Flags to control the visibility of external panels
showExternalPanel: boolean = false;
showExternalPanel2: boolean = false;
constructor(private httpClient: HttpClient, private panelService: PanelService) { }
ngOnInit() {
this.subscribeToPanelToggling();
}
// Function to request a token when a participant joins the room
async onTokenRequested(participantName: string) {
const { token } = await this.getToken(this.roomName, participantName);
this.token = token;
}
// Subscribe to panel toggling events
subscribeToPanelToggling() {
this.panelService.panelOpenedObs.subscribe((ev: PanelEvent) => {
this.showExternalPanel = ev.isOpened && ev.panelType === 'my-panel1';
this.showExternalPanel2 = ev.isOpened && ev.panelType === 'my-panel2';
});
}
// Toggle the visibility of external panels
toggleMyPanel(type: string) {
this.panelService.togglePanel(type);
}
// Function to get a token from the server
async getToken(roomName: string, participantName: string): Promise {
try {
// Send a POST request to the server to obtain a token
return lastValueFrom(this.httpClient.post(this.APPLICATION_SERVER_URL + 'api/sessions', { roomName, participantName }));
} catch (error: any) {
// Handle errors, e.g., if the server is not reachable
if (error.status === 404) {
throw { status: error.status, message: 'Cannot connect with the backend. ' + error.url + ' not found' };
}
throw error;
}
}
}