diff --git a/Ooui/Client.js b/Ooui/Client.js index 481a6e2..3eb82db 100644 --- a/Ooui/Client.js +++ b/Ooui/Client.js @@ -1,18 +1,39 @@ const debug = false; -// Create WebSocket connection. -const socket = new WebSocket ("ws://" + document.location.host + rootElementPath, "ooui"); - -console.log("Web socket created"); - const nodes = {}; +let socket = null; + +function ooui (rootElementPath) { + socket = new WebSocket ("ws://" + document.location.host + rootElementPath, "ooui"); + + socket.addEventListener('open', function (event) { + console.log("Web socket opened"); + }); + + socket.addEventListener('message', function (event) { + const messages = JSON.parse (event.data); + if (debug) console.log("Messages", messages); + if (Array.isArray (messages)) { + messages.forEach (function (m) { + // console.log('Raw value from server', m.v); + m.v = fixupValue (m.v); + processMessage (m); + }); + } + }); + + console.log("Web socket created"); +} + function getNode (id) { switch (id) { case "window": return window; case "document": return document; - case "document.body": return document.body; + case "document.body": + const bodyNode = document.getElementById ("ooui-body"); + return bodyNode || document.body; default: return nodes[id]; } } @@ -115,19 +136,3 @@ function fixupValue (v) { } return v; } - -socket.addEventListener('open', function (event) { - console.log("Web socket opened"); -}); - -socket.addEventListener('message', function (event) { - const messages = JSON.parse (event.data); - if (debug) console.log("Messages", messages); - if (Array.isArray (messages)) { - messages.forEach (function (m) { - // console.log('Raw value from server', m.v); - m.v = fixupValue (m.v); - processMessage (m); - }); - } -}); diff --git a/Ooui/UI.cs b/Ooui/UI.cs index ef3be04..c663766 100644 --- a/Ooui/UI.cs +++ b/Ooui/UI.cs @@ -18,6 +18,20 @@ namespace Ooui static readonly byte[] clientJsBytes; + public static string Template { get; set; } = $@" + + + @ElementPath + + + + +
+ + + +"; + static string host = "*"; public static string Host { get => host; @@ -139,7 +153,7 @@ namespace Ooui Func ctor; - if (path == "/client.js") { + if (path == "/ooui.js") { response.ContentLength64 = clientJsBytes.LongLength; response.ContentType = "application/javascript"; response.ContentEncoding = Encoding.UTF8; @@ -161,17 +175,17 @@ namespace Ooui } } + static string RenderTemplate (string elementPath) + { + return Template.Replace ("@ElementPath", elementPath); + } + static void WriteElementHtml (string elementPath, HttpListenerResponse response) { response.StatusCode = 200; response.ContentType = "text/html"; response.ContentEncoding = Encoding.UTF8; - var html = Encoding.UTF8.GetBytes ($@" -{elementPath} - - - -"); + var html = Encoding.UTF8.GetBytes (RenderTemplate (elementPath)); response.ContentLength64 = html.LongLength; using (var s = response.OutputStream) { s.Write (html, 0, html.Length);