I have a problem i would like to know how to read the WebSocket response with StreamReader ?
//WebSocket
WebSocket ws = new WebSocket("wss://stream.binance.com:9443/api/v3/userDataStream");
ws.Connect();
it's to do this: https://github.com/binance/binance-spot-api-docs/blob/master/user-data-stream.md#create-a-listenkey
I thank you in advance
I have found some example to receive data from a WebSocket
with a StreamReader
.
public async Task<string> Receive()
{
byte[] buffer = new byte[1024];
WebSocketReceiveResult result = await client.ReceiveAsync(new ArraySegment<byte>(buffer), CancellationToken.None);//ToDo built in CancellationToken
if (result.MessageType == WebSocketMessageType.Close)
{
return "abort";
}
using (MemoryStream stream = new MemoryStream())
{
stream.Write(buffer,0, result.Count);
while(!result.EndOfMessage)
{
result = await client.ReceiveAsync(new ArraySegment<byte>(buffer), CancellationToken.None);//ToDo built in CancellationToken
stream.Write(buffer, 0, result.Count);
}
stream.Seek(0, SeekOrigin.Begin);
using (StreamReader reader = new StreamReader(stream, Encoding.UTF8))
{
string message = reader.ReadToEnd();
return message;
}
}
}
Here is the link to the website for this code samples: https://mycsharp.de/forum/threads/122437/websocket-client-beispiel
There is another example to read and write from a websocket: https://thecodegarden.net/websocket-client-dotnet
I hope this can help you to solve your problem.