Esempio n. 1
0
        public void ProcessRequest(HttpContext context)
        {
            //context.Response.ContentType = "text/html";
            context.Response.Cache.SetCacheability(HttpCacheability.NoCache);
            context.Response.Cache.SetExpires(DateTime.Now.AddMinutes(0));
            context.Response.Cache.SetMaxAge(new TimeSpan(0));
            context.Response.AddHeader("Last-Modified", DateTime.Now.ToLongDateString());

            var handlerPair = Handlers.FirstOrDefault(pair => context.Request.Url.Segments.LastOrDefault()._f(_ => _.StartsWith(pair.Key + ".")));
            var handler     = handlerPair._f(_ => _.Value);

            if (handler == null)
            {
                context.Response.StatusCode = 404;
                return;
            }
            var handlerName = handlerPair.Key;

            if (context.Request.Url.AbsolutePath.EndsWith(".js"))
            {
                //var prevCycle = int.Parse(HttpUtility.ParseQueryString(context.Request.Url.Query).Get("cycle"));
                var prevCycle  = int.Parse(context.Request.QueryString["cycle"]);
                var updates    = Updates(context);
                var prevUpdate = updates.FirstOrDefault(_update => _update.Handler == handlerName && _update.Cycle == prevCycle);
                var prevPage   = prevUpdate != null ? prevUpdate.Page : null;
                //var prevState = updates.LastOrDefault(_update => _update.Handler == handlerName)._f(_=>_.State);
                var prevState = prevUpdate._f(_ => _.State);

                var        jsonTexts     = context.Request.Form.GetValues("commands[]");
                JsonData[] json_commands = null;
                if (jsonTexts != null && jsonTexts.Length > 0)
                {
                    json_commands = jsonTexts.Select(text => new JsonData(JsonSerializer.Deserialize(new Newtonsoft.Json.JsonTextReader(new System.IO.StringReader(text))))).ToArray();
                    //if (json_commands != null && json_commands.Length > 1) //HACK
                    //  json_commands = json_commands.Take(1).ToArray();
                }

                var watcher = new System.Diagnostics.Stopwatch();
                watcher.Start();
                HtmlResult <HElement> result = handler(prevState, json_commands, new HContext(handlerPair.Key, context));

                var update = PushUpdate(context, handlerName, result.Html, result.State);

                var page = result.Html;

                var isPartial = page.Name.LocalName != "html";

                var js_updates = HtmlJavaScriptSynchronizer.JsSync(new HElementProvider(), prevPage == null ? null : isPartial ? prevPage : prevPage.Element("body"), isPartial ? page : page.Element("body")).ToArray();
                var jupdate    = new Dictionary <string, object>()
                {
                    { "cycle", update.Cycle }, { "prev_cycle", prevCycle }, { "processed_commands", json_commands.Else_Empty().Length }, { "updates", js_updates }
                };

                if (context.Request.Url.AbsolutePath.EndsWith(".text.js"))
                {
                    context.Response.ContentType = "text/plain";
                }
                else
                {
                    context.Response.ContentType = "text/json";
                }

                //var jsSerializer = new System.Web.Script.Serialization.JavaScriptSerializer();
                //var json = jsSerializer.Serialize(jupdate);
                //context.Response.Write(json);
                JsonSerializer.Serialize(context.Response.Output, jupdate);
                watcher.Stop();
                update.Elapsed = watcher.Elapsed;
            }
            else
            {
                var lastState = Updates(context).LastOrDefault(_update => _update.Handler == handlerName)._f(_ => _.State);

                var result = handler(lastState, null, new HContext(handlerPair.Key, context));
                var page   = result.Html;
                if (!context.Request.Url.AbsolutePath.EndsWith(".raw.html"))
                {
                    var head = page.Element("head") ?? new HElement("head");

                    var update = PushUpdate(context, handlerName, new HElement("html", head, new HElement("body")), result.State);

                    var startHead = new HElement(head.Name,
                                                 head.Attributes,
                                                 //SoftTech.Wui.HWebSynchronizeHandler.Scripts(handlerName, update.Cycle, refreshPeriod: TimeSpan.FromSeconds(10)),
                                                 head.Nodes,
                                                 SoftTech.Wui.HWebSynchronizeHandler.Scripts_Inline(handlerName, update.Cycle, refreshPeriod: TimeSpan.FromSeconds(10))
                                                 //new HElement("script", new HAttribute("src", "/sync.js"), "")

                                                 );
                    page = new HElement("html", startHead, new HElement("body"));
                }

                context.Response.Write(page.ToString());
            }
            //context.Response.Write("Hello World");
            //Data++;
            //context.Response.Write(context.Request.Url);
            //context.Response.Write(context.Request.RawUrl);
            //context.Response.Write(Data);
        }
Esempio n. 2
0
        public string ProcessRequest(Dictionary <string, string> parameters, JsonData json)
        {
            var uri         = parameters.Find("SCRIPT_NAME") + parameters.Find("PATH_INFO");
            var queryString = parameters.Find("QUERY_STRING");

            Console.WriteLine("{0} {1}, {2}", uri, queryString, Handlers.Select(pair => pair.Key).FirstOrDefault());

            var handlerPair = Handlers.FirstOrDefault(pair => uri._f(_ => _.StartsWith(pair.Key)));

            if (handlerPair.Value == null)
            {
                return("<html><body>invalid page</body></html>");
            }
            var handler  = handlerPair.Value;
            var pageName = handlerPair.Key;

            if (uri.EndsWith(".js"))
            {
                var query = queryString.Else_Empty().Split('&').Select(pair => pair.Split('='))
                            .ToDictionary(ss => ss.FirstOrDefault(), ss => ss.ElementAtOrDefault(1));
                var prevCycle  = int.Parse(query.Find("cycle"));
                var prevUpdate = Updates.FirstOrDefault(_update => _update.Cycle == prevCycle);
                var prevPage   = prevUpdate != null ? prevUpdate.Page : null;

                var update = PushUpdate(null);

                var result = handler(pageName, update.Cycle, json);
                //lock (locker)
                //{
                //  update.Page = result.Html;

                //}
                var page = result.Html;


                var commands = HtmlJavaScriptSynchronizer.JsSync(new HElementProvider(), prevPage == null ? null : prevPage.Element("body"), page.Element("body")).ToArray();
                var jupdate  = new Dictionary <string, object>()
                {
                    { "cycle", update.Cycle }, { "prev_cycle", prevCycle }, { "commands", commands }
                };

                var jsSerializer = new System.Web.Script.Serialization.JavaScriptSerializer();
                return(jsSerializer.Serialize(jupdate));
            }
            else
            {
                var update = PushUpdate(null);

                var result = handler(pageName, update.Cycle, json);
                var page   = result.Html;
                if (!uri.EndsWith(".raw.html"))
                {
                    page = h.Html(new HRaw(page.Element("head").ToString_Fair()), h.Body());
                }

                //lock (locker)
                //{
                //  update.Page = page;
                //}
                return(page.ToString());
            }
        }
 public override IEnumerable <object> GetSyncCommands(XElement page)
 {
     return(HtmlJavaScriptSynchronizer.JsSync(new XElementProvider(), lastPage.Element("body"), page.Element("body")));
 }