fix: clean up old rtcPeerConnections

This commit is contained in:
Jeremy Kahn 2023-03-28 09:13:35 -05:00
parent a4b7c8e9ce
commit 38509019fd
2 changed files with 14 additions and 5 deletions

View File

@ -71,12 +71,15 @@ export const useConnectionTest = () => {
setHasRelay(false) setHasRelay(false)
console.error(e) console.error(e)
} }
return connectionTest
} }
;(async () => { ;(async () => {
while (true) { while (true) {
await checkConnection() const connectionTest = await checkConnection()
await sleep(pollInterval) await sleep(pollInterval)
connectionTest.destroy()
} }
})() })()
}, []) }, [])

View File

@ -15,16 +15,18 @@ export class ConnectionTest extends EventTarget {
hasPeerReflexive = false hasPeerReflexive = false
hasServerReflexive = false hasServerReflexive = false
rtcPeerConnection?: RTCPeerConnection
async runRtcPeerConnectionTest() { async runRtcPeerConnectionTest() {
if (typeof RTCPeerConnection === 'undefined') return if (typeof RTCPeerConnection === 'undefined') return
const { iceServers } = rtcConfig const { iceServers } = rtcConfig
const rtcPeerConnection = new RTCPeerConnection({ this.rtcPeerConnection = new RTCPeerConnection({
iceServers, iceServers,
}) })
rtcPeerConnection.addEventListener('icecandidate', event => { this.rtcPeerConnection.addEventListener('icecandidate', event => {
if (event.candidate?.candidate.length) { if (event.candidate?.candidate.length) {
const parsedCandidate = parseCandidate(event.candidate.candidate) const parsedCandidate = parseCandidate(event.candidate.candidate)
let eventType: ConnectionTestEvents | undefined let eventType: ConnectionTestEvents | undefined
@ -65,13 +67,17 @@ export class ConnectionTest extends EventTarget {
// Kick off the connection test // Kick off the connection test
try { try {
const rtcSessionDescription = await rtcPeerConnection.createOffer({ const rtcSessionDescription = await this.rtcPeerConnection.createOffer({
offerToReceiveAudio: true, offerToReceiveAudio: true,
}) })
rtcPeerConnection.setLocalDescription(rtcSessionDescription) this.rtcPeerConnection.setLocalDescription(rtcSessionDescription)
} catch (e) {} } catch (e) {}
} }
destroy() {
this.rtcPeerConnection?.close()
}
} }
export const connectionTest = new ConnectionTest() export const connectionTest = new ConnectionTest()