diff --git a/app/api/connection-details/route.ts b/app/api/connection-details/route.ts index 8e33791..9480385 100644 --- a/app/api/connection-details/route.ts +++ b/app/api/connection-details/route.ts @@ -13,6 +13,11 @@ export async function GET(request: NextRequest) { const roomName = request.nextUrl.searchParams.get('roomName'); const participantName = request.nextUrl.searchParams.get('participantName'); const metadata = request.nextUrl.searchParams.get('metadata') ?? ''; + const region = request.nextUrl.searchParams.get('region'); + const livekitServerUrl = region ? getLiveKitURL(region) : LIVEKIT_URL; + if (livekitServerUrl === undefined) { + throw new Error('Invalid region'); + } if (typeof roomName !== 'string') { return new NextResponse('Missing required query parameter: roomName', { status: 400 }); @@ -33,7 +38,7 @@ export async function GET(request: NextRequest) { // Return connection details const data: ConnectionDetails = { - serverUrl: LIVEKIT_URL!, + serverUrl: livekitServerUrl, roomName: roomName, participantToken: participantToken, participantName: participantName, @@ -59,3 +64,18 @@ function createParticipantToken(userInfo: AccessTokenOptions, roomName: string) at.addGrant(grant); return at.toJwt(); } + +/** + * Get the LiveKit server URL for the given region. + */ +function getLiveKitURL(region: string | null): string { + let targetKey = 'LIVEKIT_URL'; + if (region) { + targetKey = `LIVEKIT_URL_${region}`.toUpperCase(); + } + const url = process.env[targetKey]; + if (!url) { + throw new Error(`${targetKey} is not defined`); + } + return url; +} diff --git a/app/api/url/route.ts b/app/api/url/route.ts deleted file mode 100644 index 232a348..0000000 --- a/app/api/url/route.ts +++ /dev/null @@ -1,14 +0,0 @@ -import { getLiveKitURL } from '@/lib/server-utils'; -import { NextRequest, NextResponse } from 'next/server'; - -export async function GET(req: NextRequest) { - try { - const region = req.nextUrl.searchParams.get('region'); - const livekitUrl = getLiveKitURL(region); - return NextResponse.json({ url: livekitUrl }); - } catch (error) { - if (error instanceof Error) { - return new NextResponse(error.message, { status: 500 }); - } - } -} diff --git a/app/rooms/[roomName]/PageClientImpl.tsx b/app/rooms/[roomName]/PageClientImpl.tsx index 1bf8e0a..5f5b165 100644 --- a/app/rooms/[roomName]/PageClientImpl.tsx +++ b/app/rooms/[roomName]/PageClientImpl.tsx @@ -50,9 +50,14 @@ export function PageClientImpl(props: { const handlePreJoinSubmit = React.useCallback(async (values: LocalUserChoices) => { setPreJoinChoices(values); - const connectionDetailsResp = await fetch( - `${CONN_DETAILS_ENDPOINT}?roomName=${props.roomName}&participantName=${values.username}`, - ); + const url = new URL('/', window.location.origin); + url.pathname = CONN_DETAILS_ENDPOINT; + url.searchParams.append('roomName', props.roomName); + url.searchParams.append('participantName', values.username); + if (props.region) { + url.searchParams.append('region', props.region); + } + const connectionDetailsResp = await fetch(url.toString()); const connectionDetailsData = await connectionDetailsResp.json(); setConnectionDetails(connectionDetailsData); }, []); diff --git a/lib/client-utils.ts b/lib/client-utils.ts index 65f47ae..b37257f 100644 --- a/lib/client-utils.ts +++ b/lib/client-utils.ts @@ -1,25 +1,3 @@ -import { useEffect, useState } from 'react'; - -export function useServerUrl(region?: string) { - const [serverUrl, setServerUrl] = useState(); - useEffect(() => { - let endpoint = `/api/url`; - if (region) { - endpoint += `?region=${region}`; - } - fetch(endpoint).then(async (res) => { - if (res.ok) { - const body = await res.json(); - console.log(body); - setServerUrl(body.url); - } else { - throw Error('Error fetching server url, check server logs'); - } - }); - }); - return serverUrl; -} - export function encodePassphrase(passphrase: string) { return encodeURIComponent(passphrase); } @@ -41,8 +19,3 @@ export function randomString(length: number): string { } return result; } - -export const sleep = (time: number) => - new Promise((resolve) => { - setTimeout(resolve, time); - }); diff --git a/lib/server-utils.ts b/lib/server-utils.ts deleted file mode 100644 index b0dbb16..0000000 --- a/lib/server-utils.ts +++ /dev/null @@ -1,11 +0,0 @@ -export function getLiveKitURL(region: string | null): string { - let targetKey = 'LIVEKIT_URL'; - if (region) { - targetKey = `LIVEKIT_URL_${region}`.toUpperCase(); - } - const url = process.env[targetKey]; - if (!url) { - throw new Error(`${targetKey} is not defined`); - } - return url; -}