Skip to content

Commit

Permalink
refactored binance symbol stream limit (#769)
Browse files Browse the repository at this point in the history
- to apply to all websocket streams
  • Loading branch information
vslee authored Apr 24, 2022
1 parent 0062139 commit b4fda04
Showing 1 changed file with 13 additions and 7 deletions.
20 changes: 13 additions & 7 deletions src/ExchangeSharp/API/Exchanges/BinanceGroup/BinanceGroupCommon.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ protected async Task<string> GetWebSocketStreamUrlForSymbolsAsync(string suffix,
{
marketSymbols = (await GetMarketSymbolsAsync()).ToArray();
}
if (marketSymbols.Length > 400)
{
marketSymbols = marketSymbols.Take(400).ToArray();
Logger.Warn("subscribing to the first 400 symbols"); // binance does not allow subscribing to more than 400 symbols at a time
}

StringBuilder streams = new StringBuilder("/stream?streams=");
for (int i = 0; i < marketSymbols.Length; i++)
Expand Down Expand Up @@ -234,9 +239,15 @@ protected override async Task<IEnumerable<KeyValuePair<string, ExchangeTicker>>>
return tickers;
}

protected override Task<IWebSocket> OnGetTickersWebSocketAsync(Action<IReadOnlyCollection<KeyValuePair<string, ExchangeTicker>>> callback, params string[] symbols)
protected override async Task<IWebSocket> OnGetTickersWebSocketAsync(Action<IReadOnlyCollection<KeyValuePair<string, ExchangeTicker>>> callback, params string[] symbols)
{
return ConnectPublicWebSocketAsync("/stream?streams=!ticker@arr", async (_socket, msg) =>
string url = null;
if (symbols == null || symbols.Length == 0)
{
url = "/stream?streams=!ticker@arr";
}
else url = await GetWebSocketStreamUrlForSymbolsAsync("@ticker", symbols);
return await ConnectPublicWebSocketAsync(url, async (_socket, msg) =>
{
JToken token = JToken.Parse(msg.ToStringFromUTF8());
List<KeyValuePair<string, ExchangeTicker>> tickerList = new List<KeyValuePair<string, ExchangeTicker>>();
Expand Down Expand Up @@ -275,11 +286,6 @@ protected override async Task<IWebSocket> OnGetTradesWebSocketAsync(Func<KeyValu
{
marketSymbols = (await GetMarketSymbolsAsync()).ToArray();
}
if (marketSymbols.Length > 400)
{
marketSymbols = marketSymbols.Take(400).ToArray();
Logger.Warn("subscribing to the first 400 symbols"); // binance does not allow subscribing to more than 400 symbols at a time
}
string url = await GetWebSocketStreamUrlForSymbolsAsync("@aggTrade", marketSymbols);
return await ConnectPublicWebSocketAsync(url, messageCallback: async (_socket, msg) =>
{
Expand Down

0 comments on commit b4fda04

Please sign in to comment.