Ejemplo n.º 1
0
        public async Task <bool> SubscribeAsync(string[] Products, GDAX_Channel gdax_Channel = GDAX_Channel.full)
        {
            var productsToUnsubscribe       = new SortedList <GDAX_Channel, List <string> >();    // value is a list of products to unsubscribe from
            var productsToSubscribeFiltered = new List <string>();

            foreach (var product in Products)
            {
                if (subscribedProducts.ContainsKey(product) &&
                    subscribedProducts[product] != GDAX_Channel.full && gdax_Channel == GDAX_Channel.full)
                {                 // if currently subscribed to a non full channel, then unsubscribe
                    var currentlySubscribedChannel = subscribedProducts[product];
                    if (!productsToUnsubscribe.ContainsKey(currentlySubscribedChannel))
                    {
                        productsToUnsubscribe.Add(currentlySubscribedChannel, new List <string>());
                    }
                    productsToUnsubscribe[currentlySubscribedChannel].Add(product);
                    productsToSubscribeFiltered.Add(product);                     // these need to be subscribed to
                }

                if (!subscribedProducts.ContainsKey(product))
                {                 // these also need to be subscribed to since they're not subscribed
                    productsToSubscribeFiltered.Add(product);
                }
            }
            foreach (var kvp in productsToUnsubscribe)
            {             // unsubscribe
                await sendSubscriptionMsgAsync(Products : kvp.Value, gdax_Channel : kvp.Key, unSubscribe : true);
            }
            return(await sendSubscriptionMsgAsync(Products : Products, gdax_Channel : gdax_Channel, unSubscribe : false));
        }
Ejemplo n.º 2
0
 public RealtimeOrderBookSubscription(string[] Products, CBAuthenticationContainer auth = null,
                                      GDAX_Channel gdax_Channel = GDAX_Channel.full) : base(auth)
 { // + eventually can take an array of productStrings and subscribe simultaneously
     this.Products = Products;
     if (Products == null || Products.Length == 0)
     {
         throw new ArgumentNullException("Products");
     }
     this.gdax_Channel = gdax_Channel;
     ConnectionOpened += (product, channel) => subscribedProducts.Add(product, channel);
     ConnectionClosed += (product, channel) => subscribedProducts.Remove(product);
 }
Ejemplo n.º 3
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="Products"></param>
        /// <param name="gdax_Channel"></param>
        /// <returns>if submission of subscription request succeeded (not whether the subsciption actually succeeded)</returns>
        private async Task <bool> sendSubscriptionMsgAsync(IEnumerable <string> Products, GDAX_Channel gdax_Channel = GDAX_Channel.full, bool unSubscribe = false)
        {
            if (webSocketClient.State == System.Net.WebSockets.WebSocketState.Open)
            {
                foreach (var product in Products)
                {
                    if (String.IsNullOrWhiteSpace(product))
                    {
                        throw new ArgumentNullException("Products");
                    }
                }
                var productsString = Products.Aggregate((a, b) => a + "\", \"" + b);
                var subAction      = unSubscribe ? "unsubscribe" : "subscribe";
                // enough for unauthenticated feed
                string requestStringSubset = String.Format(
                    @"""type"": ""{0}"",""product_ids"": [""{1}""],""channels"": [""heartbeat"",""{2}""]",
                    subAction, productsString, gdax_Channel);
                string requestString;
                if (_authContainer == null)
                {                 // unauthenticated feed
                    requestString = String.Format(@"{{{0}}}", requestStringSubset);
                }
                else
                {                 // authenticated feed
                    var signBlock = _authContainer.ComputeSignature(relativeUrl: "/users/self/verify", method: "GET", body: "");
                    requestString = String.Format(
                        @"{{{0},""signature"": ""{1}"",""key"": ""{2}"",""passphrase"": ""{3}"",""timestamp"": ""{4}""}}",
                        requestStringSubset, signBlock.Signature, signBlock.ApiKey, signBlock.Passphrase, signBlock.TimeStamp);
                }
                var requestBytes     = UTF8Encoding.UTF8.GetBytes(requestString);
                var subscribeRequest = new ArraySegment <byte>(requestBytes);
                await webSocketClient.SendAsync(subscribeRequest, WebSocketMessageType.Text, true, cancellationTokenSource.Token);

                return(true);
            }
            else
            {
                RealtimeStreamError?.Invoke(this, new RealtimeError("Unable to send subscription msg - websocket not open"));
                return(false);
            }
        }
 private async void OnConnectionClosed(string product, GDAX_Channel channel)
 {
     //resetInProgress = false;
     await ResetStateWithFullOrderBookAsync();
 }