private void BinanceWebsocketHandleEvents()
        {
            // SUBSCRIBE
            var sub = m_socketClient.Spot.SubscribeToAllSymbolTickerUpdates(data =>
            {
                Console.WriteLine("Reveived list update");
            });

            // HANDLE EVENTS
            //sub.Data.Closed += () =>
            sub.Data.ConnectionLost += () =>
            {
                Console.WriteLine("Connection lost");
            };

            sub.Data.ConnectionRestored += (timeOffline) =>
            {
                Console.WriteLine($"Connection restored after {timeOffline}");
            };

            sub.Data.Exception += (e) =>
            {
                Console.WriteLine("Socket error " + e.Message);
            };

            Thread.Sleep(15000);

            // UNSUBSCRIBE
            m_socketClient.Unsubscribe(sub.Data);   //UnsubscribeFromStream(sub.Data);

            // Additionaly, all sockets can be closed with the UnsubscribeAllStreams method.
            m_socketClient.UnsubscribeAll();
        }
示例#2
0
        protected override async Task DoWork(PairConfig pair, CancellationToken stoppingToken)
        {
            while (!stoppingToken.IsCancellationRequested)
            {
                try
                {
                    using (BinanceSocketClient client = new BinanceSocketClient())
                    {
                        ConnectionInfo cnInfo = new ConnectionInfo(_settings.Value.BusConnectionString);
                        using (NatsClient natsClient = new NatsClient(cnInfo))
                        {
                            if (!natsClient.IsConnected)
                            {
                                natsClient.Connect();
                            }

                            CallResult <UpdateSubscription> successKline = null;
                            successKline = client.SubscribeToTradeUpdates(pair.Symbol, async(data) =>
                            {
                                Trade trade = data.ToEntity();

                                if (!_settings.Value.DisadleDealsSaving)
                                {
                                    long id = await _tradesProcessor.Create(trade);
                                }

                                await natsClient.PubAsJsonAsync(_settings.Value.TradesQueueName, new Notification <Trade>()
                                {
                                    Code = ActionCode.CREATED.Code, Payload = trade
                                });
                            });

                            successKline.Data.ConnectionLost     += () => { _logger.LogError($"Connection to {Exchange} is lost"); };
                            successKline.Data.ConnectionRestored += (data) => { _logger.LogError($"Connection to {Exchange} is Restored"); };

                            while (!stoppingToken.IsCancellationRequested)
                            {
                            }

                            natsClient.Disconnect();
                            await client.Unsubscribe(successKline.Data);
                        }
                    }
                }
                catch (Exception ex)
                {
                    _logger.LogError($"{Exchange.Description} Trades service failed with message {ex.Message}", ex);
                }
            }
        }
示例#3
0
        protected override async Task DoWork(PairConfig pair, CancellationToken stoppingToken)
        {
            _logger.LogInformation($"{Exchange.Description} Candles worker is started");

            IPeriodCode candlePeriod = pair.Timeframe.HasValue ? PeriodCode.Create(pair.Timeframe.Value) : DefaultCandleInterval;

            while (!stoppingToken.IsCancellationRequested)
            {
                try
                {
                    using (BinanceSocketClient client = new BinanceSocketClient())
                    {
                        ConnectionInfo cnInfo = new ConnectionInfo(_settings.Value.BusConnectionString);
                        using (NatsClient natsClient = new NatsClient(cnInfo))
                        {
                            if (!natsClient.IsConnected)
                            {
                                natsClient.Connect();
                            }

                            CallResult <UpdateSubscription> successKline = client.SubscribeToKlineUpdates(pair.Symbol, candlePeriod.ToPeriodCode(), async(data) =>
                            {
                                await SaveCandle(data.Data, natsClient);
                            });

                            successKline.Data.ConnectionLost     += () => { _logger.LogError($"Connection to {Exchange} is lost"); };
                            successKline.Data.ConnectionRestored += (data) => { _logger.LogError($"Connection to {Exchange} is Restored"); };

                            while (!stoppingToken.IsCancellationRequested)
                            {
                            }

                            natsClient.Disconnect();
                            await client.Unsubscribe(successKline.Data);
                        }
                    }
                }
                catch (Exception ex)
                {
                    _logger.LogError($"{Exchange.Description} Candles service failed with message: {ex.Message}", ex);
                }
            }
        }
示例#4
0
 static async Task BinanceUnsubscribe(BinanceSocketClient sock, UpdateSubscription subscription)
 {
     await sock.Unsubscribe(subscription);
 }