Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: handle server url with params correctly #1366

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
jiyeyuran marked this conversation as resolved.
Show resolved Hide resolved
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;
}
Loading