public void HttpOptionsSetOntoWebSocketOptions()
        {
            ClientWebSocketOptions webSocketsOptions = null;

            var httpOptions = new HttpConnectionOptions();

            httpOptions.Cookies.Add(new Cookie("Name", "Value", string.Empty, "fakeuri.org"));
            var clientCertificate = new X509Certificate();

            httpOptions.ClientCertificates.Add(clientCertificate);
            httpOptions.UseDefaultCredentials = false;
            httpOptions.Credentials           = Mock.Of <ICredentials>();
            httpOptions.Proxy = Mock.Of <IWebProxy>();
            httpOptions.WebSocketConfiguration = options => webSocketsOptions = options;

            var webSocketsTransport = new WebSocketsTransport(httpConnectionOptions: httpOptions, loggerFactory: null, accessTokenProvider: null);

            Assert.NotNull(webSocketsTransport);

            Assert.NotNull(webSocketsOptions);
            Assert.Equal(1, webSocketsOptions.Cookies.Count);
            Assert.Single(webSocketsOptions.ClientCertificates);
            Assert.Same(clientCertificate, webSocketsOptions.ClientCertificates[0]);
            Assert.False(webSocketsOptions.UseDefaultCredentials);
            Assert.Same(httpOptions.Proxy, webSocketsOptions.Proxy);
            Assert.Same(httpOptions.Credentials, webSocketsOptions.Credentials);
        }
        public async Task HttpOptionsSetOntoWebSocketOptions()
        {
            ClientWebSocketOptions webSocketsOptions = null;

            var httpOptions = new HttpConnectionOptions();

            httpOptions.Cookies.Add(new Cookie("Name", "Value", string.Empty, "fakeuri.org"));
            var clientCertificate = new X509Certificate(Array.Empty <byte>());

            httpOptions.ClientCertificates.Add(clientCertificate);
            httpOptions.UseDefaultCredentials = false;
            httpOptions.Credentials           = Mock.Of <ICredentials>();
            httpOptions.Proxy = Mock.Of <IWebProxy>();
            httpOptions.WebSocketConfiguration = options => webSocketsOptions = options;

            await using (var server = await StartServer <Startup>())
            {
                var webSocketsTransport = new WebSocketsTransport(httpConnectionOptions: httpOptions, loggerFactory: null, accessTokenProvider: null);
                Assert.NotNull(webSocketsTransport);

                // we need to open a connection so it would apply httpOptions to webSocketOptions
                await webSocketsTransport.StartAsync(new Uri(server.WebSocketsUrl + "/echo"),
                                                     TransferFormat.Binary).DefaultTimeout();

                await webSocketsTransport.StopAsync().DefaultTimeout();
            }

            Assert.NotNull(webSocketsOptions);
            Assert.Equal(1, webSocketsOptions.Cookies.Count);
            Assert.Single(webSocketsOptions.ClientCertificates);
            Assert.Same(clientCertificate, webSocketsOptions.ClientCertificates[0]);
            Assert.False(webSocketsOptions.UseDefaultCredentials);
            Assert.Same(httpOptions.Proxy, webSocketsOptions.Proxy);
            Assert.Same(httpOptions.Credentials, webSocketsOptions.Credentials);
        }
Example #3
0
 private void Configure(ClientWebSocketOptions options, ITransportConfiguration config)
 {
     config.DefaultRequestHeaders.AsEnumerable().ForEach(i => options.SetRequestHeader(i.name, i.value));
     if (!(config.Proxy is null))
     {
         options.Proxy = config.Proxy;
     }
     if (!(config.Cookies is null))
     {
         options.Cookies = config.Cookies;
     }
     if (!(config.RemoteCertificateValidator is null))
     {
         options.RemoteCertificateValidationCallback = config.RemoteCertificateValidator;
     }
     if (!(config.ClientCertificates is null))
     {
         options.ClientCertificates = config.ClientCertificates;
     }
     if (config.KeepAliveInterval.HasValue)
     {
         options.KeepAliveInterval = config.KeepAliveInterval.Value;
     }
     if (config.Credentials is null)
     {
         return;
     }
     options.Credentials           = config.Credentials;
     options.UseDefaultCredentials = false;
 }
Example #4
0
 /// <summary>
 /// Initializes a new instance of the <see cref="T:WebAssembly.Net.WebSockets.ClientWebSocket"/> class.
 /// </summary>
 public ClientWebSocket()
 {
     wsFunctionPtr = (JSObject)Runtime.GetGlobalObject("WebSocket");
     state         = created;
     options       = new ClientWebSocketOptions();
     cts           = new CancellationTokenSource();
 }
        /// <summary>
        ///
        /// </summary>
        /// <param name="uri"></param>
        /// <param name="options"></param>
        /// <exception cref="TimeoutException"></exception>
        /// <exception cref="WebSocketException"></exception>
        /// <returns></returns>
        public async Task ConnectAsync(Uri uri)
        {
            //#if NET45

            //#elif NETSTANDARD2_0
            //#endif
            //if (_io.Options.Proxy != null)
            //{
            //    _ws.Options.Proxy = _io.Options.Proxy;
            //}
            if (_ws != null)
            {
                _ws.Dispose();
            }
            _ws                = new System.Net.WebSockets.ClientWebSocket();
            Options            = _ws.Options;
            _wsWorkTokenSource = new CancellationTokenSource();
            var wsConnectionTokenSource = new CancellationTokenSource(_io.Options.ConnectionTimeout);

            try
            {
                await _ws.ConnectAsync(uri, wsConnectionTokenSource.Token);

                await Task.Factory.StartNew(ListenAsync);

                await Task.Factory.StartNew(ListenStateAsync);
            }
            catch (TaskCanceledException)
            {
                throw new TimeoutException();
            }
        }
