Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get data from GDAX web socket feed

Tags:

c#

websocket

I want to get data from this wss://ws-feed.gdax.com

I don't know anything about websocket. I am reading some tutorials and it uses terms like websocket server, TCP, etc which I have no idea about. Can anyone please advice how should I proceed, how to write a c# code to get data from the above.

This is the document which I reading to fetch real time data - https://docs.gdax.com/#websocket-feed

Started by creating a window app. Read here that the WebSocketSharp library can be used to connect WebSockets so installed it and so far written this code:

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.
        }

Any help would be much appreciated.

like image 611
user1254053 Avatar asked Jun 01 '17 08:06

user1254053


1 Answers

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();
like image 168
user2062445 Avatar answered Nov 14 '22 21:11

user2062445