@@ -86,8 +86,7 @@ module.exports = function() {
86
86
symbol : symbol ,
87
87
side : side ,
88
88
type : "LIMIT" ,
89
- quantity : quantity ,
90
- recvWindow : 60000
89
+ quantity : quantity
91
90
} ;
92
91
if ( typeof flags . type !== "undefined" ) opt . type = flags . type ;
93
92
if ( opt . type == "LIMIT" ) {
@@ -105,13 +104,14 @@ module.exports = function() {
105
104
} , "POST" ) ;
106
105
} ;
107
106
////////////////////////////
108
- const subscribe = function ( endpoint , callback ) {
107
+ const subscribe = function ( endpoint , callback , reconnect = false ) {
109
108
const ws = new WebSocket ( websocket_base + endpoint ) ;
110
109
ws . on ( 'open' , function ( ) {
111
110
//console.log("subscribe("+endpoint+")");
112
111
} ) ;
113
112
ws . on ( 'close' , function ( ) {
114
113
console . log ( "WebSocket connection closed" ) ;
114
+ if ( reconnect ) reconnect ( ) ;
115
115
} ) ;
116
116
117
117
ws . on ( 'message' , function ( data ) {
@@ -300,8 +300,8 @@ module.exports = function() {
300
300
return Math . max . apply ( Math , Object . keys ( object ) ) ;
301
301
} ,
302
302
options : function ( opt ) {
303
- if ( typeof opt . recvWindow == "undefined" ) opt . recvWindow = 16000 ;
304
303
options = opt ;
304
+ if ( typeof options . recvWindow == "undefined" ) options . recvWindow = 60000 ;
305
305
} ,
306
306
buy : function ( symbol , quantity , price , flags = { } , callback = false ) {
307
307
order ( "BUY" , symbol , quantity , price , flags , callback ) ;
@@ -371,7 +371,9 @@ module.exports = function() {
371
371
return { open :open , high :high , low :low , close :close , volume :volume } ;
372
372
} ,
373
373
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
+ } ) ;
375
377
} ,
376
378
publicRequest : function ( url , data , callback , method = "GET" ) {
377
379
publicRequest ( url , data , callback , method )
@@ -380,43 +382,49 @@ module.exports = function() {
380
382
signedRequest ( url , data , callback , method ) ;
381
383
} ,
382
384
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
+ } ;
384
389
apiRequest ( base + "v1/userDataStream" , function ( response ) {
385
390
options . listenKey = response . listenKey ;
386
391
setInterval ( function ( ) { // keepalive
387
392
apiRequest ( base + "v1/userDataStream" , false , "PUT" ) ;
388
- } , 30000 ) ;
393
+ } , 60000 ) ;
389
394
if ( typeof execution_callback == "function" ) {
390
395
options . balance_callback = callback ;
391
396
options . execution_callback = execution_callback ;
392
- subscribe ( options . listenKey , userDataHandler ) ;
397
+ subscribe ( options . listenKey , userDataHandler , reconnect ) ;
393
398
return ;
394
399
}
395
- subscribe ( options . listenKey , callback ) ;
400
+ subscribe ( options . listenKey , callback , reconnect ) ;
396
401
} , "POST" ) ;
397
402
} ,
398
- subscribe : function ( url , callback ) {
399
- subscribe ( url , callback ) ;
403
+ subscribe : function ( url , callback , reconnect = false ) {
404
+ subscribe ( url , callback , reconnect ) ;
400
405
} ,
401
- depth : function ( symbols , callback ) {
406
+ depth : function depth ( symbols , callback ) {
402
407
for ( let symbol of symbols ) {
403
408
subscribe ( symbol . toLowerCase ( ) + "@depth" , callback ) ;
404
409
}
405
410
} ,
406
- depthCache : function ( symbols , callback ) {
411
+ depthCache : function depthCacheFunction ( symbols , callback ) {
407
412
for ( let symbol of symbols ) {
408
413
if ( typeof info [ symbol ] == "undefined" ) info [ symbol ] = { } ;
409
414
info [ symbol ] . firstUpdateId = 0 ;
410
415
depthCache [ symbol ] = { bids : { } , asks : { } } ;
411
416
messageQueue [ symbol ] = [ ] ;
417
+ let reconnect = function ( ) {
418
+ depthCacheFunction ( symbols , callback ) ;
419
+ } ;
412
420
subscribe ( symbol . toLowerCase ( ) + "@depth" , function ( depth ) {
413
421
if ( ! info [ symbol ] . firstUpdateId ) {
414
422
messageQueue [ symbol ] . push ( depth ) ;
415
423
return ;
416
424
}
417
425
depthHandler ( depth ) ;
418
426
if ( callback ) callback ( symbol , depthCache [ symbol ] ) ;
419
- } ) ;
427
+ } , reconnect ) ;
420
428
publicRequest ( base + "v1/depth" , { symbol :symbol } , function ( json ) {
421
429
info [ symbol ] . firstUpdateId = json . lastUpdateId ;
422
430
depthCache [ symbol ] = depthData ( json ) ;
@@ -433,7 +441,8 @@ module.exports = function() {
433
441
subscribe ( symbol . toLowerCase ( ) + "@aggTrade" , callback ) ;
434
442
}
435
443
} ,
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
437
446
for ( let symbol of symbols ) {
438
447
if ( typeof info [ symbol ] == "undefined" ) info [ symbol ] = { } ;
439
448
if ( typeof info [ symbol ] [ interval ] == "undefined" ) info [ symbol ] [ interval ] = { } ;
@@ -444,6 +453,9 @@ module.exports = function() {
444
453
if ( typeof klineQueue [ symbol ] == "undefined" ) klineQueue [ symbol ] = { } ;
445
454
if ( typeof klineQueue [ symbol ] [ interval ] == "undefined" ) klineQueue [ symbol ] [ interval ] = [ ] ;
446
455
info [ symbol ] [ interval ] . timestamp = 0 ;
456
+ let reconnect = function ( ) {
457
+ chart ( symbols , interval , callback ) ;
458
+ } ;
447
459
subscribe ( symbol . toLowerCase ( ) + "@kline_" + interval , function ( kline ) {
448
460
if ( ! info [ symbol ] [ interval ] . timestamp ) {
449
461
klineQueue [ symbol ] [ interval ] . push ( kline ) ;
@@ -452,7 +464,7 @@ module.exports = function() {
452
464
//console.log("@klines at " + kline.k.t);
453
465
klineHandler ( symbol , kline ) ;
454
466
if ( callback ) callback ( symbol , interval , klineConcat ( symbol , interval ) ) ;
455
- } ) ;
467
+ } , reconnect ) ;
456
468
publicRequest ( base + "v1/klines" , { symbol :symbol , interval :interval } , function ( data ) {
457
469
klineData ( symbol , interval , data ) ;
458
470
//console.log("/klines at " + info[symbol][interval].timestamp);
@@ -464,12 +476,14 @@ module.exports = function() {
464
476
} ) ;
465
477
}
466
478
} ,
467
- candlesticks : function ( symbols , interval , callback ) {
479
+ candlesticks : function candlesticks ( symbols , interval , callback ) {
480
+ let reconnect = function ( ) {
481
+ candlesticks ( symbols , interval , callback ) ;
482
+ } ;
468
483
for ( let symbol of symbols ) {
469
- subscribe ( symbol . toLowerCase ( ) + "@kline_" + interval , callback ) ;
484
+ subscribe ( symbol . toLowerCase ( ) + "@kline_" + interval , callback , reconnect ) ;
470
485
}
471
486
}
472
- // deposit withdraw depositHistory withdrawHistory
473
487
}
474
488
} ;
475
489
} ( ) ;
0 commit comments