示例#1
0
        public async Task connect()
        {
            await socket.ConnectAsync();

            //https://docs.pro.coinbase.com/?r=1#protocol-overview
            // Request
            // Subscribe to ETH-USD and ETH-EUR with the level2, heartbeat and ticker channels,
            // plus receive the ticker entries for ETH-BTC and ETH-USD
            var sub = new Subscription
            {
                ProductIds =
                {
                    "BTC-USD",
                    "ETH-USD"
                },
                Channels =
                {
                    "level2",
                    "heartbeat",
                    JObject.FromObject(
                        new Channel
                    {
                        Name       = "ticker",
                        ProductIds = { "ETH-BTC","ETH-USD"                   }
                    })
                }
            };

            await socket.SubscribeAsync(sub);

            socket.RawSocket.MessageReceived += RawSocket_MessageReceived;

            await Task.Delay(TimeSpan.FromMinutes(1));
        }
示例#2
0
        private async Task Reconnect(Credentials creds, Subscription subscription)
        {
            if (this.cts.IsCancellationRequested)
            {
                return;
            }

            this.coinbase = new CoinbaseProWebSocket(new WebSocketConfig
            {
                ApiKey     = creds.ApiKey,
                Secret     = creds.ApiSecret,
                Passphrase = creds.ApiPassphrase,
                SocketUri  = "wss://ws-feed-public.sandbox.pro.coinbase.com"
            });

            WriteLine(">> Connecting websocket...");

            //Uncomment depending on your TFM if you want to debug the websocket
            //connection to Coinbase Pro with Fiddler
#if !NETFRAMEWORK
            coinbase.EnableFiddlerDebugProxy(new HttpConnectProxy(IPEndPoint.Parse("127.0.0.1:8888")));
#else
            coinbase.EnableFiddlerDebugProxy(new HttpConnectProxy(new IPEndPoint(IPAddress.Parse("127.0.0.1"), 8888)));
#endif

            var result = await coinbase.ConnectAsync();

            if (!result.Success)
            {
                var ex = new Exception("Connect failed.")
                {
                    Data = { { "ConnectResult", result } }
                };
                throw ex;
            }

            WriteLine(">> Connected.");

            coinbase.RawSocket.Closed          += Websocket_Closed;
            coinbase.RawSocket.Error           += Websocket_Error;
            coinbase.RawSocket.MessageReceived += Websocket_MessageReceived;

            WriteLine(">> Subscribing to events...");
            var sub = new Subscription
            {
                Channels   = subscription.Channels,
                ProductIds = subscription.ProductIds
            };
            await coinbase.SubscribeAsync(sub);

            WriteLine(">> Subscribed.");
        }
示例#3
0
        public static async Task SubscribeToWebsocketEvents(Credentials creds)
        {
            var socket = new CoinbaseProWebSocket(new WebSocketConfig
            {
                ApiKey     = creds.ApiKey,
                Secret     = creds.ApiSecret,
                Passphrase = creds.ApiPassphrase,
                SocketUri  = "wss://ws-feed-public.sandbox.pro.coinbase.com"
            });


            WriteLine(">> Connecting websocket...");

            //Uncomment depending on your TFM if you want to debug the websocket
            //connection to Coinbase Pro with Fiddler
#if !NETFRAMEWORK
            //socket.EnableFiddlerDebugProxy(new HttpConnectProxy(IPEndPoint.Parse("127.0.0.1:8888")));
#else
            //socket.EnableFiddlerDebugProxy(new HttpConnectProxy(new IPEndPoint(IPAddress.Parse("127.0.0.1"), 8888)));
#endif

            var result = await socket.ConnectAsync();

            if (!result.Success)
            {
                throw new Exception("Connect error.");
            }

            WriteLine(">> Connected.");

            socket.RawSocket.Closed          += Websocket_Closed;
            socket.RawSocket.Error           += Websocket_Error;
            socket.RawSocket.MessageReceived += Websocket_MessageReceived;

            var sub = new Subscription
            {
                ProductIds = { "BTC-USD" },
                Channels   = { "heartbeat" }
            };

            WriteLine(">> Subscribing to events...");
            await socket.SubscribeAsync(sub);

            WriteLine(">> Subscribed.");
        }
    async Task SubscribingToEvents(CoinbaseProWebSocket socket)
    {
        #region SubscribingToEvents

        //Using authenticated or unauthenticated instance `socket`
        //Connect the websocket,
        //when this connect method completes, the socket is ready or failure occured.
        var result = await socket.ConnectAsync();

        if (!result.Success)
        {
            throw new Exception("Failed to connect.");
        }

        //add an event handler for the message received event on the raw socket
        socket.RawSocket.MessageReceived += RawSocket_MessageReceived;

        //create a subscription of what to listen to
        var sub = new Subscription
        {
            ProductIds =
            {
                "BTC-USD",
            },
            Channels =
            {
                "heartbeat",
            }
        };

        //send the subscription upstream
        await socket.SubscribeAsync(sub);

        //now wait for data.
        await Task.Delay(TimeSpan.FromMinutes(1));

        #endregion
    }