private async Task <HandlerType> ExecuteHandler(HttpContext aspNetContext,
                                                        IEnumerable <Func <Request, Response, WebSocketDialog, Task <HandlerType> > > handlers)
        {
            var context  = new Context(aspNetContext, Plugins);
            var request  = new Request(context);
            var response = new Response(context);

            var status = HandlerType.Continue;

            try
            {
                if (aspNetContext.WebSockets.IsWebSocketRequest)
                {
                    var webSocket = await aspNetContext.WebSockets.AcceptWebSocketAsync();

                    var webSocketDialog = new WebSocketDialog(webSocket, request.Aborted);

                    foreach (var middleware in _wsMiddle.Concat(handlers))
                    {
                        status = await middleware(request, response, webSocketDialog);

                        if (status != HandlerType.Continue)
                        {
                            return(status);
                        }
                    }

                    await webSocketDialog.ReadFromWebSocket();

                    return(status);
                }

                response.Headers["Upgrade"] = "Websocket";
                await response.SendStatus(HttpStatusCode.UpgradeRequired);

                return(HandlerType.Error);
            }
            catch (OperationCanceledException)
            {
                return(HandlerType.Final);
            }
            catch (Exception e)
            {
                return(await HandleException(request, response, status, e));
            }
        }
Exemple #2
0
        private async Task <HandlerType> WrapWebsocketHandler(HttpContext aspNetContext, WsHandlerWrapper handlerWrapper)
        {
            var context  = new Context(aspNetContext, Plugins);
            var request  = context.Request;
            var response = context.Response;

            var status = HandlerType.Continue;

            try
            {
                if (aspNetContext.WebSockets.IsWebSocketRequest)
                {
                    var webSocket = await aspNetContext.WebSockets.AcceptWebSocketAsync();

                    var webSocketDialog = new WebSocketDialog(context, webSocket);
                    foreach (var middleware in _wsMiddlewareStack)
                    {
                        status = await middleware.Invoke(request, webSocketDialog, response);

                        if (status != HandlerType.Continue)
                        {
                            return(status);
                        }
                    }
                    if (status == HandlerType.Continue)
                    {
                        status = await handlerWrapper.Invoke(request, webSocketDialog, response);
                    }
                    await webSocketDialog.ReadFromWebSocket();

                    return(status);
                }
                else
                {
                    await response.SendStatus(HttpStatusCode.BadRequest);

                    return(HandlerType.Error);
                }
            }
            catch (Exception e)
            {
                return(await HandleException(context, status, e));
            }
        }
Exemple #3
0
        private async Task WrapWebsocketHandler(HttpContext context, WsHandlerWrapper handlerWrapper)
        {
            var req = new Request(context, Plugins);
            var res = new Response(context, Plugins);

            try
            {
                if (context.WebSockets.IsWebSocketRequest)
                {
                    var webSocket = await context.WebSockets.AcceptWebSocketAsync();

                    var wsd = new WebSocketDialog(context, webSocket, Plugins);
                    foreach (var middleware in _wsMiddlewareStack)
                    {
                        if (res.Closed)
                        {
                            break;
                        }
                        await middleware.Process(req, wsd, res);
                    }
                    await handlerWrapper.Invoke(req, wsd, res);

                    await wsd.ReadFromWebSocket();
                }
                else
                {
                    await res.SendStatus(HttpStatusCode.BadRequest);
                }
            }
            catch (Exception e)
            {
                if (!res.Closed)
                {
                    if (!RespondWithExceptionDetails)
                    {
                        await res.SendStatus(HttpStatusCode.InternalServerError);
                    }
                    else
                    {
                        await res.SendString(e.ToString(), status : HttpStatusCode.InternalServerError);
                    }
                }
            }
        }
        private async Task WrapWebsocketHandler(HttpContext context, Action <Request, WebSocketDialog> handler)
        {
            var req = new Request(context.Request, Plugins);
            var res = new Response(context.Response, Plugins);

            try
            {
                if (context.WebSockets.IsWebSocketRequest)
                {
                    var url       = context.Request.Path.Value;
                    var webSocket = await context.WebSockets.AcceptWebSocketAsync();

                    var wsd = new WebSocketDialog(context, webSocket, Plugins);
                    foreach (var middleware in _wsMiddlewareStack)
                    {
                        if (!await middleware.Process(url, req, res, wsd) || res.Closed)
                        {
                            return;
                        }
                    }
                    handler(req, wsd);
                    await wsd.ReadFromWebSocket();
                }
                else
                {
                    if (!res.Closed)
                    {
                        await res.SendStatus(HttpStatusCode.BadRequest);
                    }
                }
            }
            catch (Exception e)
            {
                if (!res.Closed)
                {
                    await res.SendStatus(HttpStatusCode.InternalServerError);
                }
            }
        }