public async Task Invoke(HttpContext context)
        {
            if (!context.WebSockets.IsWebSocketRequest)
            {
                if (_next != null)
                {
                    await _next(context);
                }
                return;
            }

            var selectedProtocol = GetProtocol(
                context.Request.Headers["Sec-WebSocket-Protocol"]);

            var socket = await(selectedProtocol == null
                                ? context.WebSockets.AcceptWebSocketAsync()
                                : context.WebSockets.AcceptWebSocketAsync(selectedProtocol));

            var socker = new WebSocker(socket, context);

            await Handler.OnConnected(socker);

            await Receive(socker, async (result, message) =>
            {
                if (result.MessageType == WebSocketMessageType.Close)
                {
                    await Handler.OnDisconnected(socker);
                    return;
                }

                var proto         = selectedProtocol ?? Protocols[0];
                var passedMessage = new HandlerMessage
                {
                    Socket   = socker, SocketResult = result, Serialized = message, Subprotocol = proto,
                    BasePath = context.Request.Path, HttpContext = context
                };
                await Handler.Route(new Request {
                    Context = passedMessage
                });
            });
        }
Exemple #2
0
        protected virtual async Task Dispatch(HandlerMessage context, MethodInfo method, params object[] list)
        {
            Context = context;

            var type = GetType();

            var beforeMethod = type.GetMethod("Before");

            if (beforeMethod != null)
            {
                var beforeResponse = await Utils.ExtractValTask <bool>(beforeMethod.Invoke(this, new object[] { }));

                if (beforeResponse == false)
                {
                    return;
                }
            }

            var mParamsLength = method.GetParameters().Length;

            if (list.Length < mParamsLength)
            {
                var newList = new object[mParamsLength];
                for (var i = 0; i < mParamsLength; i++)
                {
                    newList[i] = i > list.Length - 1 ? Type.Missing : list[i];
                }
                list = newList;
            }

            await Utils.ExtractVoidTask(method.Invoke(this, list));

            var afterMethod = type.GetMethod("After");

            if (afterMethod != null)
            {
                await Utils.ExtractVoidTask(afterMethod.Invoke(this, new object[] { }));
            }
        }
Exemple #3
0
 protected Task Dispatch(HandlerMessage context, string methodName, params object[] vars) =>
 Dispatch(context, GetType().GetMethod(methodName), vars);