Ejemplo n.º 1
0
        /// <summary>
        /// Invoke in asp.net core pipeline
        /// </summary>
        /// <param name="context">Http context</param>
        /// <param name="webSocketHandler">Websocket handler</param>
        /// <param name="idGenerator">Unique Id generator</param>
        public async Task Invoke(HttpContext context, IWebSocketHandler webSocketHandler, IUniqueIdentifierGenerator idGenerator)
        {
            if (!context.WebSockets.IsWebSocketRequest)
            {
                return;
            }

            var socket = new IdentifiableWebSocket(idGenerator, await context.WebSockets.AcceptWebSocketAsync());

            await webSocketHandler.HandleAsync(socket, _cancellationToken);
        }
Ejemplo n.º 2
0
        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env, IMsLoggerFactory loggerFactory, IApplicationLifetime lifetime)
        {
            Log.Debug("Configure");

            LogManager.ConfigureLogging(loggerFactory);

            lifetime.ApplicationStarted.Register(_handler.OnListeningStarted);

            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

            app.UseWebSockets();

            app.Use(async(context, next) =>
            {
                if (context.WebSockets.IsWebSocketRequest)
                {
                    try
                    {
                        Log.Trace("Accepting websocket connection");
                        var webSocket = await context.WebSockets.AcceptWebSocketAsync().ConfigureAwait(false);
                        await _handler.HandleAsync(webSocket).ConfigureAwait(false);
                        Log.Trace("Websocket connection completed");
                    }
                    catch (Exception ex)
                    {
                        Log.Trace("Websocket connection terminated with exception: {0}", ex.FormatTypeAndMessage());
                    }
                }
                else
                {
                    Log.Trace("Non-websocket connection received");
                    await next().ConfigureAwait(false);
                }
            });
        }