Beispiel #1
0
        private async Task Connect(CancellationToken token)
        {
            var reconnectCount = _reconnectCount;

loop:
            var client = new ClientWebSocket();

            try
            {
                client.Options.KeepAliveInterval = _keepAliveInterval;
                await Task.WhenAny(Task.Delay(_connectTimeout, token), client.ConnectAsync(_connectionString, token));

                if (client.State != WebSocketState.Open)
                {
                    throw new TimeoutException();
                }
                if (_interact != null)
                {
                    try { _interact.Dispose(); } catch { }
                }

                _interact = new Interact(client);
                _receive  = _interact.Receive();
            }
            catch (TaskCanceledException) { throw; }
            catch
            {
                try { client.Dispose(); } catch { }
                await Task.Delay(_reconnectInterval, token);

                if (--reconnectCount == 0)
                {
                    throw;
                }
                goto loop;
            }
        }