Example #1
0
 public WebSocketClientTests()
 {
     _resetEvent = new AutoResetEvent(false);
     _url        = "ws://127.0.0.1:5000";
     _server     = new WebSocketServer(_url);
     _client     = new DefaultWebSocketClient(_url);
 }
Example #2
0
        /// <summary>
        /// Example using manager without DI framework (not recommended).
        /// </summary>
        // ReSharper disable once InconsistentNaming
        // ReSharper disable once UnusedMember.Local
        private static void ExampleMainWithoutDI()
        {
            try
            {
                // Load configuration.
                var configuration = new ConfigurationBuilder()
                                    .SetBasePath(Directory.GetCurrentDirectory())
                                    .AddJsonFile("appsettings.json", true, false)
                                    .Build();

                // Get configuration settings.
                var symbols = configuration.GetSection("TradeHistory:Symbols").Get <string[]>()
                              ?? new string[] { Symbol.BTC_USDT };

                var limit = 25;
                try { limit = Convert.ToInt32(configuration.GetSection("TradeHistory")?["Limit"]); }
                catch { /* ignore */ }

                var loggerFactory = new LoggerFactory();
                loggerFactory.AddFile(configuration.GetSection("Logging:File"));

                // Initialize all the things... a DI framework could instantiate for you...
                var api         = new BinanceApi(BinanceHttpClient.Instance, logger: loggerFactory.CreateLogger <BinanceApi>());
                var tradeClient = new AggregateTradeClient(loggerFactory.CreateLogger <AggregateTradeClient>());
                var webSocket   = new DefaultWebSocketClient(logger: loggerFactory.CreateLogger <DefaultWebSocketClient>());
                var stream      = new BinanceWebSocketStream(webSocket, loggerFactory.CreateLogger <BinanceWebSocketStream>());
                var controller  = new BinanceWebSocketStreamController(api, stream, loggerFactory.CreateLogger <BinanceWebSocketStreamController>());
                var publisher   = new BinanceWebSocketStreamPublisher(controller, loggerFactory.CreateLogger <BinanceWebSocketStreamPublisher>());
                var client      = new AggregateTradeWebSocketClient(tradeClient, publisher, loggerFactory.CreateLogger <AggregateTradeWebSocketClient>());
                var cache       = new AggregateTradeWebSocketCache(api, client, loggerFactory.CreateLogger <AggregateTradeWebSocketCache>());

                // Add error event handler.
                controller.Error += (s, e) => HandleError(e.Exception);

                foreach (var symbol in symbols)
                {
                    // Subscribe to symbol with callback.
                    cache.Subscribe(symbol, limit, Display);

                    lock (_sync)
                    {
                        _message = symbol == symbols.Last()
                            ? $"Symbol: \"{symbol}\" ...press any key to exit."
                            : $"Symbol: \"{symbol}\" ...press any key to continue.";
                    }
                    Console.ReadKey(true);

                    // Unsubscribe from symbol.
                    cache.Unsubscribe();
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                Console.WriteLine();
                Console.WriteLine("  ...press any key to close window.");
                Console.ReadKey(true);
            }
        }
        public async Task StreamAsync()
        {
            var uri = new Uri("wss://stream.binance.com:9443");

            var client = new DefaultWebSocketClient();

            using (var cts = new CancellationTokenSource())
            {
                cts.Cancel();
                await client.StreamAsync(uri, cts.Token);
            }
        }
        public async Task Throws()
        {
            var uri = new Uri("wss://stream.binance.com:9443");

            var client = new DefaultWebSocketClient();

            using (var cts = new CancellationTokenSource())
            {
                await Assert.ThrowsAsync <ArgumentNullException>("uri", () => client.StreamAsync(null, cts.Token));

                await Assert.ThrowsAsync <ArgumentException>("token", () => client.StreamAsync(uri, CancellationToken.None));
            }
        }
Example #5
0
 public DefaultWebSocketClientTest()
 {
     _uri       = new Uri(BinanceWebSocketStream.BaseUri);
     _subject   = _uri.AbsoluteUri;
     _webSocket = CreateMockWebSocketClient(_message);
 }