From cd078afcf9902e47880d5c384fe7824a8ffcc8b4 Mon Sep 17 00:00:00 2001 From: Koushik Dutta Date: Thu, 17 Aug 2023 18:31:50 -0700 Subject: [PATCH] client: fix webrtc usage --- packages/client/src/index.ts | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/packages/client/src/index.ts b/packages/client/src/index.ts index 8b974b0ff..e25346335 100644 --- a/packages/client/src/index.ts +++ b/packages/client/src/index.ts @@ -294,13 +294,16 @@ export async function connectScryptedClient(options: ScryptedClientOptions): Pro const tryAddresses = !!addresses.length; const webrtcLastFailedKey = 'webrtcLastFailed'; - let tryWebrtc = globalThis.RTCPeerConnection && options.webrtc; - if (!tryWebrtc && globalThis.localStorage && options.webrtc === undefined) { + const canUseWebrtc = !!globalThis.RTCPeerConnection; + let tryWebrtc = canUseWebrtc && options.webrtc; + // try webrtc by default on scrypted cloud. + // but webrtc takes a while to fail, so backoff if it fails to prevent continual slow starts. + if (scryptedCloud && canUseWebrtc && globalThis.localStorage && options.webrtc === undefined) { tryWebrtc = true; const webrtcLastFailed = parseFloat(localStorage.getItem(webrtcLastFailedKey)); // if webrtc has failed in the past day, dont attempt to use it. const now = Date.now(); - if (webrtcLastFailed > now - 1 * 24 * 60 * 60 * 1000) { + if (webrtcLastFailed < now && webrtcLastFailed > now - 1 * 24 * 60 * 60 * 1000) { tryWebrtc = false; console.warn('WebRTC API connection recently failed. Skipping.') }