@@ -86,8 +86,7 @@ module.exports = function() {
8686 symbol : symbol ,
8787 side : side ,
8888 type : "LIMIT" ,
89- quantity : quantity ,
90- recvWindow : 60000
89+ quantity : quantity
9190 } ;
9291 if ( typeof flags . type !== "undefined" ) opt . type = flags . type ;
9392 if ( opt . type == "LIMIT" ) {
@@ -105,13 +104,14 @@ module.exports = function() {
105104 } , "POST" ) ;
106105 } ;
107106 ////////////////////////////
108- const subscribe = function ( endpoint , callback ) {
107+ const subscribe = function ( endpoint , callback , reconnect = false ) {
109108 const ws = new WebSocket ( websocket_base + endpoint ) ;
110109 ws . on ( 'open' , function ( ) {
111110 //console.log("subscribe("+endpoint+")");
112111 } ) ;
113112 ws . on ( 'close' , function ( ) {
114113 console . log ( "WebSocket connection closed" ) ;
114+ if ( reconnect ) reconnect ( ) ;
115115 } ) ;
116116
117117 ws . on ( 'message' , function ( data ) {
@@ -300,8 +300,8 @@ module.exports = function() {
300300 return Math . max . apply ( Math , Object . keys ( object ) ) ;
301301 } ,
302302 options : function ( opt ) {
303- if ( typeof opt . recvWindow == "undefined" ) opt . recvWindow = 16000 ;
304303 options = opt ;
304+ if ( typeof options . recvWindow == "undefined" ) options . recvWindow = 60000 ;
305305 } ,
306306 buy : function ( symbol , quantity , price , flags = { } , callback = false ) {
307307 order ( "BUY" , symbol , quantity , price , flags , callback ) ;
@@ -371,7 +371,9 @@ module.exports = function() {
371371 return { open :open , high :high , low :low , close :close , volume :volume } ;
372372 } ,
373373 candlesticks : function ( symbol , interval = "5m" , callback ) { //1m,3m,5m,15m,30m,1h,2h,4h,6h,8h,12h,1d,3d,1w,1M
374- publicRequest ( base + "v1/klines" , { symbol :symbol , interval :interval } , callback ) ;
374+ publicRequest ( base + "v1/klines" , { symbol :symbol , interval :interval } , function ( data ) {
375+ return callback . call ( this , data , symbol ) ;
376+ } ) ;
375377 } ,
376378 publicRequest : function ( url , data , callback , method = "GET" ) {
377379 publicRequest ( url , data , callback , method )
@@ -380,43 +382,49 @@ module.exports = function() {
380382 signedRequest ( url , data , callback , method ) ;
381383 } ,
382384 websockets : {
383- userData : function ( callback , execution_callback = null ) {
385+ userData : function userData ( callback , execution_callback = null ) {
386+ let reconnect = function ( ) {
387+ userData ( callback , execution_callback ) ;
388+ } ;
384389 apiRequest ( base + "v1/userDataStream" , function ( response ) {
385390 options . listenKey = response . listenKey ;
386391 setInterval ( function ( ) { // keepalive
387392 apiRequest ( base + "v1/userDataStream" , false , "PUT" ) ;
388- } , 30000 ) ;
393+ } , 60000 ) ;
389394 if ( typeof execution_callback == "function" ) {
390395 options . balance_callback = callback ;
391396 options . execution_callback = execution_callback ;
392- subscribe ( options . listenKey , userDataHandler ) ;
397+ subscribe ( options . listenKey , userDataHandler , reconnect ) ;
393398 return ;
394399 }
395- subscribe ( options . listenKey , callback ) ;
400+ subscribe ( options . listenKey , callback , reconnect ) ;
396401 } , "POST" ) ;
397402 } ,
398- subscribe : function ( url , callback ) {
399- subscribe ( url , callback ) ;
403+ subscribe : function ( url , callback , reconnect = false ) {
404+ subscribe ( url , callback , reconnect ) ;
400405 } ,
401- depth : function ( symbols , callback ) {
406+ depth : function depth ( symbols , callback ) {
402407 for ( let symbol of symbols ) {
403408 subscribe ( symbol . toLowerCase ( ) + "@depth" , callback ) ;
404409 }
405410 } ,
406- depthCache : function ( symbols , callback ) {
411+ depthCache : function depthCacheFunction ( symbols , callback ) {
407412 for ( let symbol of symbols ) {
408413 if ( typeof info [ symbol ] == "undefined" ) info [ symbol ] = { } ;
409414 info [ symbol ] . firstUpdateId = 0 ;
410415 depthCache [ symbol ] = { bids : { } , asks : { } } ;
411416 messageQueue [ symbol ] = [ ] ;
417+ let reconnect = function ( ) {
418+ depthCacheFunction ( symbols , callback ) ;
419+ } ;
412420 subscribe ( symbol . toLowerCase ( ) + "@depth" , function ( depth ) {
413421 if ( ! info [ symbol ] . firstUpdateId ) {
414422 messageQueue [ symbol ] . push ( depth ) ;
415423 return ;
416424 }
417425 depthHandler ( depth ) ;
418426 if ( callback ) callback ( symbol , depthCache [ symbol ] ) ;
419- } ) ;
427+ } , reconnect ) ;
420428 publicRequest ( base + "v1/depth" , { symbol :symbol } , function ( json ) {
421429 info [ symbol ] . firstUpdateId = json . lastUpdateId ;
422430 depthCache [ symbol ] = depthData ( json ) ;
@@ -433,7 +441,8 @@ module.exports = function() {
433441 subscribe ( symbol . toLowerCase ( ) + "@aggTrade" , callback ) ;
434442 }
435443 } ,
436- chart : function ( symbols , interval , callback ) {
444+ chart : function chart ( symbols , interval , callback ) {
445+ if ( typeof symbols == "string" ) symbols = [ symbols ] ; // accept both strings and arrays
437446 for ( let symbol of symbols ) {
438447 if ( typeof info [ symbol ] == "undefined" ) info [ symbol ] = { } ;
439448 if ( typeof info [ symbol ] [ interval ] == "undefined" ) info [ symbol ] [ interval ] = { } ;
@@ -444,6 +453,9 @@ module.exports = function() {
444453 if ( typeof klineQueue [ symbol ] == "undefined" ) klineQueue [ symbol ] = { } ;
445454 if ( typeof klineQueue [ symbol ] [ interval ] == "undefined" ) klineQueue [ symbol ] [ interval ] = [ ] ;
446455 info [ symbol ] [ interval ] . timestamp = 0 ;
456+ let reconnect = function ( ) {
457+ chart ( symbols , interval , callback ) ;
458+ } ;
447459 subscribe ( symbol . toLowerCase ( ) + "@kline_" + interval , function ( kline ) {
448460 if ( ! info [ symbol ] [ interval ] . timestamp ) {
449461 klineQueue [ symbol ] [ interval ] . push ( kline ) ;
@@ -452,7 +464,7 @@ module.exports = function() {
452464 //console.log("@klines at " + kline.k.t);
453465 klineHandler ( symbol , kline ) ;
454466 if ( callback ) callback ( symbol , interval , klineConcat ( symbol , interval ) ) ;
455- } ) ;
467+ } , reconnect ) ;
456468 publicRequest ( base + "v1/klines" , { symbol :symbol , interval :interval } , function ( data ) {
457469 klineData ( symbol , interval , data ) ;
458470 //console.log("/klines at " + info[symbol][interval].timestamp);
@@ -464,12 +476,14 @@ module.exports = function() {
464476 } ) ;
465477 }
466478 } ,
467- candlesticks : function ( symbols , interval , callback ) {
479+ candlesticks : function candlesticks ( symbols , interval , callback ) {
480+ let reconnect = function ( ) {
481+ candlesticks ( symbols , interval , callback ) ;
482+ } ;
468483 for ( let symbol of symbols ) {
469- subscribe ( symbol . toLowerCase ( ) + "@kline_" + interval , callback ) ;
484+ subscribe ( symbol . toLowerCase ( ) + "@kline_" + interval , callback , reconnect ) ;
470485 }
471486 }
472- // deposit withdraw depositHistory withdrawHistory
473487 }
474488 } ;
475489} ( ) ;
0 commit comments