Skip to content

Commit

Permalink
fix: listen to peer connection change
Browse files Browse the repository at this point in the history
  • Loading branch information
xstelea committed Feb 5, 2024
1 parent 0394f36 commit 4a1eecf
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 5 deletions.
7 changes: 4 additions & 3 deletions src/connector/webrtc/peer-connection-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,16 +65,17 @@ export const PeerConnectionClient = (input: {
)
}

const onIceConnectionStateChange = () => {
const onPeerConnectionState = () => {
logger?.debug(
`🕸🧊 onIceConnectionStateChange: ${peerConnection.iceConnectionState}`,
`🕸🧊 onPeerConnectionStateChange: ${peerConnection.connectionState}`,
)
subjects.peerConnectionStateSubject.next(peerConnection.connectionState)
}

peerConnection.onnegotiationneeded = onNegotiationNeeded
peerConnection.onsignalingstatechange = onSignalingStateChange
peerConnection.onicegatheringstatechange = onIceGatheringStateChange
peerConnection.oniceconnectionstatechange = onIceConnectionStateChange
peerConnection.onconnectionstatechange = onPeerConnectionState

const setLocalDescription = (description: RTCSessionDescriptionInit) =>
ResultAsync.fromPromise(
Expand Down
3 changes: 3 additions & 0 deletions src/connector/webrtc/subjects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,7 @@ export const WebRtcSubjects = () => ({
remoteAnswerSubject: new BehaviorSubject<
RTCSessionDescriptionInit | undefined
>(undefined),
peerConnectionStateSubject: new BehaviorSubject<
RTCPeerConnectionState | undefined
>(undefined),
})
5 changes: 3 additions & 2 deletions src/connector/webrtc/webrtc-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -212,9 +212,10 @@ export const WebRtcClient = (input: {
)

subscriptions.add(
subjects.iceConnectionStateSubject
subjects.peerConnectionStateSubject
.pipe(
filter((state) => state === 'disconnected'),
filter((state): state is RTCPeerConnectionState => !!state),
filter((state) => ['disconnected', 'closed', 'failed'].includes(state)),
tap(() => {
restart()
}),
Expand Down

0 comments on commit 4a1eecf

Please sign in to comment.