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)
console.error(e)
}
return connectionTest
}
;(async () => {
while (true) {
await checkConnection()
const connectionTest = await checkConnection()
await sleep(pollInterval)
connectionTest.destroy()
}
})()
}, [])

View File

@ -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()