gpt4 book ai didi

c# - 从 GDAX 网络套接字提要获取数据

转载 作者:太空狗 更新时间:2023-10-29 23:12:42 27 4
gpt4 key购买 nike

我想从这个wss://ws-feed.gdax.com获取数据

我对 websocket 一无所知。我正在阅读一些教程,它使用了我不知道的 websocket 服务器、TCP 等术语。任何人都可以建议我应该如何进行,如何编写 c# 代码以从上面获取数据。

这是我阅读以获取实时数据的文档 - https://docs.gdax.com/#websocket-feed

首先创建一个窗口应用程序。在此处阅读 WebSocketSharp 库可用于连接 WebSocket,因此安装了它并编写了这段代码:

using (var ws = new WebSocket("wss://ws-feed.gdax.com"))
{
ws.Connect();
string json = "{\"type\": \"subscribe\", \"product_ids\": [\"BTC-USD\"]}";
ws.Send(json); //gives error -Function evaluation disabled because a previous function evaluation timed out. You must continue execution to reenable function evaluation.
}

如有任何帮助,我们将不胜感激。

最佳答案

ClientWebSocket socket = new ClientWebSocket();
Task task = socket.ConnectAsync(new Uri("wss://ws-feed.gdax.com"), CancellationToken.None);
task.Wait();
Thread readThread = new Thread(
delegate(object obj)
{
byte[] recBytes = new byte[1024];
while (true) {
ArraySegment<byte> t = new ArraySegment<byte>(recBytes);
Task<WebSocketReceiveResult> receiveAsync = socket.ReceiveAsync(t, CancellationToken.None);
receiveAsync.Wait();
string jsonString = Encoding.UTF8.GetString(recBytes);
Console.Out.WriteLine("jsonString = {0}", jsonString);
recBytes = new byte[1024];
}

});
readThread.Start();
string json = "{\"product_ids\":[\"btc-usd\"],\"type\":\"subscribe\"}";
byte[] bytes = Encoding.UTF8.GetBytes(json);
ArraySegment<byte> subscriptionMessageBuffer = new ArraySegment<byte>(bytes);
socket.SendAsync(subscriptionMessageBuffer, WebSocketMessageType.Text, true, CancellationToken.None);
Console.ReadLine();

关于c# - 从 GDAX 网络套接字提要获取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44302022/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com