@@ -821,22 +821,25 @@ LIMIT_MAKER
821821 websockets : {
822822 userData : function userData ( callback , execution_callback = false , subscribed_callback = false ) {
823823 let reconnect = function ( ) {
824- if ( options . reconnect ) userData ( callback , execution_callback ) ;
824+ if ( options . reconnect ) userData ( callback , execution_callback , subscribed_callback ) ;
825825 } ;
826826 apiRequest ( base + 'v1/userDataStream' , function ( error , response ) {
827827 options . listenKey = response . listenKey ;
828- setInterval ( function ( ) { // keepalive
828+ setTimeout ( function userDataKeepAlive ( ) { // keepalive
829829 try {
830- apiRequest ( base + 'v1/userDataStream?listenKey=' + options . listenKey , false , 'PUT' ) ;
830+ apiRequest ( base + 'v1/userDataStream?listenKey=' + options . listenKey , function ( err , res ) {
831+ if ( err ) setTimeout ( userDataKeepAlive , 60000 ) ; // retry in 1 minute
832+ else setTimeout ( userDataKeepAlive , 60 * 30 * 1000 ) ; // 30 minute keepalive
833+ } , 'PUT' ) ;
831834 } catch ( error ) {
832- //error.message
835+ setTimeout ( userDataKeepAlive , 60000 ) ; // retry in 1 minute
833836 }
834837 } , 60 * 30 * 1000 ) ; // 30 minute keepalive
835838 options . balance_callback = callback ;
836839 options . execution_callback = execution_callback ;
837840 const subscription = subscribe ( options . listenKey , userDataHandler , reconnect ) ;
838841 if ( subscribed_callback ) subscribed_callback ( subscription . endpoint ) ;
839- } , 'POST' ) ;
842+ } , 'POST' ) ;
840843 } ,
841844 subscribe : function ( url , callback , reconnect = false ) {
842845 return subscribe ( url , callback , reconnect ) ;
0 commit comments