public WebSocketConnectionsMiddleware(RequestDelegate next,
                                       WebSocketConnectionsOptions options,
                                       IWebSocketConnectionsManager connectionsService,
                                       ILogger <WebSocketConnectionsMiddleware> logger)
 {
     _options            = options ?? throw new ArgumentNullException(nameof(options));
     _connectionsManager = connectionsService ?? throw new ArgumentNullException(nameof(connectionsService));
     _logger             = logger;
 }
        public static IApplicationBuilder MapWebSocketConnections(this IApplicationBuilder app,
                                                                  PathString pathMatch,
                                                                  WebSocketConnectionsOptions options)
        {
            if (app == null)
            {
                throw new ArgumentNullException(nameof(app));
            }

            return(app.Map(pathMatch, branchedApp => branchedApp.UseMiddleware <WebSocketConnectionsMiddleware>(options)));
        }
Esempio n. 3
0
        override public void Configure(IApplicationBuilder app, ILoggerFactory loggerFactory)
        {
            base.Configure(app, loggerFactory);
            WebSocketConnectionsOptions webSocketConnectionsOptions = new WebSocketConnectionsOptions
            {
                SendSegmentSize = 4 * 1024
            };

            app
            .UseStaticFiles()
            .UseWebSockets()
            .MapWebSocketConnections("", webSocketConnectionsOptions)
            .Run(async(context) =>
            {
                await context.Response.WriteAsync("test WebSocket");
            });
        }
        public async Task CreateWebSocketConnectionAsync(WebSocket webSocket,
                                                         WebSocketConnectionsOptions options,
                                                         Guid connectionId)
        {
            WebSocketConnection webSocketConnection = new WebSocketConnection(webSocket,
                                                                              options.ReceivePayloadBufferSize, connectionId);

            webSocketConnection.ReceiveText += OnReceiveText;
            OnConnect(webSocketConnection);

            await webSocketConnection.ReceiveMessagesUntilCloseAsync();

            if (webSocketConnection.CloseStatus.HasValue)
            {
                await webSocket.CloseAsync(webSocketConnection.CloseStatus.Value, webSocketConnection.CloseStatusDescription, CancellationToken.None);
            }
            Disconnect?.Invoke(this, webSocketConnection);
            webSocketConnection.ReceiveText -= OnReceiveText;
        }
        public void Configure(IApplicationBuilder app, IHostingEnvironment env, IServiceProvider serviceProvider)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

            DefaultFilesOptions defaultFilesOptions = new DefaultFilesOptions();

            defaultFilesOptions.DefaultFileNames.Clear();
            defaultFilesOptions.DefaultFileNames.Add("websocket-api.html");

            ITextWebSocketSubprotocol   textWebSocketSubprotocol    = new PlainTextWebSocketSubprotocol();
            WebSocketConnectionsOptions webSocketConnectionsOptions = new WebSocketConnectionsOptions
            {
                AllowedOrigins = new HashSet <string> {
                    "http://localhost:63290"
                },
                SupportedSubProtocols = new List <ITextWebSocketSubprotocol>
                {
                    new JsonWebSocketSubprotocol(),
                    textWebSocketSubprotocol
                },
                DefaultSubProtocol = textWebSocketSubprotocol,
                SendSegmentSize    = 4 * 1024
            };

            app.UseDefaultFiles(defaultFilesOptions)
            .UseStaticFiles()
            .UseWebSocketsCompression()
            .MapWebSocketConnections("/socket", webSocketConnectionsOptions)
            .Run(async(context) =>
            {
                await context.Response.WriteAsync("-- Demo.AspNetCore.WebSocket --");
            });
        }
Esempio n. 6
0
        public static IApplicationBuilder MapWebSocketConnections(this IApplicationBuilder app,
                                                                  PathString pathMatch, WebSocketConnectionsOptions options,
                                                                  bool?featureToggleEnabled = true)
        {
            if (app == null)
            {
                throw new ArgumentNullException(nameof(app));
            }

            return(app.Map(pathMatch, branchedApp =>
            {
                if (featureToggleEnabled == true)
                {
                    branchedApp.UseMiddleware <WebSocketConnectionsMiddleware>(options);
                    return;
                }
                branchedApp.UseMiddleware <DisabledWebSocketsMiddleware>();
            }));
        }
 public WebSocketConnectionsMiddleware(RequestDelegate _, WebSocketConnectionsOptions options,
                                       IWebSocketConnectionsService connectionsService)
 {
     _options            = options ?? throw new ArgumentNullException(nameof(options));
     _connectionsService = connectionsService ?? throw new ArgumentNullException(nameof(connectionsService));
 }