[英]WebSocketSharp C# StreamReader?
I have a problem i would like to know how to read the WebSocket response with StreamReader?我有一个问题,我想知道如何使用 StreamReader 读取 WebSocket 响应?
//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这样做: 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
.我找到了一些示例来从带有
StreamReader
的WebSocket
接收数据。
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这是此代码示例的网站链接: 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还有另一个从 websocket 读写的例子: https://thecodegarden.net/websocket-client-dotnet
I hope this can help you to solve your problem.我希望这可以帮助您解决问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.