Ejemplo n.º 1
0
        public ActionResult ExecuteWithWS(HttpRequest request, HttpApiServer server, JToken token)
        {
            ActionResult result = new ActionResult();
            JToken       url    = token["url"];

            WebSockets.DataFrame dataFrame = server.CreateDataFrame(result);
            if (url == null)
            {
                result.Code  = 403;
                result.Error = "not support url info notfound!";
                request.Session.Send(dataFrame);
                return(result);
            }
            result.Url = url.Value <string>();
            string baseurl = HttpParse.CharToLower(result.Url);

            if (baseurl[0] != '/')
            {
                baseurl = "/" + baseurl;
            }
            ActionHandler handler = GetAction(baseurl);

            if (handler == null)
            {
                server.BaseServer.Log(EventArgs.LogType.Warring, request.Session, baseurl + " not found");
                result.Code  = 404;
                result.Error = "url " + baseurl + " notfound!";
                request.Session.Send(dataFrame);
            }
            else
            {
                try
                {
                    JToken data = token["params"];
                    if (data == null)
                    {
                        data = (JToken)Newtonsoft.Json.JsonConvert.DeserializeObject("{}");
                    }
                    WebsocketContext dc      = new WebsocketContext(server, request, data);
                    ActionContext    context = new ActionContext(handler, dc);
                    context.Execute();
                    if (!dc.AsyncResult)
                    {
                        result.Data = context.Result;
                        request.Session.Send(dataFrame);
                    }
                }
                catch (Exception e_)
                {
                    server.BaseServer.Log(EventArgs.LogType.Error, request.Session, "{0} inner error {1}@{2}", request.Url, e_.Message, e_.StackTrace);
                    result.Code  = 500;
                    result.Error = e_.Message;
                    if (server.ServerConfig.OutputStackTrace)
                    {
                        result.StackTrace = e_.StackTrace;
                    }
                    request.Session.Send(dataFrame);
                }
            }
            return(result);
        }
Ejemplo n.º 2
0
        public ActionResult ExecuteWithWS(HttpRequest request, HttpApiServer server, JToken token)
        {
            ActionResult result = new ActionResult();
            JToken       url    = token["url"];

            WebSockets.DataFrame dataFrame = server.CreateDataFrame(result);
            if (url == null)
            {
                if (server.EnableLog(EventArgs.LogType.Warring))
                {
                    server.BaseServer.Log(EventArgs.LogType.Warring, request.Session, "websocket {0} not support, url info notfound!", request.ClientIPAddress);
                }
                result.Code  = 403;
                result.Error = "not support, url info notfound!";
                request.Session.Send(dataFrame);
                return(result);
            }
            result.Url = url.Value <string>();
            string baseurl = HttpParse.CharToLower(result.Url);

            if (baseurl[0] != '/')
            {
                baseurl = "/" + baseurl;
            }
            result.Url = baseurl;
            JToken data = token["params"];

            if (data == null)
            {
                data = (JToken)Newtonsoft.Json.JsonConvert.DeserializeObject("{}");
            }
            JToken requestid = data["_requestid"];

            if (requestid != null)
            {
                result.ID = requestid.Value <string>();
            }
            ActionHandler handler = GetAction(baseurl);

            if (handler == null)
            {
                if (server.EnableLog(EventArgs.LogType.Warring))
                {
                    server.BaseServer.Log(EventArgs.LogType.Warring, request.Session, "websocket {0} execute {1} notfound", request.ClientIPAddress, result.Url);
                }
                result.Code  = 404;
                result.Error = "url " + baseurl + " notfound!";
                request.Session.Send(dataFrame);
            }
            else
            {
                try
                {
                    WebsocketContext dc = new WebsocketContext(server, request, data);
                    dc.ActionUrl = baseurl;
                    dc.RequestID = result.ID;
                    ActionContext context = new ActionContext(handler, dc);
                    context.Execute();
                    if (!dc.AsyncResult)
                    {
                        if (context.Result is ActionResult)
                        {
                            result    = (ActionResult)context.Result;
                            result.ID = dc.RequestID;
                            if (result.Url == null)
                            {
                                result.Url = dc.ActionUrl;
                            }
                            dataFrame.Body = result;
                        }
                        else
                        {
                            result.Data = context.Result;
                        }
                        dataFrame.Send(request.Session);
                        //request.Session.Send(dataFrame);
                    }
                }
                catch (Exception e_)
                {
                    if (server.EnableLog(EventArgs.LogType.Error))
                    {
                        server.BaseServer.Log(EventArgs.LogType.Error, request.Session, "websocket {0} execute {1} inner error {2}@{3}", request.ClientIPAddress, request.Url, e_.Message, e_.StackTrace);
                    }
                    result.Code  = 500;
                    result.Error = e_.Message;
                    if (server.ServerConfig.OutputStackTrace)
                    {
                        result.StackTrace = e_.StackTrace;
                    }
                    dataFrame.Send(request.Session);
                    //request.Session.Send(dataFrame);
                }
            }
            return(result);
        }