Beispiel #1
0
        public async Task ConnectAsync(System.Uri uri)
        {
            try
            {
                await DisconnectAsync().ConfigureAwait(false);
            }
            catch
            {
                // ignored
            }

            await Locker.WaitAsync().ConfigureAwait(false);

            try
            {
                SocketTokenSource?.Dispose();
                ReceiverTokenSource?.Dispose();

                SocketTokenSource   = new CancellationTokenSource();
                SocketToken         = SocketTokenSource.Token;
                ReceiverTokenSource = new CancellationTokenSource();
                ReceiverToken       = ReceiverTokenSource.Token;

                IsClientClosed = false;

                ClientWebSocket?.Dispose();
                ClientWebSocket = new ClientWebSocket
                {
                    Options =
                    {
                        KeepAliveInterval = TimeSpan.Zero,
                    }
                };

                if (Options.WebProxy != null)
                {
                    ClientWebSocket.Options.Proxy = Options.WebProxy;
                }

                if (Options.Headers.Any())
                {
                    foreach (var(k, v) in Options.Headers)
                    {
                        ClientWebSocket.Options.SetRequestHeader(k, v);
                    }
                }

                await ClientWebSocket.ConnectAsync(uri, SocketToken).ConfigureAwait(false);

                ReadTask = Task.Run(ReadAsync, ReceiverToken);
            }
            finally
            {
                Locker.Release();
            }
        }
Beispiel #2
0
        public void Dispose()
        {
            if (IsDisposed)
            {
                return;
            }

            IsDisposed = true;

            DisconnectAsync().GetAwaiter().GetResult();

            SocketTokenSource.Dispose();
            ReceiverTokenSource.Dispose();
        }
Beispiel #3
0
        public async Task DisconnectAsync(int code = 1000, string message = "")
        {
            // ReSharper disable once MethodSupportsCancellation
            await Locker.WaitAsync().ConfigureAwait(false);

            try
            {
                IsClientClosed = true;

                if (ClientWebSocket != null)
                {
                    await ClientWebSocket.CloseOutputAsync((WebSocketCloseStatus)code, message, CancellationToken.None)
                    .ConfigureAwait(false);
                }

                if (ReadTask != null)
                {
                    await ReadTask.ConfigureAwait(false);
                }

                if (IsConnected)
                {
                    IsConnected = false;
                }

                SocketTokenSource?.Cancel();
                SocketTokenSource?.Dispose();

                ReceiverTokenSource?.Cancel();
                ReceiverTokenSource?.Dispose();
            }
            catch
            {
                // ignored
            }
            finally
            {
                Locker.Release();
            }
        }