Skip to content

Commit

Permalink
fix: handle server url with params correctly
Browse files Browse the repository at this point in the history
Change-Id: Icc11fab672200138ab11367671adc7565aa37a8a
  • Loading branch information
jiyeyuran committed Jan 9, 2025
1 parent 664fff7 commit d313355
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 8 deletions.
5 changes: 5 additions & 0 deletions .changeset/poor-garlics-cry.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'livekit-client': patch
---

fix: handle server url with params correctly
27 changes: 19 additions & 8 deletions src/api/SignalClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -258,14 +258,18 @@ export class SignalClient {
abortSignal?: AbortSignal,
): Promise<JoinResponse | ReconnectResponse | undefined> {
this.connectOptions = opts;
url = toWebsocketUrl(url);
const urlObj = new URL(toWebsocketUrl(url));
// strip trailing slash
url = url.replace(/\/$/, '');
url += '/rtc';
urlObj.pathname = urlObj.pathname.replace(/\/$/, '');
urlObj.pathname += '/rtc';

const clientInfo = getClientInfo();
const params = createConnectionParams(token, clientInfo, opts);

for (const [key, value] of params) {
urlObj.searchParams.set(key, value);
}

return new Promise<JoinResponse | ReconnectResponse | undefined>(async (resolve, reject) => {
const unlock = await this.connectionLock.lock();
try {
Expand Down Expand Up @@ -294,11 +298,11 @@ export class SignalClient {
abortHandler();
}
abortSignal?.addEventListener('abort', abortHandler);
this.log.debug(`connecting to ${url + params}`, this.logContext);
this.log.debug(`connecting to ${urlObj}`, this.logContext);
if (this.ws) {
await this.close(false);
}
this.ws = new WebSocket(url + params);
this.ws = new WebSocket(urlObj);
this.ws.binaryType = 'arraybuffer';

this.ws.onopen = () => {
Expand All @@ -310,7 +314,10 @@ export class SignalClient {
this.state = SignalConnectionState.DISCONNECTED;
clearTimeout(wsTimeout);
try {
const resp = await fetch(`http${url.substring(2)}/validate${params}`);
const validateURL = new URL(urlObj);
validateURL.protocol = `http${validateURL.protocol.substring(2)}`;
validateURL.pathname += '/validate';
const resp = await fetch(validateURL);
if (resp.status.toFixed(0).startsWith('4')) {
const msg = await resp.text();
reject(new ConnectionError(msg, ConnectionErrorReason.NotAllowed, resp.status));
Expand Down Expand Up @@ -880,7 +887,11 @@ export function toProtoSessionDescription(
return sd;
}

function createConnectionParams(token: string, info: ClientInfo, opts: ConnectOpts): string {
function createConnectionParams(
token: string,
info: ClientInfo,
opts: ConnectOpts,
): URLSearchParams {
const params = new URLSearchParams();
params.set('access_token', token);

Expand Down Expand Up @@ -928,5 +939,5 @@ function createConnectionParams(token: string, info: ClientInfo, opts: ConnectOp
params.set('network', navigator.connection.type);
}

return `?${params.toString()}`;
return params;
}

0 comments on commit d313355

Please sign in to comment.