Beispiel #1
0
        public void  OnContext(Reactor.Http.HttpContext context)
        {
            if (this.path != context.Request.Url.AbsolutePath)
            {
                this.servercb(context);

                return;
            }

            this.Upgrade(context, (exception, socket) => {
                if (exception != null)
                {
                    if (this.OnError != null)
                    {
                        this.OnError(exception);

                        return;
                    }
                }

                if (socket != null)
                {
                    if (this.OnSocket != null)
                    {
                        this.OnSocket(socket);
                    }
                }
            });
        }
        public static ServerWebSocketUpgradeRequest Create(Reactor.Http.HttpContext context)
        {
            try
            {
                var path = context.Request.Url.AbsolutePath;

                var method = context.Request.Method;

                var protocol = context.Request.ProtocolVersion;

                var upgrade = context.Request.Headers["Upgrade"];

                var connection = context.Request.Headers["Connection"];

                if (protocol == HttpVersion.Version11 && method.ToLower() == "get" && upgrade.ToLower().Contains("websocket") && connection.ToLower().Contains("upgrade"))
                {
                    return(new ServerWebSocketUpgradeRequest(context));
                }

                return(null);
            }
            catch
            {
                return(null);
            }
        }
        private ServerWebSocketUpgradeRequest(Reactor.Http.HttpContext context)
        {
            this.Context = context;

            this.SecWebSocketExtensions = context.Request.Headers["Sec-WebSocket-Extensions"];

            this.SecWebSocketKey = context.Request.Headers["Sec-WebSocket-Key"];

            this.SecWebSocketVersion = context.Request.Headers["Sec-WebSocket-Version"];
        }
Beispiel #4
0
        public Context(Reactor.Http.HttpContext context)
        {
            this.Request = context.Request;

            this.Response = context.Response;

            this.Connection = context.Connection;

            this.User = context.User;

            this.userdata = new Dictionary <string, object>();
        }
Beispiel #5
0
        private void Upgrade(Reactor.Http.HttpContext context, Reactor.Action <Exception, Reactor.Web.Socket.Socket> callback)
        {
            var request = ServerWebSocketUpgradeRequest.Create(context);

            //--------------------------------------------------------
            // if not a web socket attempt, defer to http callback.
            //--------------------------------------------------------

            if (request == null)
            {
                this.servercb(context);

                return;
            }

            var response = ServerWebSocketUpgradeResponse.Create(request);

            var socket_context = new Reactor.Web.Socket.Context(context);

            this.OnUpgrade(socket_context, (success, reason) => {
                if (!success)
                {
                    response.Reject(reason == null ? "" : reason, (exception) => callback(exception, null));

                    return;
                }

                response.Accept((exception) => {
                    if (exception != null)
                    {
                        callback(exception, null);

                        return;
                    }

                    var channel = new Transport(context.Connection);

                    var socket = new Socket(channel);

                    socket.Context = socket_context;

                    callback(null, socket);
                });
            });
        }