@@ -42,10 +42,7 @@ let countNotConnectionProxy = 0;
4242let pongReceived = false ;
4343let timerSendLocation ;
4444let lastLocationTime ;
45- let reconnectAttempts = 0 ;
4645let reconnectTimerId = null ;
47- const reconnectBaseDelay = 1000 ; // 1s
48- const reconnectMaxDelay = 30000 ; // 30s
4946let isReconnecting = false ;
5047
5148const startupTimeout = 5000 ;
@@ -235,6 +232,22 @@ const processNextMessage = () => {
235232 }
236233} ;
237234
235+ const commandProcessedStoredFn = ( commandId ) => {
236+ if ( commandIdToStructure . has ( commandId ) ) {
237+ const structure = commandIdToStructure . get ( commandId ) ;
238+ logger . debug ( `Command ${ commandId } completed, releasing structure: ${ structure } ` ) ;
239+
240+ // Clear the timeout since command completed successfully
241+ if ( structureTimeouts . has ( structure ) ) {
242+ clearTimeout ( structureTimeouts . get ( structure ) ) ;
243+ structureTimeouts . delete ( structure ) ;
244+ }
245+
246+ processingStructures . delete ( structure ) ;
247+ commandIdToStructure . delete ( commandId ) ;
248+ }
249+ } ;
250+
238251// Start the message processor
239252const startMessageProcessor = ( ) => {
240253 if ( processorIntervalId ) {
@@ -245,21 +258,7 @@ const startMessageProcessor = () => {
245258
246259 // Listen to command completion events to unblock structures
247260 if ( hooks ) {
248- hooks . on ( 'command_processed_or_stored' , ( commandId ) => {
249- if ( commandIdToStructure . has ( commandId ) ) {
250- const structure = commandIdToStructure . get ( commandId ) ;
251- logger . debug ( `Command ${ commandId } completed, releasing structure: ${ structure } ` ) ;
252-
253- // Clear the timeout since command completed successfully
254- if ( structureTimeouts . has ( structure ) ) {
255- clearTimeout ( structureTimeouts . get ( structure ) ) ;
256- structureTimeouts . delete ( structure ) ;
257- }
258-
259- processingStructures . delete ( structure ) ;
260- commandIdToStructure . delete ( commandId ) ;
261- }
262- } ) ;
261+ hooks . on ( 'command_processed_or_stored' , commandProcessedStoredFn ) ;
263262 }
264263
265264 processorIntervalId = setInterval ( processNextMessage , MESSAGE_PROCESS_DELAY ) ;
@@ -275,7 +274,7 @@ const stopMessageProcessor = () => {
275274
276275 // Clean up event listener
277276 if ( hooks && typeof hooks . removeListener === 'function' ) {
278- hooks . removeListener ( 'command_processed_or_stored' ) ;
277+ hooks . removeListener ( 'command_processed_or_stored' , commandProcessedStoredFn ) ;
279278 }
280279
281280 // Clear all pending timeouts
@@ -416,16 +415,14 @@ const validationConnectionsProxy = () => {
416415} ;
417416
418417const computeReconnectDelay = ( ) => {
419- const jitter = Math . floor ( Math . random ( ) * 1000 ) ;
420- const delay = Math . min ( reconnectBaseDelay * ( 2 ** reconnectAttempts ) + jitter , reconnectMaxDelay ) ;
421- return delay ;
418+ const jitter = Math . floor ( Math . random ( ) * 4000 ) ;
419+ return jitter ;
422420} ;
423421
424422const scheduleReconnect = ( reason , immediate = false ) => {
425- if ( ! exports . re_schedule ) return ;
423+ logger . info ( `trying to reconnect with ${ reason } and immediate= ${ immediate } ` ) ;
426424 if ( isReconnecting ) return ;
427425 isReconnecting = true ;
428- reconnectAttempts += 1 ;
429426 clearAndResetIntervals ( ) ;
430427 validationConnectionsProxy ( ) ;
431428
@@ -455,7 +452,7 @@ const scheduleReconnect = (reason, immediate = false) => {
455452 logger . warn ( 'isReconnecting stuck true, resetting' ) ;
456453 isReconnecting = false ;
457454 }
458- } , reconnectMaxDelay + 10000 ) ;
455+ } , 5000 ) ;
459456} ;
460457
461458const restartWebsocketCall = ( ) => {
@@ -607,16 +604,15 @@ const webSocketSettings = () => {
607604 notifyAckInterval = setInterval ( retryAckResponses , 4 * 1000 ) ;
608605 getStatusInterval = setInterval ( getStatusByInterval , 5 * 60 * 1000 ) ;
609606 timeOutCancelIntervalHearBeat = setTimeout ( ( ) => {
610- setIntervalWSStatus = setInterval ( exports . heartbeat , 30 * 1000 ) ;
611- } , 90 * 1000 ) ;
607+ setIntervalWSStatus = setInterval ( exports . heartbeat , 10 * 1000 ) ;
608+ } , 60 * 1000 ) ;
612609 const proxy = config . getData ( 'try_proxy' ) ;
613610 let protocol = config . getData ( 'control-panel.protocol' ) ;
614611 const host = config . getData ( 'control-panel.host' ) ;
615612 const deviceKey = keys . get ( ) . device ;
616613 const apiKey = keys . get ( ) . api ;
617614
618615 protocol = protocol === 'https' ? 'wss' : 'ws' ;
619-
620616 if ( ! keys . get ( ) . device ) {
621617 propagateError ( errors . get ( 'NO_DEVICE_KEY' ) ) ;
622618 exports . unload ( ) ;
@@ -639,15 +635,16 @@ const webSocketSettings = () => {
639635 logger . info ( 'Setting up proxy' ) ;
640636 }
641637 gettingStatus = true ;
642- if ( ! ( ! ws || ! ws . readyState || ws . readyState !== 1 ) ) ws . terminate ( ) ;
638+ if ( ! ( ! ws || ! ws . readyState || ws . readyState !== 1 ) ) {
639+ ws . terminate ( ) ;
640+ }
643641 statusTrigger . get_status ( ( _err , status ) => {
644642 gettingStatus = false ;
645643 // Create websocket
646644 ws = new WebSocket ( `${ protocol } ://${ url } ` , options ) ;
647645 ws . on ( 'open' , ( ) => {
648646 logger . debug ( 'ws.open: connection established' ) ;
649647 // successful connect: reset reconnect attempts
650- reconnectAttempts = 0 ;
651648 if ( reconnectTimerId ) {
652649 clearTimeout ( reconnectTimerId ) ;
653650 reconnectTimerId = null ;
@@ -926,7 +923,6 @@ exports.load = (cb) => {
926923exports . unload = ( cb ) => {
927924 logger . debug ( 'exports.unload: shutting down websocket module' ) ;
928925 clearAndResetIntervals ( true ) ;
929- exports . re_schedule = false ;
930926 if ( ws ) ws . terminate ( ) ;
931927 clearTimeout ( pingTimeout ) ;
932928 hooks . remove ( 'connected' ) ;
0 commit comments