clever-kite-90169
01/29/2021, 7:47 AMif (connected || connecting) {
return;
}
setConnecting(true);
try {
const authResp = await fetch('/api/rtc/auth', {
body: JSON.stringify({
room_id: sessionId,
}),
headers: {
'Content-Type': 'application/json',
},
method: 'POST',
});
const { token } = await authResp.json();
const r = await Livekit.connect(
{
host: RTC_HOST as string,
port: parseInt(RTC_PORT as string, 10),
secure: RTC_SECURE === 'true',
},
token,
{
logLevel: LogLevel.debug,
},
);
room.current = r;
setConnected(true);
setConnecting(false);
console.log('Connnected!');
room.current
.on(RoomEvent.ParticipantConnected, ParticipantConnected)
.on(RoomEvent.ParticipantDisconnected, ParticipantDisconnected)
.on(RoomEvent.TrackMessage, TrackMessage)
.on(RoomEvent.TrackSubscribed, onTrackSubscribed)
.on(RoomEvent.TrackUnsubscribed, onTrackUnsubscribed);
} catch (err) {
setConnected(false);
setConnecting(false);
console.error('Failed to connect to livekit', err);
}