@@ -29,6 +29,7 @@ module.exports = function() {
29
29
url : url ,
30
30
qs : data ,
31
31
method : method ,
32
+ timeout : options . recvWindow ,
32
33
agent : false ,
33
34
headers : {
34
35
'User-Agent' : 'Mozilla/4.0 (compatible; Node Binance API)' ,
@@ -42,9 +43,11 @@ module.exports = function() {
42
43
} ;
43
44
44
45
const apiRequest = function ( url , callback , method = "GET" ) {
46
+ if ( ! options . APIKEY ) throw "apiRequest: Invalid API Key" ;
45
47
let opt = {
46
48
url : url ,
47
49
method : method ,
50
+ timeout : options . recvWindow ,
48
51
agent : false ,
49
52
headers : {
50
53
'User-Agent' : 'Mozilla/4.0 (compatible; Node Binance API)' ,
@@ -59,6 +62,7 @@ module.exports = function() {
59
62
} ;
60
63
61
64
const signedRequest = function ( url , data , callback , method = "GET" ) {
65
+ if ( ! options . APISECRET ) throw "signedRequest: Invalid API Secret" ;
62
66
if ( ! data ) data = { } ;
63
67
data . timestamp = new Date ( ) . getTime ( ) ;
64
68
if ( typeof data . symbol !== "undefined" ) data . symbol = data . symbol . replace ( '_' , '' ) ;
@@ -68,6 +72,7 @@ module.exports = function() {
68
72
let opt = {
69
73
url : url + '?' + query + '&signature=' + signature ,
70
74
method : method ,
75
+ timeout : options . recvWindow ,
71
76
agent : false ,
72
77
headers : {
73
78
'User-Agent' : 'Mozilla/4.0 (compatible; Node Binance API)' ,
@@ -106,16 +111,17 @@ module.exports = function() {
106
111
////////////////////////////
107
112
const subscribe = function ( endpoint , callback , reconnect = false ) {
108
113
const ws = new WebSocket ( websocket_base + endpoint ) ;
114
+ ws . endpoint = endpoint ;
109
115
ws . on ( 'open' , function ( ) {
110
- //console.log("subscribe("+endpoint+")");
116
+ //console.log("subscribe("+this. endpoint+")");
111
117
} ) ;
112
118
ws . on ( 'close' , function ( ) {
113
119
if ( reconnect ) {
114
- console . log ( "WebSocket reconnecting: " + endpoint ) ;
120
+ if ( this . endpoint && this . endpoint . length == 60 ) console . log ( "Account data WebSocket reconnecting.." ) ;
121
+ else console . log ( "WebSocket reconnecting: " + this . endpoint ) ;
115
122
reconnect ( ) ;
116
- } else console . log ( "WebSocket connection closed! " + endpoint ) ;
123
+ } else console . log ( "WebSocket connection closed! " + this . endpoint ) ;
117
124
} ) ;
118
-
119
125
ws . on ( 'message' , function ( data ) {
120
126
//console.log(data);
121
127
callback ( JSON . parse ( data ) ) ;
@@ -126,9 +132,9 @@ module.exports = function() {
126
132
if ( type == "outboundAccountInfo" ) {
127
133
options . balance_callback ( data ) ;
128
134
} else if ( type == "executionReport" ) {
129
- options . execution_callback ( data ) ;
135
+ if ( options . execution_callback ) options . execution_callback ( data ) ;
130
136
} else {
131
- console . log ( "Unexpected data : " + type ) ;
137
+ console . log ( "Unexpected userData : " + type ) ;
132
138
}
133
139
} ;
134
140
////////////////////////////
@@ -452,23 +458,26 @@ module.exports = function() {
452
458
signedRequest : function ( url , data , callback , method = "GET" ) {
453
459
signedRequest ( url , data , callback , method ) ;
454
460
} ,
461
+ getMarket : function ( symbol ) {
462
+ const substring = symbol . substr ( - 3 ) ;
463
+ if ( substring == "BTC" ) return "BTC" ;
464
+ else if ( substring == "ETH" ) return "ETH" ;
465
+ else if ( substring == "BNB" ) return "BNB" ;
466
+ else if ( symbol . substr ( - 4 ) == "USDT" ) return "USDT" ;
467
+ } ,
455
468
websockets : {
456
- userData : function userData ( callback , execution_callback = null ) {
469
+ userData : function userData ( callback , execution_callback = false ) {
457
470
let reconnect = function ( ) {
458
471
userData ( callback , execution_callback ) ;
459
472
} ;
460
473
apiRequest ( base + "v1/userDataStream" , function ( response ) {
461
474
options . listenKey = response . listenKey ;
462
475
setInterval ( function ( ) { // keepalive
463
476
apiRequest ( base + "v1/userDataStream" , false , "PUT" ) ;
464
- } , 60000 ) ;
465
- if ( typeof execution_callback == "function" ) {
466
- options . balance_callback = callback ;
467
- options . execution_callback = execution_callback ;
468
- subscribe ( options . listenKey , userDataHandler , reconnect ) ;
469
- return ;
470
- }
471
- subscribe ( options . listenKey , callback , reconnect ) ;
477
+ } , 30000 ) ;
478
+ options . balance_callback = callback ;
479
+ options . execution_callback = execution_callback ;
480
+ subscribe ( options . listenKey , userDataHandler , reconnect ) ;
472
481
} , "POST" ) ;
473
482
} ,
474
483
subscribe : function ( url , callback , reconnect = false ) {
0 commit comments