Exemple #1
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);
                    }
                }
            }
        }
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));
            }
        }