示例#1
0
        public async Task Connect(BittrexFeedConnectionConfiguration configuration)
        {
            DefaultHttpClient httpClient    = new DefaultHttpClient();
            AutoTransport     autoTransport = null;

            if (configuration != null)
            {
                if (configuration.Connection != null)
                {
                    var transports = new IClientTransport[]
                    {
                        new WebSocketSharpTransport(httpClient),
                        new LongPollingTransport(httpClient)
                    };

                    autoTransport = new AutoTransport(httpClient, transports);

                    _connection.CookieContainer = configuration.Connection.CookieContainer;

                    if (configuration.Connection.Headers != null)
                    {
                        foreach (var header in configuration.Connection.Headers)
                        {
                            _connection.Headers[header.Key] = header.Value;
                        }
                    }

                    _connection.TransportConnectTimeout = new TimeSpan(0, 0, 10);
                }

                if (!string.IsNullOrEmpty(configuration.AccessToken))
                {
                    var aspNetApplicationCookie = new Cookie(BittrexConstants.accessTokenCookieName, configuration.AccessToken, "/", ".bittrex.com");
                    _connection.CookieContainer.Add(_feedUri, aspNetApplicationCookie);
                }
            }

            if (autoTransport == null)
            {
                autoTransport = new AutoTransport(httpClient);
            }

            await _connection.Start(autoTransport);
        }
示例#2
0
        static void Main(string[] args)
        {
            const string userAgent = "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36 OPR/48.0.2685.52";

            var bittrexUri     = new Uri("https://bittrex.com");
            var bittrexFeedUri = new Uri("https://socket.bittrex.com");

            //

            var feedHeaders       = new Dictionary <string, string>();
            var cookieContainer   = new CookieContainer();
            var httpClientHandler = new HttpClientHandler()
            {
                UseCookies      = true,
                CookieContainer = cookieContainer
            };

            var clearanceHandler = new ClearanceHandler(httpClientHandler);
            var httpClient       = new HttpClient(clearanceHandler);

            httpClient.DefaultRequestHeaders.UserAgent.ParseAdd(userAgent);

            //

            var connConfig = new ConnectionConfiguration()
            {
                CookieContainer = cookieContainer,
                Headers         = feedHeaders
            };

            feedHeaders.Add("User-Agent", userAgent);

            var config = new BittrexFeedConnectionConfiguration()
            {
                // NOTE: Not applicable: AccessToken = "",
                Connection = connConfig
            };

            var exchange = new BittrexExchange(bittrexFeedUri);

            //

            var request = new HttpRequestMessage(HttpMethod.Get, bittrexUri);
            var content = httpClient.SendAsync(request, CancellationToken.None).Result;

            //

            exchange.Connection.CookieContainer = cookieContainer;
            exchange.Connection.StateChanged   += (stateChange) =>
            {
                if (stateChange.NewState == ConnectionState.Connected)
                {
                    exchange.HubProxy.Invoke("SubscribeToSummaryDeltas").ContinueWith((t) =>
                    {
                        //bool isConnected = t.Result;
                        //HubProxy.Invoke("QueryExchangeState", "BTC-EXCL");//get order book snapshot
                    });

                    exchange.HubProxy.Invoke("SubscribeToExchangeDeltas", "BTC-EXCL").ContinueWith((t) =>
                    {
                        //bool isConnected = t.Result;
                        //HubProxy.Invoke("QueryExchangeState", "BTC-EXCL");//get order book snapshot
                    });
                }
            };

            exchange.Connect(config).Wait();

            Console.ReadLine();
        }