Skip to content

Commit 8163837

Browse files
authored
Merge pull request #936 from ccxt/bookticker-fix
fix(client): bookTickers stream
2 parents 821fa7a + 99803ba commit 8163837

File tree

2 files changed

+20
-12
lines changed

2 files changed

+20
-12
lines changed

src/node-binance-api.ts

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1210,7 +1210,7 @@ export default class Binance {
12101210
subscribe(endpoint: string, callback: Callback, reconnect?: Callback, opened_callback?: Callback) {
12111211
const httpsproxy = this.getHttpsProxy();
12121212
let socksproxy = this.getSocksProxy();
1213-
let ws: any = undefined;
1213+
let ws: WebSocket = undefined;
12141214

12151215
if (socksproxy) {
12161216
socksproxy = this.proxyReplacewithIp(socksproxy);
@@ -1231,17 +1231,17 @@ export default class Binance {
12311231
}
12321232

12331233
if (this.Options.verbose) this.Options.log('Subscribed to ' + endpoint);
1234-
ws.reconnect = this.Options.reconnect;
1235-
ws.endpoint = endpoint;
1236-
ws.isAlive = false;
1234+
(ws as any).reconnect = this.Options.reconnect;
1235+
(ws as any).endpoint = endpoint;
1236+
(ws as any).isAlive = false;
12371237
ws.on('open', this.handleSocketOpen.bind(this, ws, opened_callback));
12381238
ws.on('pong', this.handleSocketHeartbeat.bind(this, ws));
12391239
ws.on('error', this.handleSocketError.bind(this, ws));
12401240
ws.on('close', this.handleSocketClose.bind(this, ws, reconnect));
12411241
ws.on('message', data => {
12421242
try {
12431243
if (this.Options.verbose) this.Options.log('WebSocket data:', data);
1244-
callback(JSONbig.parse(data));
1244+
callback(JSONbig.parse(data as any));
12451245
} catch (error) {
12461246
this.Options.log('Parse error: ' + error.message);
12471247
}
@@ -5544,7 +5544,7 @@ export default class Binance {
55445544
this.Options.balance_callback = balance_callback;
55455545
this.Options.execution_callback = execution_callback ? execution_callback : balance_callback;//This change is required to listen for Orders
55465546
this.Options.list_status_callback = list_status_callback;
5547-
const subscription = this.subscribe(this.Options.listenKey, this.userDataHandler.bind(this), reconnect);
5547+
const subscription = this.subscribe(this.Options.listenKey, this.userDataHandler.bind(this), reconnect) as any;
55485548
if (subscribed_callback) subscribed_callback(subscription.endpoint);
55495549
});
55505550
}
@@ -5580,7 +5580,7 @@ export default class Binance {
55805580
this.Options.margin_balance_callback = balance_callback;
55815581
this.Options.margin_execution_callback = execution_callback;
55825582
this.Options.margin_list_status_callback = list_status_callback;
5583-
const subscription = this.subscribe(this.Options.listenMarginKey, this.userMarginDataHandler.bind(this), reconnect);
5583+
const subscription = this.subscribe(this.Options.listenMarginKey, this.userMarginDataHandler.bind(this), reconnect) as any;
55845584
if (subscribed_callback) subscribed_callback(subscription.endpoint);
55855585
});
55865586
}
@@ -6106,16 +6106,24 @@ export default class Binance {
61066106

61076107
/**
61086108
* Spot WebSocket bookTicker (bid/ask quotes including price & amount)
6109-
* @param {symbol} symbol name or false. can also be a callback
6109+
* @param {string | string[]} symbol symbol or array of symbols
61106110
* @param {function} callback - callback function
61116111
* @return {string} the websocket endpoint
61126112
*/
6113-
bookTickersStream(symbol?: string, callback = console.log) {
6113+
bookTickersStream(symbol: string | string[], callback = console.log) {
61146114
const reconnect = () => {
61156115
if (this.Options.reconnect) this.bookTickersStream(symbol, callback);
61166116
};
6117-
const endpoint = symbol ? `${symbol.toLowerCase()}@bookTicker` : '!bookTicker';
6118-
const subscription = this.subscribe(endpoint, data => callback(this.fBookTickerConvertData(data)), reconnect);
6117+
let subscription: any;
6118+
if (Array.isArray(symbol)) {
6119+
const streams = symbol.map(function (symbol) {
6120+
return symbol.toLowerCase() + '@bookTicker';
6121+
});
6122+
subscription = this.subscribeCombined(streams, data => callback(this.fBookTickerConvertData(data)), reconnect);
6123+
} else {
6124+
const endpoint = `${(symbol as string).toLowerCase()}@bookTicker`;
6125+
subscription = this.subscribe(endpoint, data => callback(this.fBookTickerConvertData(data)), reconnect);
6126+
}
61196127
return (subscription as any).url;
61206128
}
61216129

src/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ export interface IWebsocketsMethods {
187187
chart(symbols: string | string[], interval: Interval, callback?: Callback, limit?: number)
188188
candlesticks(symbols: string | string[], interval: Interval, callback: Callback)
189189
miniTicker(callback: Callback): string;
190-
bookTickers(symbol: string, callback: Callback): string;
190+
bookTickers(symbol: string | string[], callback?: Callback): string;
191191
prevDay(symbols: string | string[] | undefined, callback?: Callback, singleCallback?: Callback)
192192
futuresCandlesticks(symbols: string[] | string, interval: Interval, callback: Callback)
193193
futuresTicker(symbol?: string, callback?: Callback)

0 commit comments

Comments
 (0)