99 ConnectionClosedCode ,
1010} from '../types' ;
1111import { TalkMessageStreamPayload } from '../types/signalling/TalkMessageStreamPayload' ;
12+ import { toUnencodedMessage } from '../types/signalling/SignalMessage' ;
1213
1314export class SignallingClient {
1415 private publicEventEmitter : PublicEventEmitter ;
@@ -19,7 +20,6 @@ export class SignallingClient {
1920 private realtime : Ably . Realtime | null = null ;
2021 private channel : Ably . RealtimeChannel | null = null ;
2122 private stopSignal = false ;
22- private isConnected = false ;
2323
2424 constructor (
2525 sessionId : string ,
@@ -65,6 +65,11 @@ export class SignallingClient {
6565 this . realtime = new Ably . Realtime ( {
6666 token : this . ablyToken ,
6767 echoMessages : false ,
68+ disconnectedRetryTimeout : 1000 , // Retry after 1 second
69+ suspendedRetryTimeout : 2000 , // Retry after 2 seconds if suspended (this comes after repeated disconnection and failed reconnects)
70+ transportParams : {
71+ heartbeatInterval : 5000 , // this is the minimum heartbeat interval, we want it low so we can quickly detect disconnections.
72+ } ,
6873 } ) ;
6974 // Initialize Ably Realtime client
7075 this . realtime = new Ably . Realtime ( this . ablyToken ) ;
@@ -74,15 +79,13 @@ export class SignallingClient {
7479 params : { rewind : '100' } ,
7580 } ) ;
7681
82+ this . channel . presence . enter ( ) ;
83+
7784 // Set up connection state listeners
7885 this . realtime . connection . on ( 'connected' , ( ) => {
7986 this . onConnected ( ) ;
8087 } ) ;
8188
82- this . realtime . connection . on ( 'disconnected' , ( ) => {
83- this . onDisconnected ( ) ;
84- } ) ;
85-
8689 this . realtime . connection . on ( 'failed' , ( ) => {
8790 this . onConnectionFailed ( ) ;
8891 } ) ;
@@ -91,6 +94,7 @@ export class SignallingClient {
9194 this . channel . subscribe ( ( message ) => {
9295 this . onMessage ( message ) ;
9396 } ) ;
97+ this . realtime . connect ( ) ;
9498 }
9599
96100 public async sendOffer ( localDescription : RTCSessionDescription ) {
@@ -134,12 +138,6 @@ export class SignallingClient {
134138 ) ;
135139 }
136140
137- if ( ! this . isConnected ) {
138- throw new Error (
139- 'SignallingClient - sendSignalMessage: Cannot send message, connection not established yet.' ,
140- ) ;
141- }
142-
143141 try {
144142 this . channel . publish ( 'signal' , message ) ;
145143 } catch ( error ) {
@@ -171,13 +169,11 @@ export class SignallingClient {
171169 this . realtime . close ( ) ;
172170 this . realtime = null ;
173171 this . channel = null ;
174- this . isConnected = false ;
175172 }
176173 }
177174
178175 private onConnected ( ) : void {
179176 try {
180- this . isConnected = true ;
181177 this . internalEventEmitter . emit ( InternalEvent . WEB_SOCKET_OPEN ) ;
182178 } catch ( e ) {
183179 console . error ( 'SignallingClient - onConnected: error' , e ) ;
@@ -188,14 +184,6 @@ export class SignallingClient {
188184 }
189185 }
190186
191- private onDisconnected ( ) {
192- this . isConnected = false ;
193- if ( this . stopSignal ) {
194- return ;
195- }
196- // Ably handles reconnection automatically
197- }
198-
199187 private onConnectionFailed ( ) {
200188 if ( this . stopSignal ) {
201189 return ;
@@ -208,7 +196,9 @@ export class SignallingClient {
208196
209197 private onMessage ( message : Ably . Message ) {
210198 // Extract the SignalMessage from Ably message data
211- const signalMessage : SignalMessage = message . data ;
199+ let signalMessage : SignalMessage = message . data ;
200+ // Messages coming back from the server may have an encoded payload, convert it to unencoded for cosumption elsewhere in the SDK
201+ signalMessage = toUnencodedMessage ( signalMessage ) ;
212202 this . internalEventEmitter . emit (
213203 InternalEvent . SIGNAL_MESSAGE_RECEIVED ,
214204 signalMessage ,
0 commit comments