private async Task SubscribeToSymbols()
        {
            if (_symbolsSubscribeTask != null)
            {
                return;
            }

            try
            {
                try
                {
                    await _symbolsStatisticSemaphore.WaitAsync();

                    if (_symbolsSubscribeTask != null)
                    {
                        return;
                    }

                    _log.LogInformation($"Subscribe to symbols");

                    _symbolStatisticCancellationTokenSource?.Cancel();
                    _symbolStatisticCancellationTokenSource?.Dispose();

                    _symbolStatisticCancellationTokenSource = new CancellationTokenSource();

                    _symbolStatisticsWebSocketClient = _serviceProvider.GetService <ISymbolStatisticsWebSocketClient>();

                    _symbolsSubscribeTask = _symbolStatisticsWebSocketClient.SubscribeAsync(_onSymbolStatisticUpdate, _symbolStatisticCancellationTokenSource.Token);

                    SymbolsDisconnectionTimerInitialize();
                }
                finally
                {
                    _symbolsStatisticSemaphore.Release();
                }

                await _symbolsSubscribeTask;

                _symbolsSubscribeTask = null;
            }
            catch (Exception)
            {
                _symbolsSubscribeTask = null;

                OnSymbolStatisticDisconnect(true);
            }
        }
Пример #2
0
        private async Task SubscribeToSymbols(bool reConnect = false)
        {
            _log.LogInformation($"Subscribe to symbols");

            if (_symbolsSubscribeTask != null && !reConnect)
            {
                return;
            }

            try
            {
                _symbolStatisticCancellationTokenSource?.Cancel();
                _symbolStatisticCancellationTokenSource?.Dispose();

                _symbolStatisticCancellationTokenSource = new CancellationTokenSource();

                _symbolStatisticsWebSocketClient = _serviceProvider.GetService <ISymbolStatisticsWebSocketClient>();

                _symbolsSubscribeTask = _symbolStatisticsWebSocketClient.SubscribeAsync(_onSymbolStatisticUpdate, _symbolStatisticCancellationTokenSource.Token);

                if (!reConnect)
                {
                    SymbolsReConnectionTimerInitialize();
                }

                await _symbolsSubscribeTask;
            }
            catch (Exception ex)
            {
                _symbolsSubscribeTask = null;

                _log.LogError($"Error with symbols statistic websocket {ex.Message}", ex);

                _onSymbolStatisticError?.Invoke();
            }
        }
Пример #3
0
 /// <summary>
 ///
 /// </summary>
 /// <param name="client"></param>
 /// <param name="symbol"></param>
 /// <param name="token"></param>
 /// <returns></returns>
 public static Task SubscribeAsync(this ISymbolStatisticsWebSocketClient client, string symbol, CancellationToken token)
 => client.SubscribeAsync(symbol, null, token);