示例#1
0
        /// <summary>
        /// Establish a connection with optional custom headers.
        /// </summary>
        /// <param name="requestHeaders">An optional <see cref="IDictionary{TKey, TValue}"/> of string header names and string header values to include when sending the
        /// initial request to establish this connection.
        /// </param>
        /// <param name="cancellationToken">An optional <see cref="CancellationToken"/> used to signal this operation should be cancelled.</param>
        /// <returns>A <see cref="Task"/> that will not resolve until the client stops listening for incoming messages.</returns>
#pragma warning disable UseAsyncSuffix // Use Async suffix (we can't change this without breaking binary compat)
        public async Task ConnectAsyncEx(IDictionary <string, string> requestHeaders = null, CancellationToken cancellationToken = default(CancellationToken))
#pragma warning restore UseAsyncSuffix // Use Async suffix
        {
            if (IsConnected)
            {
                return;
            }

            var clientWebSocket = new ClientWebSocket();

            if (requestHeaders != null)
            {
                foreach (var key in requestHeaders.Keys)
                {
                    clientWebSocket.Options.SetRequestHeader(key, requestHeaders[key]);
                }
            }

            await clientWebSocket.ConnectAsync(new Uri(_url), cancellationToken).ConfigureAwait(false);

#pragma warning disable CA2000 // Dispose objects before losing scope

            // We don't dispose the websocket, since WebSocketTransport is now
            // the owner of the web socket.
            var socketTransport = new WebSocketTransport(clientWebSocket);
#pragma warning restore CA2000 // Dispose objects before losing scope

            // Listen for disconnected events.
            _sender.Disconnected   += OnConnectionDisconnected;
            _receiver.Disconnected += OnConnectionDisconnected;

            _sender.Connect(socketTransport);
            _receiver.Connect(socketTransport);

            IsConnected = true;
        }
        /// <summary>
        /// Establish a connection with injected web socket for more control in tests.
        /// </summary>
        /// <param name="socket">A <see cref="WebSocket"/> for the client which msut already be .</param>
        /// <returns>A <see cref="Task"/> that will not resolve until the client stops listening for incoming messages.</returns>
        internal Task ConnectInternalAsync(WebSocket socket)
        {
            if (IsConnected)
            {
                return(Task.CompletedTask);
            }

            // We don't dispose the websocket, since WebSocketTransport is now
            // the owner of the web socket.
#pragma warning disable CA2000 // Dispose objects before losing scope
            var socketTransport = new WebSocketTransport(socket);
#pragma warning restore CA2000 // Dispose objects before losing scope

            // Listen for disconnected events.
            _sender.Disconnected   += OnConnectionDisconnected;
            _receiver.Disconnected += OnConnectionDisconnected;

            _sender.Connect(socketTransport);
            _receiver.Connect(socketTransport);

            IsConnected = true;

            return(Task.CompletedTask);
        }