@@ -48,6 +48,18 @@ class MessageReceiver extends eventing.EventTarget {
4848 return new this ( signal , addr , deviceId , signalingKey , noWebSocket ) ;
4949 }
5050
51+ async checkRegistration ( ) {
52+ try {
53+ // possible auth or network issue. Make a request to confirm
54+ await this . signal . getDevices ( ) ;
55+ } catch ( e ) {
56+ console . error ( "Invalid network state:" , e ) ;
57+ const ev = new eventing . Event ( 'error' ) ;
58+ ev . error = e ;
59+ await this . dispatchEvent ( ev ) ;
60+ }
61+ }
62+
5163 async connect ( ) {
5264 if ( this . _closing ) {
5365 throw new Error ( "Invalid State: Already Closed" ) ;
@@ -65,7 +77,8 @@ class MessageReceiver extends eventing.EventTarget {
6577 }
6678 return ;
6779 } catch ( e ) {
68- console . warn ( `Connect problem (${ attempts ++ } attempts):` , e ) ;
80+ await this . checkRegistration ( ) ;
81+ console . warn ( `Connect problem (${ attempts ++ } attempts)` ) ;
6982 }
7083 }
7184 } ) ( ) ;
@@ -116,17 +129,10 @@ class MessageReceiver extends eventing.EventTarget {
116129 return ;
117130 }
118131 console . warn ( 'Websocket closed:' , ev . code , ev . reason || '' ) ;
119- try {
120- // possible auth or network issue. Make a request to confirm
121- await this . signal . getDevices ( ) ;
122- } catch ( e ) {
123- // TODO: Catch auth error and unregister here?
124- console . error ( "Invalid network state:" , e ) ;
125- const errorEvent = new eventing . Event ( 'error' ) ;
126- errorEvent . error = e ;
127- await this . dispatchEvent ( errorEvent ) ;
132+ await this . checkRegistration ( ) ;
133+ if ( ! this . _closing ) {
134+ await this . connect ( ) ;
128135 }
129- await this . connect ( ) ;
130136 }
131137
132138 async handleRequest ( request ) {
0 commit comments