@@ -1543,12 +1543,14 @@ export default class Binance {
15431543 ws = new WebSocket ( this . getWsApiUrl ( ) ) ;
15441544 }
15451545
1546- if ( this . Options . verbose ) this . Options . log ( 'WebSocket API: Connected to ' + this . getWsApiUrl ( ) ) ;
15471546 ( ws as any ) . reconnect = this . Options . reconnect ;
15481547 ( ws as any ) . connectionId = connectionId ;
15491548 ( ws as any ) . isAlive = false ;
15501549
1551- ws . on ( 'open' , this . handleSocketOpen . bind ( this , ws , null ) ) ;
1550+ ws . on ( 'open' , ( ) => {
1551+ if ( this . Options . verbose ) this . Options . log ( 'WebSocket API: Connected to ' + this . getWsApiUrl ( ) ) ;
1552+ this . handleSocketOpen ( ws , null ) ;
1553+ } ) ;
15521554 ws . on ( 'pong' , this . handleSocketHeartbeat . bind ( this , ws ) ) ;
15531555 ws . on ( 'error' , this . handleSocketError . bind ( this , ws ) ) ;
15541556 ws . on ( 'close' , this . handleSocketClose . bind ( this , ws , reconnect ) ) ;
@@ -1560,7 +1562,6 @@ export default class Binance {
15601562 // Handle JSON-RPC responses
15611563 if ( message . id && this . wsApiPendingRequests [ message . id ] ) {
15621564 const pending = this . wsApiPendingRequests [ message . id ] ;
1563- delete this . wsApiPendingRequests [ message . id ] ;
15641565
15651566 if ( message . status === 200 ) {
15661567 pending . resolve ( message . result ) ;
@@ -1603,26 +1604,29 @@ export default class Binance {
16031604 params : params
16041605 } ;
16051606
1606- this . wsApiPendingRequests [ requestId ] = { resolve, reject } ;
1607+ const settle = ( fn : Function , value : any ) => {
1608+ const pending = this . wsApiPendingRequests [ requestId ] ;
1609+ if ( ! pending ) return ; // already settled
1610+ clearTimeout ( pending . timer ) ;
1611+ delete this . wsApiPendingRequests [ requestId ] ;
1612+ fn ( value ) ;
1613+ } ;
1614+
1615+ const timer = setTimeout ( ( ) => {
1616+ settle ( reject , new Error ( 'WebSocket API request timeout' ) ) ;
1617+ } , 30000 ) ;
1618+
1619+ this . wsApiPendingRequests [ requestId ] = { resolve : ( v : any ) => settle ( resolve , v ) , reject : ( e : any ) => settle ( reject , e ) , timer, connectionId } ;
16071620
16081621 if ( this . Options . verbose ) {
16091622 this . Options . log ( 'WebSocket API: Sending request:' , JSON . stringify ( request ) ) ;
16101623 }
16111624
16121625 ws . send ( JSON . stringify ( request ) , ( error ) => {
16131626 if ( error ) {
1614- delete this . wsApiPendingRequests [ requestId ] ;
1615- reject ( error ) ;
1627+ settle ( reject , error ) ;
16161628 }
16171629 } ) ;
1618-
1619- // Timeout after 30 seconds
1620- setTimeout ( ( ) => {
1621- if ( this . wsApiPendingRequests [ requestId ] ) {
1622- delete this . wsApiPendingRequests [ requestId ] ;
1623- reject ( new Error ( 'WebSocket API request timeout' ) ) ;
1624- }
1625- } , 30000 ) ;
16261630 } ) ;
16271631 }
16281632
@@ -1648,6 +1652,14 @@ export default class Binance {
16481652 ws . reconnect = reconnect ;
16491653 ws . terminate ( ) ;
16501654 delete this . wsApiConnections [ connectionId ] ;
1655+
1656+ // Reject all pending requests for this connection
1657+ for ( const requestId in this . wsApiPendingRequests ) {
1658+ const pending = this . wsApiPendingRequests [ requestId ] ;
1659+ if ( pending . connectionId === connectionId ) {
1660+ pending . reject ( new Error ( 'WebSocket API connection terminated' ) ) ;
1661+ }
1662+ }
16511663 }
16521664
16531665 /**
@@ -3872,13 +3884,20 @@ export default class Binance {
38723884 private ensureWsApiConnection ( connectionId : string ) : Promise < void > {
38733885 return new Promise ( ( resolve , reject ) => {
38743886 const existing = this . wsApiConnections [ connectionId ] ;
3875- if ( existing && existing . readyState === WebSocket . OPEN ) {
3876- resolve ( ) ;
3877- return ;
3887+ if ( existing ) {
3888+ if ( existing . readyState === WebSocket . OPEN ) {
3889+ resolve ( ) ;
3890+ return ;
3891+ }
3892+ if ( existing . readyState === WebSocket . CONNECTING ) {
3893+ existing . once ( 'open' , ( ) => resolve ( ) ) ;
3894+ existing . once ( 'error' , ( err : Error ) => reject ( err ) ) ;
3895+ return ;
3896+ }
38783897 }
38793898 const ws = this . connectWsApi ( connectionId , ( ) => { } , ( ) => { } ) ;
3880- ws . on ( 'open' , ( ) => resolve ( ) ) ;
3881- ws . on ( 'error' , ( err : Error ) => reject ( err ) ) ;
3899+ ws . once ( 'open' , ( ) => resolve ( ) ) ;
3900+ ws . once ( 'error' , ( err : Error ) => reject ( err ) ) ;
38823901 } ) ;
38833902 }
38843903
0 commit comments