@@ -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