static UpdateCycle <HElement> PushUpdate(HElement page) { lock (locker) { var lastUpdate = Updates.LastOrDefault(); var cycle = (lastUpdate != null ? lastUpdate.Cycle : 0) + 1; var update = new UpdateCycle <HElement>(null, cycle, page, null); Updates = Updates.Skip(Math.Max(Updates.Length - 100, 0)).Concat(new[] { update }).ToArray(); return(update); } }
static UpdateCycle <HElement> PushUpdate(HttpContext context, string handlerName, HElement page, object state) //HACK updateCycle меняется после добавления в очередь { lock (Updates_Locker(context)) { var updates = Updates(context); var lastUpdate = updates.LastOrDefault(); var cycle = (lastUpdate != null ? lastUpdate.Cycle : 0) + 1; var update = new UpdateCycle <HElement>(handlerName, cycle, page, state); updates = updates.Skip(Math.Max(updates.Length - 100, 0)).Concat(new[] { update }).ToArray(); context.Session["Wui.updates"] = updates; return(update); } }
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); }