从 GDAX web socket feed 获取数据
Posted
技术标签:
【中文标题】从 GDAX web socket feed 获取数据【英文标题】:Get data from GDAX web socket feed 【发布时间】:2017-06-01 08:11:53 【问题描述】:我想从这个 wss://ws-feed.gdax.com 获取数据
我对 websocket 一无所知。我正在阅读一些教程,它使用了我不知道的 websocket 服务器、TCP 等术语。任何人都可以请建议我应该如何进行,如何编写 c# 代码来从上面获取数据。
这是我阅读以获取实时数据的文档 - https://docs.gdax.com/#websocket-feed
首先创建一个窗口应用程序。在这里阅读WebSocketSharp库可以用来连接WebSockets所以安装它,到目前为止写了这段代码:
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.
任何帮助将不胜感激。
【问题讨论】:
【参考方案1】: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();
【讨论】:
【参考方案2】:基本上,您所做的是朝着正确的方向。只需在阅读消息之前检查 websocket 状态。
以下示例来自https://github.com/sefbkn/gdax.netcore,稍作修改
var webSocketClient = new ClientWebSocket();
var cancellationToken = new CancellationToken();
await webSocketClient.ConnectAsync(new Uri("wss://ws-feed.gdax.com"), cancellationToken).ConfigureAwait(false);
if (webSocketClient.State == WebSocketState.Open)
var requestString = JsonConvert.SerializeObject(new
type = "subscribe",
product_ids = new[]"ETH-EUR",
channels = new[]"ticker"
);
var requestBytes = UTF8Encoding.UTF8.GetBytes(requestString);
var subscribeRequest = new ArraySegment<byte>(requestBytes);
var sendCancellationToken = new CancellationToken();
await webSocketClient.SendAsync(subscribeRequest, WebSocketMessageType.Text, true, sendCancellationToken).ConfigureAwait(false);
while (webSocketClient.State == WebSocketState.Open)
var receiveCancellationToken = new CancellationToken();
using(var stream = new MemoryStream(1024))
var receiveBuffer = new ArraySegment<byte>(new byte[1024*8]);
WebSocketReceiveResult webSocketReceiveResult;
do
webSocketReceiveResult = await webSocketClient.ReceiveAsync(receiveBuffer, receiveCancellationToken).ConfigureAwait(false);
await stream.WriteAsync(receiveBuffer.Array, receiveBuffer.Offset, receiveBuffer.Count);
while(!webSocketReceiveResult.EndOfMessage);
var message = stream.ToArray().Where(b => b != 0).ToArray();
messageReceived(Encoding.ASCII.GetString(message, 0, message.Length));
【讨论】:
以上是关于从 GDAX web socket feed 获取数据的主要内容,如果未能解决你的问题,请参考以下文章
如何使用 Web 套接字从 GDAX 抓取比特币价格,以便实时更改价值?
使用带有webpack的socket.io连接到GDAX websocket api