Exemplo n.º 1
0
        public async Task Connect(string remote, string authToken, WebSocketConnectionOptions options = null)
        {
            var connectionString = GenerateConnectionString(remote, authToken);

            _connection = new WebSocketEventStream(OnWebSocketEvent);
            await _connection.Connect(connectionString, options);
        }
Exemplo n.º 2
0
        public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseExceptionHandler("/Home/Error");
                app.UseHsts();
            }

            app.UseHttpsRedirection();
            app.UseStaticFiles();
            app.UseCookiePolicy();

            var textSubProtocol     = new TextSubProtocol();
            var wsConnectionOptions = new WebSocketConnectionOptions
            {
                DefaultProtocol    = textSubProtocol,
                SupportedProtocols = new List <ISubProtocol> {
                    textSubProtocol, new JsonSubProtocol()
                }
            };

            app.UseWebSocketEcho("/ws", wsConnectionOptions)
            .UseMvc(routes =>
            {
                routes.MapRoute(
                    name: "default",
                    template: "{controller=Home}/{action=Index}/{id?}");
            });
        }
Exemplo n.º 3
0
        public WebSocketsTransport(WebSocketConnectionOptions connectionOptions, ILoggerFactory loggerFactory, Func <Task <string> > accessTokenProvider)
        {
            _logger    = (loggerFactory ?? throw new ArgumentNullException(nameof(loggerFactory))).CreateLogger <WebSocketsTransport>();
            _webSocket = new ClientWebSocket();

            // Issue in ClientWebSocket prevents user-agent being set - https://github.com/dotnet/corefx/issues/26627
            //_webSocket.Options.SetRequestHeader("User-Agent", Constants.UserAgentHeader.ToString());

            if (connectionOptions != null)
            {
                if (connectionOptions.Headers != null)
                {
                    foreach (var header in connectionOptions.Headers)
                    {
                        _webSocket.Options.SetRequestHeader(header.Key, header.Value);
                    }
                }

                if (connectionOptions.Cookies != null)
                {
                    _webSocket.Options.Cookies = connectionOptions.Cookies;
                }

                if (connectionOptions.ClientCertificates != null)
                {
                    _webSocket.Options.ClientCertificates.AddRange(connectionOptions.ClientCertificates);
                }

                if (connectionOptions.Credentials != null)
                {
                    _webSocket.Options.Credentials = connectionOptions.Credentials;
                }

                if (connectionOptions.Proxy != null)
                {
                    _webSocket.Options.Proxy = connectionOptions.Proxy;
                }

                if (connectionOptions.UseDefaultCredentials != null)
                {
                    _webSocket.Options.UseDefaultCredentials = connectionOptions.UseDefaultCredentials.Value;
                }

                connectionOptions.WebSocketConfiguration?.Invoke(_webSocket.Options);

                _closeTimeout = connectionOptions.CloseTimeout;
            }

            // Set this header so the server auth middleware will set an Unauthorized instead of Redirect status code
            // See: https://github.com/aspnet/Security/blob/ff9f145a8e89c9756ea12ff10c6d47f2f7eb345f/src/Microsoft.AspNetCore.Authentication.Cookies/Events/CookieAuthenticationEvents.cs#L42
            _webSocket.Options.SetRequestHeader("X-Requested-With", "XMLHttpRequest");

            // Ignore the HttpConnectionOptions access token provider. We were given an updated delegate from the HttpConnection.
            _accessTokenProvider = accessTokenProvider;
        }
 public async Task ConnectAsync(Uri uri, WebSocketConnectionOptions options)
 {
     _ws            = new WebSocket(uri.ToString());
     _ws.OnMessage += OnMessage;
     _ws.Connect();
     if (_ws.ReadyState == WebSocketState.Closed || _ws.ReadyState == WebSocketState.Closing)
     {
         throw new System.Net.WebSockets.WebSocketException("Unable to connect to the remote server.");
     }
     _ws.OnClose     += OnClose;
     _connectionToken = new CancellationTokenSource();
     await Task.Factory.StartNew(ListenStateAsync, _connectionToken.Token);
 }
 /// <summary>
 ///
 /// </summary>
 /// <param name="uri"></param>
 /// <param name="options"></param>
 /// <returns></returns>
 /// <exception cref="System.Net.WebSockets.WebSocketException"></exception>
 public async Task ConnectAsync(Uri uri, WebSocketConnectionOptions options)
 {
     _ws = new WebSocket(uri.ToString());
     WebSocketConfiger?.Invoke(_ws);
     _ws.OnMessage += OnMessage;
     _ws.OnError   += OnError;
     _ws.OnClose   += OnClose;
     // set enabled client Ssl protocols as defined via options
     _ws.SslConfiguration.EnabledSslProtocols = _io.Options.EnabledSslProtocols;
     _ws.Connect();
     if (_ws.ReadyState == WebSocketState.Closed || _ws.ReadyState == WebSocketState.Closing)
     {
         throw new System.Net.WebSockets.WebSocketException("Unable to connect to the remote server.");
     }
     _ws.OnClose     += OnClose;
     _connectionToken = new CancellationTokenSource();
     await Task.Factory.StartNew(ListenStateAsync, _connectionToken.Token);
 }
Exemplo n.º 6
0
        public static IApplicationBuilder UseWebSocketEcho(this IApplicationBuilder app, string pathMatch, WebSocketConnectionOptions options)
        {
            if (app == null)
            {
                throw new ArgumentNullException(nameof(app));
            }

            app.UseWebSockets(new WebSocketOptions {
                KeepAliveInterval = options.KeepAliveInterval, ReceiveBufferSize = options.ReceiveBufferSize
            });

            return(app.Map(pathMatch, builder => builder.UseMiddleware <WebSocketEchoMiddleware>(options)));
        }
 public static void UseWebSocketConnections(this IApplicationBuilder self, ChannelFactory channelFactory, WebSocketConnectionOptions options)
 {
     if (channelFactory == null)
     {
         throw new ArgumentNullException(nameof(channelFactory));
     }
     if (options == null)
     {
         throw new ArgumentNullException(nameof(options));
     }
     self.UseWebSocketConnections(channelFactory, options);
     self.UseMiddleware <WebSocketConnectionMiddleware>(channelFactory, options);
 }
Exemplo n.º 8
0
 public WebSocketEchoMiddleware(RequestDelegate next, WebSocketConnectionOptions options)
 {
     this.options = options;
 }