From 176de592cce41243cf0c58ca406d278a030636d7 Mon Sep 17 00:00:00 2001 From: "Frank A. Krueger" Date: Mon, 12 Jun 2017 18:24:30 -0700 Subject: [PATCH] Add Client.js --- Ooui/Client.js | 4 ++++ Ooui/Ooui.csproj | 3 +++ Ooui/Server.cs | 43 ++++++++++++++++++++++++++++++++++++++----- 3 files changed, 45 insertions(+), 5 deletions(-) create mode 100644 Ooui/Client.js diff --git a/Ooui/Client.js b/Ooui/Client.js new file mode 100644 index 0000000..1868524 --- /dev/null +++ b/Ooui/Client.js @@ -0,0 +1,4 @@ + + +alert("Hello"); + diff --git a/Ooui/Ooui.csproj b/Ooui/Ooui.csproj index fdfd7da..e10537f 100644 --- a/Ooui/Ooui.csproj +++ b/Ooui/Ooui.csproj @@ -5,4 +5,7 @@ + + + \ No newline at end of file diff --git a/Ooui/Server.cs b/Ooui/Server.cs index 510e1f1..e5a3c13 100644 --- a/Ooui/Server.cs +++ b/Ooui/Server.cs @@ -14,6 +14,22 @@ namespace Ooui readonly Dictionary> publishedPaths = new Dictionary> (); + readonly static byte[] clientJsBytes; + + static Server () + { + var asm = typeof(Server).Assembly; + System.Console.WriteLine("ASM = {0}", asm); + foreach (var n in asm.GetManifestResourceNames()) { + System.Console.WriteLine(" {0}", n); + } + using (var s = asm.GetManifestResourceStream ("Ooui.Client.js")) { + using (var r = new StreamReader (s)) { + clientJsBytes = Encoding.UTF8.GetBytes (r.ReadToEnd ()); + } + } + } + public Task RunAsync (string listenerPrefix) { return RunAsync (listenerPrefix, CancellationToken.None); @@ -55,15 +71,26 @@ namespace Ooui Console.WriteLine ($"{listenerContext.Request.HttpMethod} {url.LocalPath}"); + var response = listenerContext.Response; + Func ctor; - if (publishedPaths.TryGetValue (path, out ctor)) { + + if (path == "/client.js") { + response.ContentLength64 = clientJsBytes.LongLength; + response.ContentType = "application/javascript"; + response.ContentEncoding = Encoding.UTF8; + using (var s = response.OutputStream) { + s.Write (clientJsBytes, 0, clientJsBytes.Length); + } + } + else if (publishedPaths.TryGetValue (path, out ctor)) { var element = ctor (); RegisterElement (element); - WriteElementHtml (element, listenerContext.Response); + WriteElementHtml (element, response); } else { - listenerContext.Response.StatusCode = 404; - listenerContext.Response.Close (); + response.StatusCode = 404; + response.Close (); } } @@ -74,9 +101,15 @@ namespace Ooui void WriteElementHtml (Element element, HttpListenerResponse response) { response.StatusCode = 200; + response.ContentType = "text/html"; + response.ContentEncoding = Encoding.UTF8; using (var s = response.OutputStream) { using (var w = new StreamWriter (s, Encoding.UTF8)) { - w.WriteLine ($"Hello {element}"); + w.WriteLine ($""); + w.WriteLine ($"{element}"); + w.WriteLine ($""); + w.WriteLine ($""); + w.WriteLine ($""); } } response.Close ();