Skip to content

Commit

Permalink
fix: renegotiate webRTC when remote client disconnects
Browse files Browse the repository at this point in the history
Co-authored-by: Alex Stelea <alex.s@rdx.works>
  • Loading branch information
xstelea and xstelea authored Mar 8, 2023
1 parent e015c9b commit a032145
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 8 deletions.
4 changes: 4 additions & 0 deletions .env.localhost
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
VITE_APP_SIGNALING_SERVER_BASE_URL=ws://localhost:4000
VITE_APP_LOG_LEVEL=0
VITE_APP_IS_INITIATOR=true
VITE_APP_USE_TARGET_CLIENT_ID=true
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"license": "Apache-2.0",
"scripts": {
"start": "DEV_TOOLS=true vite",
"start:local": "DEV_TOOLS=true vite --mode localhost",
"start:beta": "DEV_TOOLS=true vite --mode beta",
"build:dev": "tsc && vite build --mode development",
"build:beta": "tsc && vite build --mode beta",
Expand Down
9 changes: 2 additions & 7 deletions src/connector/secrets-client.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Logger } from 'tslog'
import { ReplaySubject, share, tap } from 'rxjs'
import { ReplaySubject } from 'rxjs'
import { deriveSecretsFromPassword } from './helpers/derive-secrets-from-connection-password'
import { generateConnectionPassword } from './helpers/generate-connection-password'
import { Secrets } from './_types'
Expand All @@ -23,11 +23,6 @@ export const SecretsClient = (input: { logger?: Logger<unknown> }) => {
deriveSecretsFromPassword(password).map((secrets) => {
secretsSubject.next(secrets)
}),
secrets$: secretsSubject.pipe(
tap(() => {
logger?.debug(`🔐💾 connection password set`)
}),
share()
),
secrets$: secretsSubject,
}
}
4 changes: 4 additions & 0 deletions src/connector/signaling/signaling-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,10 @@ export const SignalingClient = (input: {
method,
source,
}))
.mapErr((err) => {
logger?.error(`prepareMessage`, err)
return err
})

const sendMessage = (
message: Pick<DataTypes, 'payload' | 'method' | 'source' | 'targetClientId'>
Expand Down
12 changes: 12 additions & 0 deletions src/connector/webrtc/webrtc-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
filter,
first,
merge,
mergeMap,
Subject,
Subscription,
switchMap,
Expand Down Expand Up @@ -79,6 +80,17 @@ export const WebRtcClient = (

const subscriptions = new Subscription()

subscriptions.add(
signalingClient.remoteClientConnected$
.pipe(
first(),
mergeMap(() =>
signalingClient.remoteClientDisconnected$.pipe(tap(() => restart()))
)
)
.subscribe()
)

subscriptions.add(
merge(onLocalOffer$, onLocalAnswer$, onLocalIceCandidate$)
.pipe(concatMap(input.signalingClient.sendMessage))
Expand Down
4 changes: 3 additions & 1 deletion src/pairing/pairing.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,9 @@ export const Paring = () => {
.getItem('connectionPassword')
.andThen(({ connectionPassword }) => {
if (connectionPassword) {
connectorClient.setConnectionPassword(connectionPassword)
connectorClient.setConnectionPassword(
Buffer.from(connectionPassword, 'hex')
)
return ok(null)
} else {
connectorClient.connect()
Expand Down

0 comments on commit a032145

Please sign in to comment.