Connect WebSocket to correct Element
This commit is contained in:
parent
a6e19e7923
commit
9ee271559f
|
@ -1,6 +1,6 @@
|
||||||
|
|
||||||
// Create WebSocket connection.
|
// 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");
|
console.log("Socket created");
|
||||||
|
|
||||||
|
|
|
@ -19,10 +19,10 @@ namespace Ooui
|
||||||
static Server ()
|
static Server ()
|
||||||
{
|
{
|
||||||
var asm = typeof(Server).Assembly;
|
var asm = typeof(Server).Assembly;
|
||||||
System.Console.WriteLine("ASM = {0}", asm);
|
// System.Console.WriteLine("ASM = {0}", asm);
|
||||||
foreach (var n in asm.GetManifestResourceNames()) {
|
// foreach (var n in asm.GetManifestResourceNames()) {
|
||||||
System.Console.WriteLine(" {0}", n);
|
// System.Console.WriteLine(" {0}", n);
|
||||||
}
|
// }
|
||||||
using (var s = asm.GetManifestResourceStream ("Ooui.Client.js")) {
|
using (var s = asm.GetManifestResourceStream ("Ooui.Client.js")) {
|
||||||
using (var r = new StreamReader (s)) {
|
using (var r = new StreamReader (s)) {
|
||||||
clientJsBytes = Encoding.UTF8.GetBytes (r.ReadToEnd ());
|
clientJsBytes = Encoding.UTF8.GetBytes (r.ReadToEnd ());
|
||||||
|
@ -37,10 +37,26 @@ namespace Ooui
|
||||||
|
|
||||||
public async Task RunAsync (string listenerPrefix, CancellationToken token)
|
public async Task RunAsync (string listenerPrefix, CancellationToken token)
|
||||||
{
|
{
|
||||||
var listener = new HttpListener ();
|
HttpListener listener = null;
|
||||||
|
|
||||||
|
var started = false;
|
||||||
|
while (!started) {
|
||||||
|
try {
|
||||||
|
listener = new HttpListener ();
|
||||||
listener.Prefixes.Add (listenerPrefix);
|
listener.Prefixes.Add (listenerPrefix);
|
||||||
listener.Start ();
|
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.WriteLine ($"Listening at {listenerPrefix}...");
|
||||||
|
Console.ResetColor ();
|
||||||
|
|
||||||
while (!token.IsCancellationRequested) {
|
while (!token.IsCancellationRequested) {
|
||||||
var listenerContext = await listener.GetContextAsync ().ConfigureAwait (false);
|
var listenerContext = await listener.GetContextAsync ().ConfigureAwait (false);
|
||||||
|
@ -85,9 +101,7 @@ namespace Ooui
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else if (publishedPaths.TryGetValue (path, out ctor)) {
|
else if (publishedPaths.TryGetValue (path, out ctor)) {
|
||||||
var element = ctor ();
|
WriteElementHtml (path, response);
|
||||||
RegisterElement (element);
|
|
||||||
WriteElementHtml (element, response);
|
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
response.StatusCode = 404;
|
response.StatusCode = 404;
|
||||||
|
@ -95,18 +109,16 @@ namespace Ooui
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void RegisterElement (Element element)
|
void WriteElementHtml (string elementPath, HttpListenerResponse response)
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
void WriteElementHtml (Element element, HttpListenerResponse response)
|
|
||||||
{
|
{
|
||||||
response.StatusCode = 200;
|
response.StatusCode = 200;
|
||||||
response.ContentType = "text/html";
|
response.ContentType = "text/html";
|
||||||
response.ContentEncoding = Encoding.UTF8;
|
response.ContentEncoding = Encoding.UTF8;
|
||||||
var html = Encoding.UTF8.GetBytes ($@"<html>
|
var html = Encoding.UTF8.GetBytes ($@"<html>
|
||||||
<head><title>{element}</title></head>
|
<head><title>{elementPath}</title></head>
|
||||||
<body><script src=""/client.js""> </script></body>
|
<body>
|
||||||
|
<script>rootElementPath = ""{elementPath}"";</script>
|
||||||
|
<script src=""/client.js""> </script></body>
|
||||||
</html>");
|
</html>");
|
||||||
response.ContentLength64 = html.LongLength;
|
response.ContentLength64 = html.LongLength;
|
||||||
using (var s = response.OutputStream) {
|
using (var s = response.OutputStream) {
|
||||||
|
@ -117,20 +129,40 @@ namespace Ooui
|
||||||
|
|
||||||
async void ProcessWebSocketRequest (HttpListenerContext listenerContext, CancellationToken token)
|
async void ProcessWebSocketRequest (HttpListenerContext listenerContext, CancellationToken token)
|
||||||
{
|
{
|
||||||
|
var url = listenerContext.Request.Url;
|
||||||
|
var path = url.LocalPath;
|
||||||
|
|
||||||
|
Func<Element> ctor;
|
||||||
|
if (!publishedPaths.TryGetValue (path, out ctor)) {
|
||||||
|
listenerContext.Response.StatusCode = 404;
|
||||||
|
listenerContext.Response.Close ();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
Element element = null;
|
||||||
|
try {
|
||||||
|
element = ctor ();
|
||||||
|
}
|
||||||
|
catch (Exception ex) {
|
||||||
|
listenerContext.Response.StatusCode = 500;
|
||||||
|
listenerContext.Response.Close();
|
||||||
|
Error ("Failed to create element", ex);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
WebSocketContext webSocketContext = null;
|
WebSocketContext webSocketContext = null;
|
||||||
try {
|
try {
|
||||||
webSocketContext = await listenerContext.AcceptWebSocketAsync(subProtocol: "ooui-1.0").ConfigureAwait (false);
|
webSocketContext = await listenerContext.AcceptWebSocketAsync(subProtocol: "ooui-1.0").ConfigureAwait (false);
|
||||||
Console.WriteLine ("Accepted WebSocket: {0}", webSocketContext);
|
Console.WriteLine ("WEBSOCKET {0}", listenerContext.Request.Url.LocalPath);
|
||||||
}
|
}
|
||||||
catch (Exception e) {
|
catch (Exception ex) {
|
||||||
listenerContext.Response.StatusCode = 500;
|
listenerContext.Response.StatusCode = 500;
|
||||||
listenerContext.Response.Close();
|
listenerContext.Response.Close();
|
||||||
Console.WriteLine ("Failed to accept WebSocket: {0}", e);
|
Error ("Failed to accept WebSocket", ex);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
WebSocket webSocket = null;
|
WebSocket webSocket = null;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
webSocket = webSocketContext.WebSocket;
|
webSocket = webSocketContext.WebSocket;
|
||||||
|
|
||||||
|
@ -163,12 +195,22 @@ namespace Ooui
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch (Exception e) {
|
catch (WebSocketException ex) when (ex.WebSocketErrorCode == WebSocketError.ConnectionClosedPrematurely) {
|
||||||
Console.WriteLine ("Exception: {0}", e);
|
// The remote party closed the WebSocket connection without completing the close handshake.
|
||||||
|
}
|
||||||
|
catch (Exception ex) {
|
||||||
|
Error ("Failed to process web socket", ex);
|
||||||
}
|
}
|
||||||
finally {
|
finally {
|
||||||
webSocket?.Dispose();
|
webSocket?.Dispose ();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void Error (string message, Exception ex)
|
||||||
|
{
|
||||||
|
Console.ForegroundColor = ConsoleColor.Red;
|
||||||
|
Console.WriteLine ("{0}: {1}", message, ex);
|
||||||
|
Console.ResetColor ();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,7 +7,6 @@ namespace Samples
|
||||||
{
|
{
|
||||||
static int Main (string[] args)
|
static int Main (string[] args)
|
||||||
{
|
{
|
||||||
Console.WriteLine ("Hello World!");
|
|
||||||
var server = new Server ();
|
var server = new Server ();
|
||||||
var button = new Button();
|
var button = new Button();
|
||||||
server.Publish ("/button", button);
|
server.Publish ("/button", button);
|
||||||
|
|
Loading…
Reference in New Issue