From f28ebd15ad226ccb1269ed9c8c9c3750f3cc155b Mon Sep 17 00:00:00 2001 From: dank074 Date: Wed, 7 May 2025 23:03:29 -0500 Subject: [PATCH] set timeout when waiting for connection --- src/webrtc/opcodes/Video.ts | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/src/webrtc/opcodes/Video.ts b/src/webrtc/opcodes/Video.ts index 7bf96a4f..d78827a4 100644 --- a/src/webrtc/opcodes/Video.ts +++ b/src/webrtc/opcodes/Video.ts @@ -52,12 +52,28 @@ export async function onVideo(this: WebRtcWebSocket, payload: VoicePayload) { const wantsToProduceVideo = d.video_ssrc !== 0 && stream?.active; // this is to handle a really weird case where the client sends audio info before the - // dtls ice connection is completely connected. TODO: find a better way to handle this + // dtls ice connection is completely connected. Wait for connection for 3 seconds + // and if no connection, just ignore this message if (!this.webRtcClient.webrtcConnected) { if (wantsToProduceAudio) { - await new Promise((resolve, reject) => { - this.webRtcClient?.emitter.once("connected", () => resolve()); - }); + try { + await Promise.race([ + new Promise((resolve, reject) => { + this.webRtcClient?.emitter.once("connected", () => + resolve(), + ); + }), + new Promise((resolve, reject) => { + // Reject after 3 seconds if still not connected + setTimeout(() => { + if (this.webRtcClient?.webrtcConnected) resolve(); + else reject(); + }, 3000); + }), + ]); + } catch (e) { + return; // just ignore this message if client didn't connect within 3 seconds + } } else return; }