gpt4 book ai didi

c# - ClientWebSocket “The remote party closed the WebSocket connection without completing the close handshake.”

转载 作者:行者123 更新时间:2023-12-03 11:51:35 52 4
gpt4 key购买 nike

我正在尝试实现WebSocket,但是每次尝试阅读它时,都会引发“远程方在未完成关闭握手的情况下关闭了WebSocket连接”。错误。
这是我的代码:

string url = "wss://ws.url.com/v1/func/func2";
var param = new Dictionary<string, string>()
{
{ "apikey", "apikey" }
};
Uri uri = new Uri(QueryHelpers.AddQueryString(url, param));
CancellationToken token = CancellationToken.None;

using (var client = new ClientWebSocket() { Options = { KeepAliveInterval = new TimeSpan(0, 0, 3, 0) } })
{
client.ConnectAsync(uri, token).Wait();
Subscribe(client, token).Wait();
}

public static async Task Subscribe(ClientWebSocket client, CancellationToken token)
{
StringBuilder message = new StringBuilder("{\"action\":\"subscribe\",\"params\":{\"param\":\"value\"}}");
Console.WriteLine("Subscribe : " + message.ToString());

var sendBuffer = new ArraySegment<byte>(Encoding.UTF8.GetBytes(message.ToString()));
await client.SendAsync(sendBuffer, WebSocketMessageType.Text, true, token);

ArraySegment<byte> receivedBytes = new ArraySegment<byte>(new byte[1024]);
WebSocketReceiveResult result = await client.ReceiveAsync(receivedBytes, token);
Console.WriteLine(Encoding.UTF8.GetString(receivedBytes.Array, 0, result.Count));
}
该连接一直保持打开状态,直到ReadAsync终止,然后我收到“远程方未完成关闭握手就关闭了WebSocket连接”的消息。错误。我搜寻了互联网,但找不到解决方案。订阅后我应该会收到一条消息,但是它不起作用,并且看起来互联网对此了解很多。
WebSocket服务器是非常流行的TwelveData金融API,因此可以正常使用。
由于异常,我根本无法读取任何数据。

最佳答案

订阅仅限于主键。确保使用其中之一。

关于c# - ClientWebSocket “The remote party closed the WebSocket connection without completing the close handshake.”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64362357/

52 4 0