Example #6
0
 internal WebSocketClientOptions(
     ClientWebSocketOptions options,
     HttpContext httpContext)
 {
     _options    = options;
     HttpContext = httpContext;
 }
Example #7
0
        private async Task <int> connect_task()
        {
            while (m_run == 0)
            {
                Console.Error.WriteLine(
                    "Task Connecting to {0}: ", m_uri.ToString());

                var cancellationTokenSource = new CancellationTokenSource(
                    TimeSpan.FromSeconds(10));

                try
                {
                    m_connect_sock = new ClientWebSocket();

                    ClientWebSocketOptions opt = m_connect_sock.Options;

                    Console.Error.WriteLine(opt.KeepAliveInterval.ToString());
                    opt.KeepAliveInterval = new TimeSpan(0, 0, 10);
                    Console.Error.WriteLine(opt.KeepAliveInterval.ToString());

                    m_connect_sock.ConnectAsync(
                        m_uri, cancellationTokenSource.Token).Wait(
                        cancellationTokenSource.Token);

                    Console.Error.WriteLine(
                        "Task Connected to {0}: ", m_uri.ToString());

                    iwsServiceHandler svc =
                        m_service_handler_strategy.makeServiceHandler();
                    m_run = await svc.open(m_connect_sock);
                }
                catch (Exception ex)
                {
                    Console.Error.WriteLine("Connection failed: {0}", ex.Message);
                    // Async wait before re-trying
                    await Task.Delay(3000);
                }
                finally
                {
                    //await Task.Delay(3000);
                    //await m_connect_sock.CloseAsync();
                    m_connect_sock.Dispose();
                }
            }
            return(m_run);
        }
        public async Task ShouldThrowForWrongCredentials(string username, string password)
        {
            ClientWebSocketOptions optionsSet = null;
            var webSocketConfiguration        =
                new Action <ClientWebSocketOptions>(options =>
            {
                options.RemoteCertificateValidationCallback += IgnoreCertificateValidationLiveDangerouslyWheeeeeeee;
                optionsSet = options;
            });
            var gremlinServer = new GremlinServer(TestHost, TestPort, username: username, password: password, enableSsl: true);

            using (var gremlinClient = new GremlinClient(gremlinServer, webSocketConfiguration: webSocketConfiguration))
            {
                var exception = await Assert.ThrowsAsync <ResponseException>(
                    async() => await gremlinClient.SubmitWithSingleResultAsync <string>(_requestMessageProvider
                                                                                        .GetDummyMessage()));

                Assert.Contains("Unauthorized", exception.Message);
            }
        }
        public async Task ScriptShouldBeEvaluatedAndResultReturnedForCorrectCredentials(string requestMsg,
                                                                                        string expectedResponse)
        {
            ClientWebSocketOptions optionsSet = null;
            var webSocketConfiguration        =
                new Action <ClientWebSocketOptions>(options =>
            {
                options.RemoteCertificateValidationCallback += IgnoreCertificateValidationLiveDangerouslyWheeeeeeee;
                optionsSet = options;
            });
            const string username      = "******";
            const string password      = "******";
            var          gremlinServer = new GremlinServer(TestHost, TestPort, username: username, password: password, enableSsl: true);

            using (var gremlinClient = new GremlinClient(gremlinServer, webSocketConfiguration: webSocketConfiguration))
            {
                var response = await gremlinClient.SubmitWithSingleResultAsync <string>(requestMsg);

                Assert.Equal(expectedResponse, response);
            }
        }
Example #10
0
        public async Task ShouldConfigureWebSocketOptionsAsSpecified()
        {
            var gremlinServer = new GremlinServer(TestHost, TestPort);
            ClientWebSocketOptions optionsSet = null;
            var expectedKeepAliveInterval     = TimeSpan.FromMilliseconds(11);
            var webSocketConfiguration        =
                new Action <ClientWebSocketOptions>(options =>
            {
                options.UseDefaultCredentials = false;
                options.KeepAliveInterval     = expectedKeepAliveInterval;
                optionsSet = options;
            });

            using (var gremlinClient = new GremlinClient(gremlinServer, webSocketConfiguration: webSocketConfiguration))
            {
                // send dummy message to create at least one connection
                await gremlinClient.SubmitAsync(_requestMessageProvider.GetDummyMessage());

                Assert.NotNull(optionsSet);
                Assert.False(optionsSet.UseDefaultCredentials);
                Assert.Equal(expectedKeepAliveInterval, optionsSet.KeepAliveInterval);
            }
        }
Example #11
0
 public CustomClientWebSocketOptions(ClientWebSocketOptions options)
 {
     this.options = options;
 }
Example #12
0
 /// <summary>
 /// Initializes a new instance of the <see cref="T:WebAssembly.Net.WebSockets.ClientWebSocket"/> class.
 /// </summary>
 public ClientWebSocket()
 {
     state   = created;
     options = new ClientWebSocketOptions();
     cts     = new CancellationTokenSource();
 }
Example #13
0
            static void AppendHeaderValue(ClientWebSocketOptions options, IHeaderDictionary headers, string key, string value)
            {
                var newValue = new StringValues(headers[key].Append(value).ToArray());

                options.SetRequestHeader(key, newValue);
            }