Skip to content

Commit

Permalink
Merge pull request #1 from radixdlt/allow-dynamic-signaling-server
Browse files Browse the repository at this point in the history
feat: allow dynamic signaling server
  • Loading branch information
dawidsowardx authored Sep 12, 2023
2 parents 97f750c + 6aefe73 commit c9736a2
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 15 deletions.
14 changes: 8 additions & 6 deletions src/connector/__tests__/webrtc.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,31 +46,33 @@ describe('connector client', () => {

const createExtensionConnector = () => {
extensionConnector = ConnectorClient({
turnServers: [],
source: 'extension',
target: 'wallet',
signalingServerBaseUrl:
'wss://signaling-server-dev.rdx-works-main.extratools.works',
logger: extensionLogger,
isInitiator: false,
subjects: extensionConnectorSubjects,
createSignalingSubjects: () => extensionSignalingSubjects,
createWebRtcSubjects: () => extensionWebRtcSubjects,
})
extensionConnector.setConnectionConfig({
signalingServerBaseUrl:
'wss://signaling-server-dev.rdx-works-main.extratools.works',
})
}

const createWalletConnector = () => {
walletConnector = ConnectorClient({
turnServers: [],
source: 'wallet',
target: 'extension',
signalingServerBaseUrl:
'wss://signaling-server-dev.rdx-works-main.extratools.works',
// logger: walletLogger,
subjects: walletConnectorSubjects,
isInitiator: true,
createWebRtcSubjects: () => walletWebRtcSubjects,
})
walletConnector.setConnectionConfig({
signalingServerBaseUrl:
'wss://signaling-server-dev.rdx-works-main.extratools.works',
})
}

beforeEach(async () => {
Expand Down
5 changes: 5 additions & 0 deletions src/connector/_types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,3 +110,8 @@ export type TurnServer = {
export type StunServer = {
urls: string
}

export type ConnectionConfig = {
signalingServerBaseUrl: string
turnServers?: TurnServer[]
}
28 changes: 20 additions & 8 deletions src/connector/connector-client.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { SecretsClient } from './secrets-client'
import { SignalingClient } from './signaling/signaling-client'
import {
combineLatest,
distinctUntilChanged,
filter,
finalize,
Expand All @@ -24,16 +25,14 @@ import { errAsync, ResultAsync } from 'neverthrow'
import { errorIdentity } from '../utils/error-identity'
import { sendMessageOverDataChannelAndWaitForConfirmation } from './webrtc/helpers/send-message-over-data-channel-and-wait-for-confirmation'
import { ConnectorClientSubjects } from './subjects'
import type { MessageErrorReasons, Secrets, TurnServer } from './_types'
import type { ConnectionConfig, MessageErrorReasons, Secrets } from './_types'

export type ConnectorClient = ReturnType<typeof ConnectorClient>

export const ConnectorClient = (input: {
target: MessageSources
source: MessageSources
signalingServerBaseUrl: string
isInitiator: boolean
turnServers: TurnServer[]
logger?: Logger<unknown>
createWebRtcSubjects?: () => WebRtcSubjectsType
createSignalingSubjects?: () => SignalingSubjectsType
Expand Down Expand Up @@ -73,11 +72,22 @@ export const ConnectorClient = (input: {

const subscriptions = new Subscription()

const connection$ = secretsClient.secrets$.pipe(
filter((secrets): secrets is Secrets => !!secrets),
switchMap((secrets) => {
const connection$ = combineLatest([
secretsClient.secrets$.pipe(
filter((secrets): secrets is Secrets => !!secrets),
),
subjects.connectionConfig
.asObservable()
.pipe(
filter(
(connectionConfig): connectionConfig is ConnectionConfig =>
!!connectionConfig,
),
),
]).pipe(
switchMap(([secrets, connectionConfig]) => {
const signalingClient = SignalingClient({
baseUrl: input.signalingServerBaseUrl,
baseUrl: connectionConfig.signalingServerBaseUrl,
target: input.target,
source: input.source,
logger,
Expand All @@ -104,7 +114,7 @@ export const ConnectorClient = (input: {
{
urls: 'stun:stun4.l.google.com:19302',
},
...input.turnServers,
...(connectionConfig.turnServers || []),
],
},
dataChannelConfig: {
Expand Down Expand Up @@ -162,6 +172,8 @@ export const ConnectorClient = (input: {
connectionPassword$: secretsClient.secrets$.pipe(
map((secrets) => secrets?.encryptionKey),
),
setConnectionConfig: (config: ConnectionConfig) =>
subjects.connectionConfig.next(config),
generateConnectionPassword: () => secretsClient.generateConnectionSecrets(),
connect: () => shouldConnectSubject.next(true),
disconnect: () => shouldConnectSubject.next(false),
Expand Down
5 changes: 4 additions & 1 deletion src/connector/subjects.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import { BehaviorSubject, Subject } from 'rxjs'
import type { ChunkedMessageType, Message } from './_types'
import type { ChunkedMessageType, ConnectionConfig, Message } from './_types'

export type ConnectorClientSubjects = ReturnType<typeof ConnectorClientSubjects>

export const ConnectorClientSubjects = () => ({
shouldConnectSubject: new BehaviorSubject<boolean>(false),
connected: new BehaviorSubject<boolean>(false),
connectionConfig: new BehaviorSubject<ConnectionConfig | undefined>(
undefined,
),
triggerRestartSubject: new Subject<void>(),
onDataChannelMessageSubject: new Subject<ChunkedMessageType>(),
onMessage: new Subject<Message>(),
Expand Down

0 comments on commit c9736a2

Please sign in to comment.