Skip to content

Commit 65d390c

Browse files
author
Jon Eyrick
authored
recvWindow also sets timeout on signedRequest, etc
1 parent d4f06e7 commit 65d390c

File tree

1 file changed

+24
-15
lines changed

1 file changed

+24
-15
lines changed

node-binance-api.js

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ module.exports = function() {
2929
url: url,
3030
qs: data,
3131
method: method,
32+
timeout: options.recvWindow,
3233
agent: false,
3334
headers: {
3435
'User-Agent': 'Mozilla/4.0 (compatible; Node Binance API)',
@@ -42,9 +43,11 @@ module.exports = function() {
4243
};
4344

4445
const apiRequest = function(url, callback, method = "GET") {
46+
if ( !options.APIKEY ) throw "apiRequest: Invalid API Key";
4547
let opt = {
4648
url: url,
4749
method: method,
50+
timeout: options.recvWindow,
4851
agent: false,
4952
headers: {
5053
'User-Agent': 'Mozilla/4.0 (compatible; Node Binance API)',
@@ -59,6 +62,7 @@ module.exports = function() {
5962
};
6063

6164
const signedRequest = function(url, data, callback, method = "GET") {
65+
if ( !options.APISECRET ) throw "signedRequest: Invalid API Secret";
6266
if ( !data ) data = {};
6367
data.timestamp = new Date().getTime();
6468
if ( typeof data.symbol !== "undefined" ) data.symbol = data.symbol.replace('_','');
@@ -68,6 +72,7 @@ module.exports = function() {
6872
let opt = {
6973
url: url+'?'+query+'&signature='+signature,
7074
method: method,
75+
timeout: options.recvWindow,
7176
agent: false,
7277
headers: {
7378
'User-Agent': 'Mozilla/4.0 (compatible; Node Binance API)',
@@ -106,16 +111,17 @@ module.exports = function() {
106111
////////////////////////////
107112
const subscribe = function(endpoint, callback, reconnect = false) {
108113
const ws = new WebSocket(websocket_base+endpoint);
114+
ws.endpoint = endpoint;
109115
ws.on('open', function() {
110-
//console.log("subscribe("+endpoint+")");
116+
//console.log("subscribe("+this.endpoint+")");
111117
});
112118
ws.on('close', function() {
113119
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);
115122
reconnect();
116-
} else console.log("WebSocket connection closed! "+endpoint);
123+
} else console.log("WebSocket connection closed! "+this.endpoint);
117124
});
118-
119125
ws.on('message', function(data) {
120126
//console.log(data);
121127
callback(JSON.parse(data));
@@ -126,9 +132,9 @@ module.exports = function() {
126132
if ( type == "outboundAccountInfo" ) {
127133
options.balance_callback(data);
128134
} else if ( type == "executionReport" ) {
129-
options.execution_callback(data);
135+
if ( options.execution_callback ) options.execution_callback(data);
130136
} else {
131-
console.log("Unexpected data: "+type);
137+
console.log("Unexpected userData: "+type);
132138
}
133139
};
134140
////////////////////////////
@@ -452,23 +458,26 @@ module.exports = function() {
452458
signedRequest: function(url, data, callback, method = "GET") {
453459
signedRequest(url, data, callback, method);
454460
},
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+
},
455468
websockets: {
456-
userData: function userData(callback, execution_callback = null) {
469+
userData: function userData(callback, execution_callback = false) {
457470
let reconnect = function() {
458471
userData(callback, execution_callback);
459472
};
460473
apiRequest(base+"v1/userDataStream", function(response) {
461474
options.listenKey = response.listenKey;
462475
setInterval(function() { // keepalive
463476
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);
472481
},"POST");
473482
},
474483
subscribe: function(url, callback, reconnect = false) {

0 commit comments

Comments
 (0)