Last active
May 23, 2022 19:26
-
-
Save JKorf/0c7789dce19e3639ece83db737c227a0 to your computer and use it in GitHub Desktop.
Bybit socket keepalive interval sending incrementally more messages, resulting in increasing network usage even though the actual data being received is very limited
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Diagnostics; | |
using System.Net.WebSockets; | |
using System.Text; | |
var socket = new ClientWebSocket(); | |
socket.Options.KeepAliveInterval = TimeSpan.FromSeconds(1); | |
socket.Options.SetBuffer(65536, 65536); | |
await socket.ConnectAsync(new Uri("wss://stream.bybit.com/spot/quote/ws/v2"), 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 = "{\"topic\":\"realtimes\",\"params\":{\"symbol\":\"BTCUSDT\"},\"event\":\"sub\"}"; | |
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