@@ -4,6 +4,7 @@ import type { Credentials, DialOptions } from '@viamrobotics/rpc/src/dial';
44import { Duration } from 'google-protobuf/google/protobuf/duration_pb' ;
55import { dialDirect , dialWebRTC } from '@viamrobotics/rpc' ;
66import type { grpc } from '@improbable-eng/grpc-web' ;
7+ import { DISCONNECTED , EventDispatcher , events , RECONNECTED } from '../events' ;
78import proto from '../gen/robot/v1/robot_pb' ;
89import type {
910 PoseInFrame ,
@@ -29,7 +30,6 @@ import { SLAMServiceClient } from '../gen/service/slam/v1/slam_pb_service';
2930import { SensorsServiceClient } from '../gen/service/sensors/v1/sensors_pb_service' ;
3031import { ServoServiceClient } from '../gen/component/servo/v1/servo_pb_service' ;
3132import { VisionServiceClient } from '../gen/service/vision/v1/vision_pb_service' ;
32- import { events } from '../events' ;
3333import { ViamResponseStream } from '../responses' ;
3434import SessionManager from './session-manager' ;
3535import type { Robot , RobotStatusStream } from './robot' ;
@@ -55,7 +55,7 @@ abstract class ServiceClient {
5555 *
5656 * @group Clients
5757 */
58- export class RobotClient implements Robot {
58+ export class RobotClient extends EventDispatcher implements Robot {
5959 private readonly serviceHost : string ;
6060 private readonly webrtcOptions : WebRTCOptions | undefined ;
6161 private readonly sessionOptions : SessionOptions | undefined ;
@@ -114,6 +114,7 @@ export class RobotClient implements Robot {
114114 webrtcOptions ?: WebRTCOptions ,
115115 sessionOptions ?: SessionOptions
116116 ) {
117+ super ( ) ;
117118 this . serviceHost = serviceHost ;
118119 this . webrtcOptions = webrtcOptions ;
119120 this . sessionOptions = sessionOptions ;
@@ -126,6 +127,35 @@ export class RobotClient implements Robot {
126127 return this . transportFactory ( opts ) ;
127128 }
128129 ) ;
130+
131+ events . on ( RECONNECTED , ( ) => {
132+ this . emit ( RECONNECTED , { } ) ;
133+ } ) ;
134+ events . on ( DISCONNECTED , ( ) => {
135+ this . emit ( DISCONNECTED , { } ) ;
136+ if ( this . webrtcOptions ?. noReconnect ) {
137+ return ;
138+ }
139+
140+ let retries = 0 ;
141+ // eslint-disable-next-line no-console
142+ console . debug ( 'connection closed, will try to reconnect' ) ;
143+ void backOff ( ( ) =>
144+ this . connect ( ) . then (
145+ ( ) => {
146+ // eslint-disable-next-line no-console
147+ console . debug ( 'reconnected successfully!' ) ;
148+ events . emit ( RECONNECTED , { } ) ;
149+ } ,
150+ ( error ) => {
151+ // eslint-disable-next-line no-console
152+ console . debug ( `failed to reconnect - retries count: ${ retries } ` ) ;
153+ retries += 1 ;
154+ throw error ;
155+ }
156+ )
157+ ) ;
158+ } ) ;
129159 }
130160
131161 get sessionId ( ) {
@@ -284,6 +314,10 @@ export class RobotClient implements Robot {
284314 this . sessionManager . reset ( ) ;
285315 }
286316
317+ public isConnected ( ) : boolean {
318+ return this . peerConn ?. iceConnectionState === 'connected' ;
319+ }
320+
287321 public async connect (
288322 authEntity = this . savedAuthEntity ,
289323 creds = this . savedCreds
@@ -353,31 +387,11 @@ export class RobotClient implements Robot {
353387 * connection getting closed, so restarting ice is not a valid way to
354388 * recover.
355389 */
356- if ( this . peerConn ?. iceConnectionState === 'closed' ) {
357- if ( this . webrtcOptions ?. noReconnect ) {
358- return ;
359- }
360-
361- let retries = 0 ;
362- // eslint-disable-next-line no-console
363- console . debug ( 'connection closed, will try to reconnect' ) ;
364- void backOff ( ( ) =>
365- this . connect ( ) . then (
366- ( ) => {
367- // eslint-disable-next-line no-console
368- console . debug ( 'reconnected successfully!' ) ;
369- events . emit ( 'reconnected' , { } ) ;
370- } ,
371- ( error ) => {
372- // eslint-disable-next-line no-console
373- console . debug (
374- `failed to reconnect - retries count: ${ retries } `
375- ) ;
376- retries += 1 ;
377- throw error ;
378- }
379- )
380- ) ;
390+ if ( this . peerConn ?. iceConnectionState === 'connected' ) {
391+ events . emit ( RECONNECTED , { } ) ;
392+ } else if ( this . peerConn ?. iceConnectionState === 'closed' ) {
393+ console . log ( 'emit disconnected' ) ;
394+ events . emit ( DISCONNECTED , { } ) ;
381395 }
382396 } ) ;
383397
0 commit comments