forked from Shiloh/remnantchat
feat: expire host/relay checks
This commit is contained in:
parent
38509019fd
commit
8fa4f666e0
@ -9,6 +9,8 @@ export enum ConnectionTestEvents {
|
|||||||
|
|
||||||
export type ConnectionTestEvent = CustomEvent<ConnectionTest>
|
export type ConnectionTestEvent = CustomEvent<ConnectionTest>
|
||||||
|
|
||||||
|
const checkExperationTime = 10 * 1000
|
||||||
|
|
||||||
export class ConnectionTest extends EventTarget {
|
export class ConnectionTest extends EventTarget {
|
||||||
hasHost = false
|
hasHost = false
|
||||||
hasRelay = false
|
hasRelay = false
|
||||||
@ -26,6 +28,26 @@ export class ConnectionTest extends EventTarget {
|
|||||||
iceServers,
|
iceServers,
|
||||||
})
|
})
|
||||||
|
|
||||||
|
const hasHostCheckTimeout = setTimeout(() => {
|
||||||
|
this.hasHost = false
|
||||||
|
|
||||||
|
this.dispatchEvent(
|
||||||
|
new CustomEvent(ConnectionTestEvents.HAS_HOST_CHANGED, {
|
||||||
|
detail: this,
|
||||||
|
})
|
||||||
|
)
|
||||||
|
}, checkExperationTime)
|
||||||
|
|
||||||
|
const hasRelayCheckTimeout = setTimeout(() => {
|
||||||
|
this.hasRelay = false
|
||||||
|
|
||||||
|
this.dispatchEvent(
|
||||||
|
new CustomEvent(ConnectionTestEvents.HAS_RELAY_CHANGED, {
|
||||||
|
detail: this,
|
||||||
|
})
|
||||||
|
)
|
||||||
|
}, checkExperationTime)
|
||||||
|
|
||||||
this.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)
|
||||||
@ -33,11 +55,13 @@ export class ConnectionTest extends EventTarget {
|
|||||||
|
|
||||||
switch (parsedCandidate.type) {
|
switch (parsedCandidate.type) {
|
||||||
case 'host':
|
case 'host':
|
||||||
|
clearTimeout(hasHostCheckTimeout)
|
||||||
this.hasHost = true
|
this.hasHost = true
|
||||||
eventType = ConnectionTestEvents.HAS_HOST_CHANGED
|
eventType = ConnectionTestEvents.HAS_HOST_CHANGED
|
||||||
break
|
break
|
||||||
|
|
||||||
case 'relay':
|
case 'relay':
|
||||||
|
clearTimeout(hasRelayCheckTimeout)
|
||||||
this.hasRelay = true
|
this.hasRelay = true
|
||||||
eventType = ConnectionTestEvents.HAS_RELAY_CHANGED
|
eventType = ConnectionTestEvents.HAS_RELAY_CHANGED
|
||||||
break
|
break
|
||||||
|
Loading…
Reference in New Issue
Block a user