Skip to content

Commit

Permalink
client: fix webrtc usage
Browse files Browse the repository at this point in the history
  • Loading branch information
koush committed Aug 18, 2023
1 parent 6e39351 commit cd078af
Showing 1 changed file with 6 additions and 3 deletions.
9 changes: 6 additions & 3 deletions packages/client/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.')
}
Expand Down

0 comments on commit cd078af

Please sign in to comment.