diff --git a/Ooui/Client.js b/Ooui/Client.js index 5e047d5..6dc56cb 100644 --- a/Ooui/Client.js +++ b/Ooui/Client.js @@ -1,6 +1,6 @@ // Create WebSocket connection. -const socket = new WebSocket ("ws://localhost:8080", "ooui-1.0"); +const socket = new WebSocket ("ws://localhost:8080" + rootElementPath, "ooui-1.0"); console.log("Socket created"); diff --git a/Ooui/Server.cs b/Ooui/Server.cs index f4f51f2..c581d61 100644 --- a/Ooui/Server.cs +++ b/Ooui/Server.cs @@ -19,10 +19,10 @@ namespace Ooui static Server () { var asm = typeof(Server).Assembly; - System.Console.WriteLine("ASM = {0}", asm); - foreach (var n in asm.GetManifestResourceNames()) { - System.Console.WriteLine(" {0}", n); - } + // 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 ()); @@ -37,10 +37,26 @@ namespace Ooui public async Task RunAsync (string listenerPrefix, CancellationToken token) { - var listener = new HttpListener (); - listener.Prefixes.Add (listenerPrefix); - listener.Start (); + HttpListener listener = null; + + var started = false; + while (!started) { + try { + listener = new HttpListener (); + listener.Prefixes.Add (listenerPrefix); + listener.Start (); + started = true; + } + catch (System.Net.Sockets.SocketException ex) when + (ex.SocketErrorCode == System.Net.Sockets.SocketError.AddressAlreadyInUse) { + var wait = 5; + Console.WriteLine ($"{listenerPrefix} is in use, trying again in {wait} seconds..."); + await Task.Delay (wait * 1000); + } + } + Console.ForegroundColor = ConsoleColor.Green; Console.WriteLine ($"Listening at {listenerPrefix}..."); + Console.ResetColor (); while (!token.IsCancellationRequested) { var listenerContext = await listener.GetContextAsync ().ConfigureAwait (false); @@ -85,9 +101,7 @@ namespace Ooui } } else if (publishedPaths.TryGetValue (path, out ctor)) { - var element = ctor (); - RegisterElement (element); - WriteElementHtml (element, response); + WriteElementHtml (path, response); } else { response.StatusCode = 404; @@ -95,18 +109,16 @@ namespace Ooui } } - void RegisterElement (Element element) - { - } - - void WriteElementHtml (Element element, HttpListenerResponse response) + void WriteElementHtml (string elementPath, HttpListenerResponse response) { response.StatusCode = 200; response.ContentType = "text/html"; response.ContentEncoding = Encoding.UTF8; var html = Encoding.UTF8.GetBytes ($@" -