Compare commits

...

2 Commits

Author SHA1 Message Date
8e016bcae2 Fix the type errors by more explicitly assigning types
This is def more verbose but it was a lot easier to make tsc happy.
2023-11-30 15:28:31 -05:00
9ef7fcc2ea Start to add in State exports to the EngineConnection
I want to start tracking more of the connection health and putting it
into the Network Connectivity widget. This is the first step (of many)
to export the status of the connection and retain errors we have hit
during the handshake.
2023-11-30 13:20:42 -05:00

View File

@ -48,6 +48,41 @@ type Timeout = ReturnType<typeof setTimeout>
type ClientMetrics = Models['ClientMetrics_type']
type ConnectionStage = 'websocket' | 'ice' | 'webrtc'
type ConnectionState = 'ok' | 'pending' | 'failed'
type ConnectionError = {
type: string
message: string
raw: any
}
type ConnectionRecord = {
status: ConnectionState
errors: ConnectionError[]
}
type ConnectionStatus = {
websocket: ConnectionRecord
ice: ConnectionRecord
webrtc: ConnectionRecord
}
const CONNECTION_STATUSES_DEFAULT: ConnectionStatus = {
websocket: {
status: 'pending',
errors: [],
},
ice: {
status: 'pending',
errors: [],
},
webrtc: {
status: 'pending',
errors: [],
},
}
// EngineConnection encapsulates the connection(s) to the Engine
// for the EngineCommandManager; namely, the underlying WebSocket
// and WebRTC connections.
@ -70,6 +105,8 @@ class EngineConnection {
private onClose: (engineConnection: EngineConnection) => void
private onNewTrack: (track: NewTrackArgs) => void
private connectionStatuses: ConnectionStatus
// TODO: actual type is ClientMetrics
private webrtcStatsCollector?: () => Promise<ClientMetrics>
@ -104,6 +141,7 @@ class EngineConnection {
this.onConnectionStarted = onConnectionStarted
this.onClose = onClose
this.onNewTrack = onNewTrack
this.connectionStatuses = Object.assign({}, CONNECTION_STATUSES_DEFAULT)
// TODO(paultag): This ought to be tweakable.
const pingIntervalMs = 10000
@ -204,6 +242,7 @@ class EngineConnection {
)
}
this.connectionStatuses = Object.assign({}, CONNECTION_STATUSES_DEFAULT)
this.websocket = new WebSocket(this.url, [])
this.websocket.binaryType = 'arraybuffer'
@ -221,6 +260,12 @@ class EngineConnection {
console.error(
`ICE candidate returned an error: ${event.errorCode}: ${event.errorText} for ${event.url}`
)
this.connectionStatuses.ice.status = 'failed'
this.connectionStatuses.ice.errors.push({
type: event.type,
message: event.errorText,
raw: event,
})
})
this.pc.addEventListener('connectionstatechange', (event) => {
@ -228,15 +273,20 @@ class EngineConnection {
if (this.shouldTrace()) {
iceSpan.resolve?.()
}
this.connectionStatuses.ice.status = 'ok'
this.connectionStatuses.webrtc.status = 'ok'
console.log(this.connectionStatuses)
} else if (this.pc?.iceConnectionState === 'failed') {
// failed is a terminal state; let's explicitly kill the
// connection to the server at this point.
console.log('failed to negotiate ice connection; restarting')
this.connectionStatuses.webrtc.status = 'failed'
this.close()
}
})
this.websocket.addEventListener('open', (event) => {
this.connectionStatuses.websocket.status = 'ok'
if (this.shouldTrace()) {
websocketSpan.resolve?.()
@ -280,6 +330,12 @@ class EngineConnection {
this.websocket.addEventListener('error', (event) => {
console.log('websocket connection error', event)
this.connectionStatuses.websocket.status = 'failed'
this.connectionStatuses.websocket.errors.push({
type: event.type,
message: 'an unknown websocket error occurred',
raw: event,
})
this.close()
})