From 38509019fd1227f76eef9250d40b7a6aa8b5ce94 Mon Sep 17 00:00:00 2001 From: Jeremy Kahn Date: Tue, 28 Mar 2023 09:13:35 -0500 Subject: [PATCH] fix: clean up old rtcPeerConnections --- src/components/Shell/useConnectionTest.ts | 5 ++++- src/services/ConnectionTest/ConnectionTest.ts | 14 ++++++++++---- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/src/components/Shell/useConnectionTest.ts b/src/components/Shell/useConnectionTest.ts index 024791d..f1bfc7c 100644 --- a/src/components/Shell/useConnectionTest.ts +++ b/src/components/Shell/useConnectionTest.ts @@ -71,12 +71,15 @@ export const useConnectionTest = () => { setHasRelay(false) console.error(e) } + + return connectionTest } ;(async () => { while (true) { - await checkConnection() + const connectionTest = await checkConnection() await sleep(pollInterval) + connectionTest.destroy() } })() }, []) diff --git a/src/services/ConnectionTest/ConnectionTest.ts b/src/services/ConnectionTest/ConnectionTest.ts index 107b843..7916ed2 100644 --- a/src/services/ConnectionTest/ConnectionTest.ts +++ b/src/services/ConnectionTest/ConnectionTest.ts @@ -15,16 +15,18 @@ export class ConnectionTest extends EventTarget { hasPeerReflexive = false hasServerReflexive = false + rtcPeerConnection?: RTCPeerConnection + async runRtcPeerConnectionTest() { if (typeof RTCPeerConnection === 'undefined') return const { iceServers } = rtcConfig - const rtcPeerConnection = new RTCPeerConnection({ + this.rtcPeerConnection = new RTCPeerConnection({ iceServers, }) - rtcPeerConnection.addEventListener('icecandidate', event => { + this.rtcPeerConnection.addEventListener('icecandidate', event => { if (event.candidate?.candidate.length) { const parsedCandidate = parseCandidate(event.candidate.candidate) let eventType: ConnectionTestEvents | undefined @@ -65,13 +67,17 @@ export class ConnectionTest extends EventTarget { // Kick off the connection test try { - const rtcSessionDescription = await rtcPeerConnection.createOffer({ + const rtcSessionDescription = await this.rtcPeerConnection.createOffer({ offerToReceiveAudio: true, }) - rtcPeerConnection.setLocalDescription(rtcSessionDescription) + this.rtcPeerConnection.setLocalDescription(rtcSessionDescription) } catch (e) {} } + + destroy() { + this.rtcPeerConnection?.close() + } } export const connectionTest = new ConnectionTest()