コード例 #1
0
        private async Task <bool> InternalAssertConnected(CancellationToken cancellationToken)
        {
            if (this.client != null &&
                this.wsClient != null &&
                this.wsClient.IsConnected &&
                !this.receiveMessagesCts.IsCancellationRequested)
            {
                Logger.Trace("Already connected");
                return(false);
            }

            var retryCount = 0;

            do
            {
                try
                {
                    this.receiveMessagesCts.Cancel();
                    await this.messageLoopTask;
                    Logger.Trace("Message loop task finished");
                    this.receiveMessagesCts = new CancellationTokenSource();

                    this.client   = new WebSocketClient(this.options);
                    this.wsClient = await this.client.ConnectAsync(this.Endpoint, cancellationToken);

                    this.messageLoopTask = Task.Run(() => ReceiveMessagesLoop(this.receiveMessagesCts.Token), cancellationToken);

                    Logger.Log("Connected to server");
                    return(true);
                }
                catch (OperationCanceledException)
                {
                    throw;
                }
                catch (Exception ex)
                {
                    Logger.Warn($"Connection failed, retry {++retryCount}/10");
                    Logger.Debug($"{ex}");
                    await Task.Delay(2000, cancellationToken);
                }
            } while (retryCount < 10);

            throw new WebsocketConnectionFailedException();
        }
コード例 #2
0
 public void Warning(string message, Exception error = null)
 {
     Logger.Warn($"{message} {error}");
 }