public override bool ConnectAsync()

in src/Transport/WebSocketTransportInitiator.cs [21:73]


        public override bool ConnectAsync(TimeSpan timeout, TransportAsyncCallbackArgs callbackArgs)
        {
            ClientWebSocket cws = new ClientWebSocket();
            cws.Options.AddSubProtocol(this.settings.SubProtocol);
            if (this.settings.InternalSendBufferSize > 0 || this.settings.InternalReceiveBufferSize > 0)
            {
                cws.Options.SetBuffer(this.settings.ReceiveBufferSize, this.settings.SendBufferSize);
            }

            if (this.settings.Proxy != null)
            {
                cws.Options.Proxy = this.settings.Proxy;
            }

            if (this.settings.WebsocketKeepAliveInterval != null)
            {
                cws.Options.KeepAliveInterval = this.settings.WebsocketKeepAliveInterval.Value;
            }

            var task = new TimeoutTaskSource<ClientWebSocket>(
                cws,
                s => s.ConnectAsync(this.settings.Uri, CancellationToken.None),
                static s => s.Abort(),
                timeout).Task;

            if (task.IsCompleted)
            {
                callbackArgs.Transport = new WebSocketTransport(cws, this.settings.Uri, null,
                    new DnsEndPoint(this.settings.Uri.Host, this.settings.Uri.Port));
                return false;
            }

            task.ContinueWith(static (t,s) =>
            {
                var (transport, callbackArgs, cws) = (Tuple<WebSocketTransportInitiator, TransportAsyncCallbackArgs, ClientWebSocket>) s;
                if (t.IsFaulted)
                {
                    callbackArgs.Exception = t.Exception?.InnerException;
                }
                else if (t.IsCanceled)
                {
                    callbackArgs.Exception = new OperationCanceledException();
                }
                else
                {
                    callbackArgs.Transport = new WebSocketTransport(cws, transport.settings.Uri, null,
                        new DnsEndPoint(transport.settings.Uri.Host, transport.settings.Uri.Port));
                }

                callbackArgs.CompletedCallback(callbackArgs);
            }, Tuple.Create(this, callbackArgs, cws));
            return true;
        }