@@ -330,7 +330,6 @@ export default class ExchangeKlineWs {
330330 this . _onReconnecting = null
331331 this . _onReconnected = null
332332 this . _reconnectAttempts = 0
333- this . _maxReconnectAttempts = 20
334333 this . _reconnectTimer = null
335334 this . _pingTimer = null
336335 this . _closed = false
@@ -344,6 +343,7 @@ export default class ExchangeKlineWs {
344343 this . _dataTimeout = null
345344 this . _openGen = 0
346345 this . _gotData = false
346+ this . _awaitingPongAt = 0
347347 }
348348
349349 /**
@@ -448,9 +448,7 @@ export default class ExchangeKlineWs {
448448 clearTimeout ( this . _connectTimeout )
449449 this . _connectTimeout = null
450450 }
451- const wasReconnect = this . _everConnected
452451 this . _everConnected = true
453- this . _reconnectAttempts = 0
454452 this . _gotData = false
455453 this . _startPing ( )
456454
@@ -465,22 +463,13 @@ export default class ExchangeKlineWs {
465463
466464 // If no data arrives within 12s after open, the connection or subscription
467465 // likely failed silently, so the caller must continue same-venue REST polling.
468- if ( ! this . _fallbackUsed ) {
469- this . _dataTimeout = setTimeout ( ( ) => {
470- if ( myGen !== this . _openGen ) return
471- if ( ! this . _gotData && ! this . _closed ) {
472- console . warn ( `[ExchangeWs] ${ this . _exchangeId } connected but no data received` )
473- this . _ws . onclose = null
474- try { this . _ws . close ( ) } catch ( _ ) { }
475- this . _ws = null
476- this . _tryFallback ( )
477- }
478- } , 12000 )
479- }
480-
481- if ( wasReconnect && this . _onReconnected ) {
482- this . _onReconnected ( )
483- }
466+ this . _dataTimeout = setTimeout ( ( ) => {
467+ if ( myGen !== this . _openGen ) return
468+ if ( ! this . _gotData && ! this . _closed ) {
469+ console . warn ( `[ExchangeWs] ${ this . _exchangeId } connected but no data received` )
470+ this . _tryFallback ( )
471+ }
472+ } , 12000 )
484473 }
485474
486475 this . _ws . onmessage = ( evt ) => {
@@ -491,30 +480,30 @@ export default class ExchangeKlineWs {
491480 this . _ws . onerror = ( ) => { }
492481
493482 this . _ws . onclose = ( ) => {
494- if ( myGen !== this . _openGen ) return
495- if ( this . _connectTimeout ) {
496- clearTimeout ( this . _connectTimeout )
497- this . _connectTimeout = null
498- }
499- this . _clearPing ( )
500- if ( ! this . _closed ) {
501- if ( ! this . _everConnected && ! this . _fallbackUsed ) {
502- this . _tryFallback ( )
503- } else {
504- if ( this . _everConnected && this . _onReconnecting ) {
505- this . _onReconnecting ( )
506- }
507- this . _scheduleReconnect ( )
508- }
509- }
483+ if ( myGen !== this . _openGen || this . _closed ) return
484+ this . _tryFallback ( )
510485 }
511486 }
512487
513- _tryFallback ( ) {
488+ _tryFallback ( retry = true ) {
489+ this . _openGen ++
514490 this . _fallbackUsed = true
515- this . _closed = true
516491 this . _clearTimers ( )
517- if ( this . _onError ) this . _onError ( )
492+ if ( this . _ws ) {
493+ this . _ws . onclose = null
494+ this . _ws . onmessage = null
495+ this . _ws . onerror = null
496+ this . _ws . onopen = null
497+ try { this . _ws . close ( ) } catch ( _ ) { }
498+ this . _ws = null
499+ }
500+ if ( ! retry ) {
501+ this . _closed = true
502+ if ( this . _onError ) this . _onError ( )
503+ return
504+ }
505+ if ( this . _onReconnecting ) this . _onReconnecting ( )
506+ this . _scheduleReconnect ( )
518507 }
519508
520509 async _decodeMessage ( raw ) {
@@ -540,10 +529,14 @@ export default class ExchangeKlineWs {
540529 return
541530 }
542531 if ( generation !== this . _openGen || this . _closed ) return
543- if ( raw === 'pong' || raw === '' ) return
532+ if ( raw === 'pong' ) { this . _awaitingPongAt = 0 ; return }
533+ if ( raw === '' ) return
544534
545535 let data
546536 try { data = JSON . parse ( raw ) } catch ( _ ) { return }
537+ if ( ! data || typeof data !== 'object' || Array . isArray ( data ) ) return
538+
539+ if ( String ( data . channel || '' ) . endsWith ( '.pong' ) ) { this . _awaitingPongAt = 0 ; return }
547540
548541 if ( data . ping !== undefined ) {
549542 try { this . _ws . send ( JSON . stringify ( { pong : data . ping } ) ) } catch ( _ ) { }
@@ -563,22 +556,26 @@ export default class ExchangeKlineWs {
563556 try { this . _ws . close ( ) } catch ( _ ) { }
564557 this . _ws = null
565558 }
566- this . _tryFallback ( )
559+ this . _tryFallback ( false )
567560 }
568561 return
569562 }
570- if ( data . event === 'pong' || data . ret_msg === 'pong' ) return
563+ if ( data . event === 'pong' || data . op === 'pong' || data . ret_msg === 'pong' ) { this . _awaitingPongAt = 0 ; return }
571564
572565 let parsed
573566 try {
574567 parsed = this . _exchangeConf . parseBar ( data , this . _context ( ) )
575568 } catch ( _ ) {
576569 return
577570 }
578- if ( ! parsed ) return
571+ if ( ! parsed || ! [ parsed . timestamp , parsed . open , parsed . high , parsed . low , parsed . close , parsed . volume ] . every ( Number . isFinite ) ||
572+ parsed . timestamp <= 0 || parsed . close <= 0 || parsed . volume < 0 ) return
579573
580574 if ( ! this . _gotData ) {
581575 this . _gotData = true
576+ this . _reconnectAttempts = 0
577+ if ( this . _fallbackUsed && this . _onReconnected ) this . _onReconnected ( )
578+ this . _fallbackUsed = false
582579 if ( this . _dataTimeout ) {
583580 clearTimeout ( this . _dataTimeout )
584581 this . _dataTimeout = null
@@ -607,11 +604,7 @@ export default class ExchangeKlineWs {
607604 _scheduleReconnect ( ) {
608605 if ( this . _closed ) return
609606 this . _reconnectAttempts ++
610- if ( this . _reconnectAttempts > this . _maxReconnectAttempts ) {
611- if ( this . _onError ) this . _onError ( )
612- return
613- }
614- const delay = Math . min ( 1000 * Math . pow ( 2 , this . _reconnectAttempts - 1 ) , 30000 )
607+ const delay = Math . min ( 5000 * Math . pow ( 2 , Math . min ( this . _reconnectAttempts - 1 , 4 ) ) , 60000 )
615608 this . _reconnectTimer = setTimeout ( ( ) => {
616609 this . _reconnectTimer = null
617610 this . _open ( )
@@ -624,12 +617,18 @@ export default class ExchangeKlineWs {
624617 if ( ! interval || typeof this . _exchangeConf . ping !== 'function' ) return
625618 this . _pingTimer = setInterval ( ( ) => {
626619 if ( this . _ws && this . _ws . readyState === WebSocket . OPEN ) {
627- this . _exchangeConf . ping ( this . _ws , this . _context ( ) )
620+ if ( this . _awaitingPongAt && Date . now ( ) - this . _awaitingPongAt > 45000 ) {
621+ this . _tryFallback ( )
622+ return
623+ }
624+ if ( ! this . _awaitingPongAt ) this . _awaitingPongAt = Date . now ( )
625+ try { this . _exchangeConf . ping ( this . _ws , this . _context ( ) ) } catch ( _ ) { this . _tryFallback ( ) }
628626 }
629627 } , interval )
630628 }
631629
632630 _clearPing ( ) {
631+ this . _awaitingPongAt = 0
633632 if ( this . _pingTimer ) {
634633 clearInterval ( this . _pingTimer )
635634 this . _pingTimer = null
0 commit comments