Skip to content

Commit 78b22c6

Browse files
author
Jon Eyrick
authored
WebSocket Reconnect
Also added cumulative depth cache, easier callbacks
1 parent b94b9fb commit 78b22c6

File tree

1 file changed

+21
-5
lines changed

1 file changed

+21
-5
lines changed

node-binance-api.js

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -110,8 +110,11 @@ module.exports = function() {
110110
//console.log("subscribe("+endpoint+")");
111111
});
112112
ws.on('close', function() {
113-
console.log("WebSocket connection closed");
114-
if ( reconnect ) reconnect();
113+
if ( reconnect ) {
114+
console.log("WebSocket reconnecting: "+endpoint);
115+
reconnect();
116+
}
117+
console.log("WebSocket connection closed! "+endpoint);
115118
});
116119

117120
ws.on('message', function(data) {
@@ -260,13 +263,22 @@ module.exports = function() {
260263
}
261264
return output;
262265
},
266+
array: function(obj) {
267+
return Object.keys(obj).map(function(key) {
268+
return [Number(key), obj[key]];
269+
});
270+
},
263271
sortBids: function(symbol, max = Infinity, baseValue = false) {
264272
let object = {}, count = 0, cache;
265273
if ( typeof symbol == "object" ) cache = symbol;
266274
else cache = getDepthCache(symbol).bids;
267275
let sorted = Object.keys(cache).sort(function(a, b){return parseFloat(b)-parseFloat(a)});
276+
let cumulative = 0;
268277
for ( let price of sorted ) {
269-
if ( !baseValue ) object[price] = cache[price];
278+
if ( baseValue == "cumulative" ) {
279+
cumulative+= cache[price];
280+
object[price] = cumulative.toFixed(8);
281+
} else if ( !baseValue ) object[price] = cache[price];
270282
else object[price] = parseFloat((cache[price] * parseFloat(price)).toFixed(8));
271283
if ( ++count > max ) break;
272284
}
@@ -277,8 +289,12 @@ module.exports = function() {
277289
if ( typeof symbol == "object" ) cache = symbol;
278290
else cache = getDepthCache(sparseFloatymbol).asks;
279291
let sorted = Object.keys(cache).sort(function(a, b){return parseFloat(a)-parseFloat(b)});
292+
let cumulative = 0;
280293
for ( let price of sorted ) {
281-
if ( !baseValue ) object[price] = cache[price];
294+
if ( baseValue == "cumulative" ) {
295+
cumulative+= cache[price];
296+
object[price] = cumulative.toFixed(8);
297+
} else if ( !baseValue ) object[price] = cache[price];
282298
else object[price] = parseFloat((cache[price] * parseFloat(price)).toFixed(8));
283299
if ( ++count > max ) break;
284300
}
@@ -329,7 +345,7 @@ module.exports = function() {
329345
},
330346
depth: function(symbol, callback) {
331347
publicRequest(base+"v1/depth", {symbol:symbol}, function(data) {
332-
return callback(depthData(data));
348+
return callback.call(this, depthData(data), symbol);
333349
});
334350
},
335351
prices: function(callback) {

0 commit comments

Comments
 (0)