@@ -86,6 +86,9 @@ export class SystemAccessPoint extends EventEmitter {
8686 }
8787
8888 this . webSocket = this . createWebSocket ( certificateVerification ) ;
89+ // (Re-)create the web socket keepalive timer
90+ this . webSocketKeepaliveSubscription ?. unsubscribe ( ) ;
91+ this . webSocketKeepaliveSubscription = undefined ;
8992 this . webSocketKeepaliveSubscription =
9093 this . webSocketKeepaliveTimer$ . subscribe ( ( ) => {
9194 if ( ! ( this . webSocket && this . webSocket . readyState === WebSocket . OPEN ) )
@@ -94,6 +97,7 @@ export class SystemAccessPoint extends EventEmitter {
9497 this . logger . log ( "keepalive timer expired, sending ping message..." ) ;
9598 this . webSocket . ping ( ) ;
9699 } ) ;
100+ this . webSocketKeepaliveTimerReset$ . next ( ) ;
97101 }
98102
99103 /**
@@ -144,6 +148,9 @@ export class SystemAccessPoint extends EventEmitter {
144148 webSocket . on ( "ping" , ( data : Buffer ) => {
145149 this . emit ( "websocket-ping" , data ) ;
146150 this . logger . debug ( "Ping received" , data . toString ( "ascii" ) ) ;
151+ this . webSocketKeepaliveTimerReset$ . next ( ) ;
152+ webSocket . pong ( data ) ;
153+ this . logger . debug ( "Pong sent" , data . toString ( "ascii" ) ) ;
147154 } ) ;
148155 webSocket . on ( "pong" , ( data : Buffer ) => {
149156 this . emit ( "websocket-pong" , data ) ;
@@ -164,6 +171,8 @@ export class SystemAccessPoint extends EventEmitter {
164171 webSocket . on ( "close" , ( code , reason ) => {
165172 this . emit ( "websocket-close" , code , reason ) ;
166173 this . logger . log ( "Connection closed" ) ;
174+ this . webSocketKeepaliveSubscription ?. unsubscribe ( ) ;
175+ this . webSocketKeepaliveSubscription = undefined ;
167176 } ) ;
168177 webSocket . on ( "message" , ( data : RawData , isBinary : boolean ) => {
169178 this . emit ( "websocket-message" , data , isBinary ) ;
@@ -182,8 +191,6 @@ export class SystemAccessPoint extends EventEmitter {
182191 throw new Error ( "Web socket is not open" ) ;
183192 }
184193
185- this . webSocketKeepaliveSubscription ?. unsubscribe ( ) ;
186- this . webSocketKeepaliveSubscription = undefined ;
187194 if ( force ) {
188195 this . webSocket . terminate ( ) ;
189196 } else {
0 commit comments