Skip to content

Commit 0fb4823

Browse files
authored
Avoid logging the WebSocket fallback even if it's not being used. (#576)
1 parent e551fc4 commit 0fb4823

File tree

2 files changed

+12
-13
lines changed

2 files changed

+12
-13
lines changed

js/moq/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@kixelated/moq",
33
"type": "module",
4-
"version": "0.8.1",
4+
"version": "0.8.2",
55
"description": "Media over QUIC library",
66
"license": "(MIT OR Apache-2.0)",
77
"repository": "github:kixelated/moq",

js/moq/src/connection.ts

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -60,19 +60,9 @@ export async function connect(url: URL, props?: ConnectionProps): Promise<Connec
6060
// Give QUIC a 200ms head start to connect before trying WebSocket, unless WebSocket has won in the past.
6161
// NOTE that QUIC should be faster because it involves 1/2 fewer RTTs.
6262
const headstart = !webtransport || websocketWon.get(url) ? 0 : (props?.websocket?.delay ?? 200);
63-
6463
const websocket =
6564
props?.websocket?.enabled !== false
66-
? new Promise<WebTransport>((resolve) => setTimeout(resolve, headstart)).then(() => {
67-
const websocketUrl = props?.websocket?.url ?? url;
68-
if (headstart) {
69-
console.debug(
70-
websocketUrl.toString(),
71-
"no WebTransport after 200ms, attempting WebSocket fallback",
72-
);
73-
}
74-
return connectWebSocket(websocketUrl, cancel);
75-
})
65+
? connectWebSocket(props?.websocket?.url ?? url, headstart, cancel)
7666
: undefined;
7767

7868
if (!websocket && !webtransport) {
@@ -175,7 +165,16 @@ async function connectWebTransport(
175165
}
176166

177167
// TODO accept arguments to control the port/path used.
178-
async function connectWebSocket(url: URL, cancel: Promise<void>): Promise<WebTransport | undefined> {
168+
async function connectWebSocket(url: URL, delay: number, cancel: Promise<void>): Promise<WebTransport | undefined> {
169+
const timer = new Promise<void>((resolve) => setTimeout(resolve, delay));
170+
171+
const active = await Promise.race([cancel, timer.then(() => true)]);
172+
if (!active) return undefined;
173+
174+
if (delay) {
175+
console.debug(url.toString(), `no WebTransport after ${delay}ms, attempting WebSocket fallback`);
176+
}
177+
179178
const quic = new WebTransportWs(url);
180179

181180
// Wait for the WebSocket to connect, or for the cancel promise to resolve.

0 commit comments

Comments
 (0)