From dbcc301cf640bcbbe058b1c9505cd192144b6bfc Mon Sep 17 00:00:00 2001 From: "Frank A. Krueger" Date: Thu, 15 Jun 2017 00:58:55 -0700 Subject: [PATCH] Round-trip events --- Ooui/Button.cs | 4 ++-- Ooui/Client.js | 18 +++++++++++++++--- Ooui/UI.cs | 12 ++++++++++-- Tests/ButtonTests.cs | 2 +- 4 files changed, 28 insertions(+), 8 deletions(-) diff --git a/Ooui/Button.cs b/Ooui/Button.cs index 6fdd9b1..501b247 100644 --- a/Ooui/Button.cs +++ b/Ooui/Button.cs @@ -17,8 +17,8 @@ namespace Ooui } public event EventHandler Clicked { - add => AddEventListener ("onclick", value); - remove => RemoveEventListener ("onclick", value); + add => AddEventListener ("click", value); + remove => RemoveEventListener ("click", value); } public Button () diff --git a/Ooui/Client.js b/Ooui/Client.js index abe5d20..9a1ed3a 100644 --- a/Ooui/Client.js +++ b/Ooui/Client.js @@ -51,6 +51,21 @@ function msgCall (m) { } function msgListen (m) { + const node = getNode (m.id); + if (!node) { + console.error ("Unknown node id", m); + return; + } + node.addEventListener(m.k, function () { + const em = { + m: "event", + id: m.id, + k: m.k, + }; + const ems = JSON.stringify (em); + socket.send (ems); + console.log ("EVENT", em); + }); } function processMessage (m) { @@ -92,13 +107,10 @@ function fixupValue (v) { return v; } -// Connection opened socket.addEventListener('open', function (event) { console.log("WebSocket opened"); - socket.send('Hello Server!'); }); -// Listen for messages socket.addEventListener('message', function (event) { const message = JSON.parse (event.data); message.v = fixupValue (message.v); diff --git a/Ooui/UI.cs b/Ooui/UI.cs index 3cb1600..ba0ee1e 100644 --- a/Ooui/UI.cs +++ b/Ooui/UI.cs @@ -83,7 +83,7 @@ namespace Ooui Console.ForegroundColor = ConsoleColor.Green; Console.WriteLine ($"Stopping..."); Console.ResetColor (); - + scts.Cancel (); } @@ -277,7 +277,15 @@ namespace Ooui size += receiveResult.Count; } var receivedString = Encoding.UTF8.GetString (receiveBuffer, 0, size); - Console.WriteLine ("RECEIVED: {0}", receivedString); + + try { + Console.WriteLine ("RECEIVED: {0}", receivedString); + var message = Newtonsoft.Json.JsonConvert.DeserializeObject (receivedString); + element.Receive (message); + } + catch (Exception ex) { + Error ("Failed to process received message", ex); + } // var outputBuffer = new ArraySegment (Encoding.UTF8.GetBytes ($"You said: {receivedString}")); // await webSocket.SendAsync (outputBuffer, WebSocketMessageType.Text, true, token).ConfigureAwait (false); diff --git a/Tests/ButtonTests.cs b/Tests/ButtonTests.cs index c1e7f75..0361c47 100644 --- a/Tests/ButtonTests.cs +++ b/Tests/ButtonTests.cs @@ -38,7 +38,7 @@ namespace Tests }; Assert.IsTrue (listened); Assert.IsFalse (clicked); - b.Receive (Message.Event (b.Id, "onclick")); + b.Receive (Message.Event (b.Id, "click")); Assert.IsTrue (clicked); } }