Skip to content

Instantly share code, notes, and snippets.

@JKorf
Created Feb 28, 2022
Embed
What would you like to do?
Binance socket response delay icw KeepAlive interval
using System.Diagnostics;
using System.Net.WebSockets;
using System.Text;
var socket = new ClientWebSocket();
socket.Options.KeepAliveInterval = TimeSpan.FromSeconds(15);
socket.Options.SetBuffer(65536, 65536); // Setting it to anything bigger than 65536 throws an exception in .net framework
await socket.ConnectAsync(new Uri("wss://stream.binance.com:9443/stream"), default);
var readThread = new Thread(async () =>
{
while (true)
{
var sw = Stopwatch.StartNew();
var result = new byte[1048576];
int index = 0;
while (true) {
var buffer = new byte[4096];
var rec = await socket.ReceiveAsync(buffer, default);
Array.Copy(buffer, 0, result, index, rec.Count);
index += rec.Count;
if (rec.EndOfMessage)
break;
}
var strData = Encoding.UTF8.GetString(new ArraySegment<byte>(result, 0, index));
Console.WriteLine($"Received data in {sw.ElapsedMilliseconds}: " + strData);
}
});
readThread.Start();
var data = "{\"method\":\"SUBSCRIBE\",\"params\":[\"ethbtc@kline_1m\",\"ethbtc@kline_3m\",\"ethbtc@kline_5m\",\"ethbtc@kline_15m\",\"ethbtc@kline_30m\",\"ethbtc@kline_1h\",\"ethbtc@kline_2h\",\"ethbtc@kline_4h\",\"ethbtc@kline_6h\",\"ethbtc@kline_8h\",\"ethbtc@kline_12h\",\"ethbtc@kline_1d\",\"ethbtc@kline_3d\",\"ethbtc@kline_1w\",\"ethbtc@kline_1M\",\"ltcbtc@kline_1m\",\"ltcbtc@kline_3m\",\"ltcbtc@kline_5m\",\"ltcbtc@kline_15m\",\"ltcbtc@kline_30m\",\"ltcbtc@kline_1h\",\"ltcbtc@kline_2h\",\"ltcbtc@kline_4h\",\"ltcbtc@kline_6h\",\"ltcbtc@kline_8h\",\"ltcbtc@kline_12h\",\"ltcbtc@kline_1d\",\"ltcbtc@kline_3d\",\"ltcbtc@kline_1w\",\"ltcbtc@kline_1M\",\"bnbbtc@kline_1m\",\"bnbbtc@kline_3m\",\"bnbbtc@kline_5m\",\"bnbbtc@kline_15m\",\"bnbbtc@kline_30m\",\"bnbbtc@kline_1h\",\"bnbbtc@kline_2h\",\"bnbbtc@kline_4h\",\"bnbbtc@kline_6h\",\"bnbbtc@kline_8h\",\"bnbbtc@kline_12h\",\"bnbbtc@kline_1d\",\"bnbbtc@kline_3d\",\"bnbbtc@kline_1w\",\"bnbbtc@kline_1M\",\"neobtc@kline_1m\",\"neobtc@kline_3m\",\"neobtc@kline_5m\",\"neobtc@kline_15m\",\"neobtc@kline_30m\",\"neobtc@kline_1h\",\"neobtc@kline_2h\",\"neobtc@kline_4h\",\"neobtc@kline_6h\",\"neobtc@kline_8h\",\"neobtc@kline_12h\",\"neobtc@kline_1d\",\"neobtc@kline_3d\",\"neobtc@kline_1w\",\"neobtc@kline_1M\",\"qtumeth@kline_1m\",\"qtumeth@kline_3m\",\"qtumeth@kline_5m\",\"qtumeth@kline_15m\",\"qtumeth@kline_30m\",\"qtumeth@kline_1h\",\"qtumeth@kline_2h\",\"qtumeth@kline_4h\",\"qtumeth@kline_6h\",\"qtumeth@kline_8h\",\"qtumeth@kline_12h\",\"qtumeth@kline_1d\",\"qtumeth@kline_3d\",\"qtumeth@kline_1w\",\"qtumeth@kline_1M\",\"eoseth@kline_1m\",\"eoseth@kline_3m\",\"eoseth@kline_5m\",\"eoseth@kline_15m\",\"eoseth@kline_30m\",\"eoseth@kline_1h\",\"eoseth@kline_2h\",\"eoseth@kline_4h\",\"eoseth@kline_6h\",\"eoseth@kline_8h\",\"eoseth@kline_12h\",\"eoseth@kline_1d\",\"eoseth@kline_3d\",\"eoseth@kline_1w\",\"eoseth@kline_1M\",\"snteth@kline_1m\",\"snteth@kline_3m\",\"snteth@kline_5m\",\"snteth@kline_15m\",\"snteth@kline_30m\",\"snteth@kline_1h\",\"snteth@kline_2h\",\"snteth@kline_4h\",\"snteth@kline_6h\",\"snteth@kline_8h\",\"snteth@kline_12h\",\"snteth@kline_1d\",\"snteth@kline_3d\",\"snteth@kline_1w\",\"snteth@kline_1M\",\"bnteth@kline_1m\",\"bnteth@kline_3m\",\"bnteth@kline_5m\",\"bnteth@kline_15m\",\"bnteth@kline_30m\",\"bnteth@kline_1h\",\"bnteth@kline_2h\",\"bnteth@kline_4h\",\"bnteth@kline_6h\",\"bnteth@kline_8h\",\"bnteth@kline_12h\",\"bnteth@kline_1d\",\"bnteth@kline_3d\",\"bnteth@kline_1w\",\"bnteth@kline_1M\",\"bccbtc@kline_1m\",\"bccbtc@kline_3m\",\"bccbtc@kline_5m\",\"bccbtc@kline_15m\",\"bccbtc@kline_30m\",\"bccbtc@kline_1h\",\"bccbtc@kline_2h\",\"bccbtc@kline_4h\",\"bccbtc@kline_6h\",\"bccbtc@kline_8h\",\"bccbtc@kline_12h\",\"bccbtc@kline_1d\",\"bccbtc@kline_3d\",\"bccbtc@kline_1w\",\"bccbtc@kline_1M\",\"gasbtc@kline_1m\",\"gasbtc@kline_3m\",\"gasbtc@kline_5m\",\"gasbtc@kline_15m\",\"gasbtc@kline_30m\",\"gasbtc@kline_1h\",\"gasbtc@kline_2h\",\"gasbtc@kline_4h\",\"gasbtc@kline_6h\",\"gasbtc@kline_8h\",\"gasbtc@kline_12h\",\"gasbtc@kline_1d\",\"gasbtc@kline_3d\",\"gasbtc@kline_1w\",\"gasbtc@kline_1M\",\"bnbeth@kline_1m\",\"bnbeth@kline_3m\",\"bnbeth@kline_5m\",\"bnbeth@kline_15m\",\"bnbeth@kline_30m\",\"bnbeth@kline_1h\",\"bnbeth@kline_2h\",\"bnbeth@kline_4h\",\"bnbeth@kline_6h\",\"bnbeth@kline_8h\",\"bnbeth@kline_12h\",\"bnbeth@kline_1d\",\"bnbeth@kline_3d\",\"bnbeth@kline_1w\",\"bnbeth@kline_1M\",\"btcusdt@kline_1m\",\"btcusdt@kline_3m\",\"btcusdt@kline_5m\",\"btcusdt@kline_15m\",\"btcusdt@kline_30m\",\"btcusdt@kline_1h\",\"btcusdt@kline_2h\",\"btcusdt@kline_4h\",\"btcusdt@kline_6h\",\"btcusdt@kline_8h\",\"btcusdt@kline_12h\",\"btcusdt@kline_1d\",\"btcusdt@kline_3d\",\"btcusdt@kline_1w\",\"btcusdt@kline_1M\",\"ethusdt@kline_1m\",\"ethusdt@kline_3m\",\"ethusdt@kline_5m\",\"ethusdt@kline_15m\",\"ethusdt@kline_30m\",\"ethusdt@kline_1h\",\"ethusdt@kline_2h\",\"ethusdt@kline_4h\",\"ethusdt@kline_6h\",\"ethusdt@kline_8h\",\"ethusdt@kline_12h\",\"ethusdt@kline_1d\",\"ethusdt@kline_3d\",\"ethusdt@kline_1w\",\"ethusdt@kline_1M\",\"hsrbtc@kline_1m\",\"hsrbtc@kline_3m\",\"hsrbtc@kline_5m\",\"hsrbtc@kline_15m\",\"hsrbtc@kline_30m\",\"hsrbtc@kline_1h\",\"hsrbtc@kline_2h\",\"hsrbtc@kline_4h\",\"hsrbtc@kline_6h\",\"hsrbtc@kline_8h\",\"hsrbtc@kline_12h\",\"hsrbtc@kline_1d\",\"hsrbtc@kline_3d\",\"hsrbtc@kline_1w\",\"hsrbtc@kline_1M\",\"oaxeth@kline_1m\",\"oaxeth@kline_3m\",\"oaxeth@kline_5m\",\"oaxeth@kline_15m\",\"oaxeth@kline_30m\",\"oaxeth@kline_1h\",\"oaxeth@kline_2h\",\"oaxeth@kline_4h\",\"oaxeth@kline_6h\",\"oaxeth@kline_8h\",\"oaxeth@kline_12h\",\"oaxeth@kline_1d\",\"oaxeth@kline_3d\",\"oaxeth@kline_1w\",\"oaxeth@kline_1M\",\"dnteth@kline_1m\",\"dnteth@kline_3m\",\"dnteth@kline_5m\",\"dnteth@kline_15m\",\"dnteth@kline_30m\",\"dnteth@kline_1h\",\"dnteth@kline_2h\",\"dnteth@kline_4h\",\"dnteth@kline_6h\",\"dnteth@kline_8h\",\"dnteth@kline_12h\",\"dnteth@kline_1d\",\"dnteth@kline_3d\",\"dnteth@kline_1w\",\"dnteth@kline_1M\",\"mcoeth@kline_1m\",\"mcoeth@kline_3m\",\"mcoeth@kline_5m\",\"mcoeth@kline_15m\",\"mcoeth@kline_30m\",\"mcoeth@kline_1h\",\"mcoeth@kline_2h\",\"mcoeth@kline_4h\",\"mcoeth@kline_6h\",\"mcoeth@kline_8h\",\"mcoeth@kline_12h\",\"mcoeth@kline_1d\",\"mcoeth@kline_3d\",\"mcoeth@kline_1w\",\"mcoeth@kline_1M\",\"icneth@kline_1m\",\"icneth@kline_3m\",\"icneth@kline_5m\",\"icneth@kline_15m\",\"icneth@kline_30m\",\"icneth@kline_1h\",\"icneth@kline_2h\",\"icneth@kline_4h\",\"icneth@kline_6h\",\"icneth@kline_8h\",\"icneth@kline_12h\",\"icneth@kline_1d\",\"icneth@kline_3d\",\"icneth@kline_1w\",\"icneth@kline_1M\",\"mcobtc@kline_1m\",\"mcobtc@kline_3m\",\"mcobtc@kline_5m\",\"mcobtc@kline_15m\",\"mcobtc@kline_30m\",\"mcobtc@kline_1h\",\"mcobtc@kline_2h\",\"mcobtc@kline_4h\",\"mcobtc@kline_6h\",\"mcobtc@kline_8h\",\"mcobtc@kline_12h\",\"mcobtc@kline_1d\",\"mcobtc@kline_3d\",\"mcobtc@kline_1w\",\"mcobtc@kline_1M\",\"wtcbtc@kline_1m\",\"wtcbtc@kline_3m\",\"wtcbtc@kline_5m\",\"wtcbtc@kline_15m\",\"wtcbtc@kline_30m\",\"wtcbtc@kline_1h\",\"wtcbtc@kline_2h\",\"wtcbtc@kline_4h\",\"wtcbtc@kline_6h\",\"wtcbtc@kline_8h\",\"wtcbtc@kline_12h\",\"wtcbtc@kline_1d\",\"wtcbtc@kline_3d\",\"wtcbtc@kline_1w\",\"wtcbtc@kline_1M\",\"wtceth@kline_1m\",\"wtceth@kline_3m\",\"wtceth@kline_5m\",\"wtceth@kline_15m\",\"wtceth@kline_30m\",\"wtceth@kline_1h\",\"wtceth@kline_2h\",\"wtceth@kline_4h\",\"wtceth@kline_6h\",\"wtceth@kline_8h\",\"wtceth@kline_12h\",\"wtceth@kline_1d\",\"wtceth@kline_3d\",\"wtceth@kline_1w\",\"wtceth@kline_1M\",\"lrcbtc@kline_1m\",\"lrcbtc@kline_3m\",\"lrcbtc@kline_5m\",\"lrcbtc@kline_15m\",\"lrcbtc@kline_30m\",\"lrcbtc@kline_1h\",\"lrcbtc@kline_2h\",\"lrcbtc@kline_4h\",\"lrcbtc@kline_6h\",\"lrcbtc@kline_8h\",\"lrcbtc@kline_12h\",\"lrcbtc@kline_1d\",\"lrcbtc@kline_3d\",\"lrcbtc@kline_1w\",\"lrcbtc@kline_1M\",\"lrceth@kline_1m\",\"lrceth@kline_3m\",\"lrceth@kline_5m\",\"lrceth@kline_15m\",\"lrceth@kline_30m\",\"lrceth@kline_1h\",\"lrceth@kline_2h\",\"lrceth@kline_4h\",\"lrceth@kline_6h\",\"lrceth@kline_8h\",\"lrceth@kline_12h\",\"lrceth@kline_1d\",\"lrceth@kline_3d\",\"lrceth@kline_1w\",\"lrceth@kline_1M\",\"qtumbtc@kline_1m\",\"qtumbtc@kline_3m\",\"qtumbtc@kline_5m\",\"qtumbtc@kline_15m\",\"qtumbtc@kline_30m\",\"qtumbtc@kline_1h\",\"qtumbtc@kline_2h\",\"qtumbtc@kline_4h\",\"qtumbtc@kline_6h\",\"qtumbtc@kline_8h\",\"qtumbtc@kline_12h\",\"qtumbtc@kline_1d\",\"qtumbtc@kline_3d\",\"qtumbtc@kline_1w\",\"qtumbtc@kline_1M\",\"yoyobtc@kline_1m\",\"yoyobtc@kline_3m\",\"yoyobtc@kline_5m\",\"yoyobtc@kline_15m\",\"yoyobtc@kline_30m\",\"yoyobtc@kline_1h\",\"yoyobtc@kline_2h\",\"yoyobtc@kline_4h\",\"yoyobtc@kline_6h\",\"yoyobtc@kline_8h\",\"yoyobtc@kline_12h\",\"yoyobtc@kline_1d\",\"yoyobtc@kline_3d\",\"yoyobtc@kline_1w\",\"yoyobtc@kline_1M\",\"omgbtc@kline_1m\",\"omgbtc@kline_3m\",\"omgbtc@kline_5m\",\"omgbtc@kline_15m\",\"omgbtc@kline_30m\",\"omgbtc@kline_1h\",\"omgbtc@kline_2h\",\"omgbtc@kline_4h\",\"omgbtc@kline_6h\",\"omgbtc@kline_8h\",\"omgbtc@kline_12h\",\"omgbtc@kline_1d\",\"omgbtc@kline_3d\",\"omgbtc@kline_1w\",\"omgbtc@kline_1M\",\"omgeth@kline_1m\",\"omgeth@kline_3m\",\"omgeth@kline_5m\",\"omgeth@kline_15m\",\"omgeth@kline_30m\",\"omgeth@kline_1h\",\"omgeth@kline_2h\",\"omgeth@kline_4h\",\"omgeth@kline_6h\",\"omgeth@kline_8h\",\"omgeth@kline_12h\",\"omgeth@kline_1d\",\"omgeth@kline_3d\",\"omgeth@kline_1w\",\"omgeth@kline_1M\",\"zrxbtc@kline_1m\",\"zrxbtc@kline_3m\",\"zrxbtc@kline_5m\",\"zrxbtc@kline_15m\",\"zrxbtc@kline_30m\",\"zrxbtc@kline_1h\",\"zrxbtc@kline_2h\",\"zrxbtc@kline_4h\",\"zrxbtc@kline_6h\",\"zrxbtc@kline_8h\",\"zrxbtc@kline_12h\",\"zrxbtc@kline_1d\",\"zrxbtc@kline_3d\",\"zrxbtc@kline_1w\",\"zrxbtc@kline_1M\",\"zrxeth@kline_1m\",\"zrxeth@kline_3m\",\"zrxeth@kline_5m\",\"zrxeth@kline_15m\",\"zrxeth@kline_30m\",\"zrxeth@kline_1h\",\"zrxeth@kline_2h\",\"zrxeth@kline_4h\",\"zrxeth@kline_6h\",\"zrxeth@kline_8h\",\"zrxeth@kline_12h\",\"zrxeth@kline_1d\",\"zrxeth@kline_3d\",\"zrxeth@kline_1w\",\"zrxeth@kline_1M\",\"stratbtc@kline_1m\",\"stratbtc@kline_3m\",\"stratbtc@kline_5m\",\"stratbtc@kline_15m\",\"stratbtc@kline_30m\",\"stratbtc@kline_1h\",\"stratbtc@kline_2h\",\"stratbtc@kline_4h\",\"stratbtc@kline_6h\",\"stratbtc@kline_8h\",\"stratbtc@kline_12h\",\"stratbtc@kline_1d\",\"stratbtc@kline_3d\",\"stratbtc@kline_1w\",\"stratbtc@kline_1M\"],\"id\":2}";
var bytes = Encoding.UTF8.GetBytes(data);
Console.WriteLine($"Sending {bytes.Length}");
await socket.SendAsync(bytes, WebSocketMessageType.Text, true, default);
Console.WriteLine($"Send done");
Console.ReadLine();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment