Skip to content

Instantly share code, notes, and snippets.

@buybackoff
Last active December 7, 2017 15:57
Show Gist options
  • Save buybackoff/434b71cbab9b41462e9899cc4baddae2 to your computer and use it in GitHub Desktop.
Save buybackoff/434b71cbab9b41462e9899cc4baddae2 to your computer and use it in GitHub Desktop.
GDAX WebSockets connection
using System.Net.WebSockets;
var wsc = new ClientWebSocket();
await wsc.ConnectAsync(new Uri("wss://ws-feed.gdax.com"), CancellationToken.None);
Console.WriteLine("Connected...");
var msg = @"{""type"": ""subscribe"", ""product_ids"": [""BTC-USD"", ""ETH-USD"", ""ETH-BTC""]}";
Console.WriteLine(msg);
await wsc.SendAsync(new ArraySegment<byte>(Encoding.UTF8.GetBytes(msg)), WebSocketMessageType.Text, true, CancellationToken.None);
var buffer = new ArraySegment<byte>(new byte[4096]);
while (true)
{
WebSocketReceiveResult result = await wsc.ReceiveAsync(buffer, CancellationToken.None);
if (result.CloseStatus.HasValue)
{
Console.WriteLine("Closed connection...");
break;
}
var str = Encoding.UTF8.GetString(buffer.Array, 0, result.Count);
Console.WriteLine(str);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment