Ooui-tws-port/Ooui.AspNetCore/ElementResult.cs

58 lines
2.1 KiB
C#
Raw Normal View History

2017-11-10 03:34:37 +00:00
using System;
2018-02-02 04:18:16 +00:00
using System.Text;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
2018-02-02 04:18:16 +00:00
using Microsoft.AspNetCore.Mvc;
2017-11-10 03:34:37 +00:00
namespace Ooui.AspNetCore
{
public class ElementResult : ActionResult
2018-02-02 04:18:16 +00:00
{
2017-11-16 04:20:09 +00:00
readonly Element element;
2018-02-02 04:18:16 +00:00
readonly string title;
2017-11-16 04:20:09 +00:00
public ElementResult (Element element, string title = "")
2018-02-02 04:18:16 +00:00
{
2017-11-16 04:20:09 +00:00
this.element = element;
2018-02-02 04:18:16 +00:00
this.title = title;
2017-11-10 03:34:37 +00:00
}
2018-02-02 04:18:16 +00:00
public override async Task ExecuteResultAsync (ActionContext context)
2017-11-10 03:34:37 +00:00
{
var response = context.HttpContext.Response;
response.StatusCode = 200;
2017-11-10 05:00:15 +00:00
response.ContentType = "text/html; charset=utf-8";
response.Headers["Cache-Control"] = "no-cache, no-store, must-revalidate";
if (element.WantsFullScreen) {
element.Style.Width = GetCookieDouble (context.HttpContext.Request.Cookies, "oouiWindowWidth", 32, 640, 10000);
element.Style.Height = GetCookieDouble (context.HttpContext.Request.Cookies, "oouiWindowHeight", 24, 480, 10000);
}
2017-11-10 05:00:15 +00:00
var sessionId = WebSocketHandler.BeginSession (context.HttpContext, element);
2018-02-02 04:18:16 +00:00
var initialHtml = element.OuterHtml;
var html = UI.RenderTemplate (WebSocketHandler.WebSocketPath + "?id=" + sessionId, title: title, initialHtml: initialHtml);
2017-11-16 04:20:09 +00:00
var htmlBytes = Encoding.UTF8.GetBytes (html);
response.ContentLength = htmlBytes.Length;
2017-11-10 03:34:37 +00:00
using (var s = response.Body) {
2017-11-16 04:20:09 +00:00
await s.WriteAsync (htmlBytes, 0, htmlBytes.Length).ConfigureAwait (false);
2017-11-10 03:34:37 +00:00
}
}
static double GetCookieDouble (IRequestCookieCollection cookies, string key, double min, double def, double max)
{
if (cookies.TryGetValue (key, out var s)) {
if (double.TryParse (s, out var d)) {
if (d < min) return min;
if (d > max) return max;
return d;
}
return def;
}
else {
return def;
}
}
2017-11-10 03:34:37 +00:00
}
}