Round-trip events

This commit is contained in:
Frank A. Krueger 2017-06-15 00:58:55 -07:00
parent 1033273484
commit dbcc301cf6
4 changed files with 28 additions and 8 deletions

View File

@ -17,8 +17,8 @@ namespace Ooui
} }
public event EventHandler Clicked { public event EventHandler Clicked {
add => AddEventListener ("onclick", value); add => AddEventListener ("click", value);
remove => RemoveEventListener ("onclick", value); remove => RemoveEventListener ("click", value);
} }
public Button () public Button ()

View File

@ -51,6 +51,21 @@ function msgCall (m) {
} }
function msgListen (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) { function processMessage (m) {
@ -92,13 +107,10 @@ function fixupValue (v) {
return v; return v;
} }
// Connection opened
socket.addEventListener('open', function (event) { socket.addEventListener('open', function (event) {
console.log("WebSocket opened"); console.log("WebSocket opened");
socket.send('Hello Server!');
}); });
// Listen for messages
socket.addEventListener('message', function (event) { socket.addEventListener('message', function (event) {
const message = JSON.parse (event.data); const message = JSON.parse (event.data);
message.v = fixupValue (message.v); message.v = fixupValue (message.v);

View File

@ -83,7 +83,7 @@ namespace Ooui
Console.ForegroundColor = ConsoleColor.Green; Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine ($"Stopping..."); Console.WriteLine ($"Stopping...");
Console.ResetColor (); Console.ResetColor ();
scts.Cancel (); scts.Cancel ();
} }
@ -277,7 +277,15 @@ namespace Ooui
size += receiveResult.Count; size += receiveResult.Count;
} }
var receivedString = Encoding.UTF8.GetString (receiveBuffer, 0, size); 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<Message> (receivedString);
element.Receive (message);
}
catch (Exception ex) {
Error ("Failed to process received message", ex);
}
// var outputBuffer = new ArraySegment<byte> (Encoding.UTF8.GetBytes ($"You said: {receivedString}")); // var outputBuffer = new ArraySegment<byte> (Encoding.UTF8.GetBytes ($"You said: {receivedString}"));
// await webSocket.SendAsync (outputBuffer, WebSocketMessageType.Text, true, token).ConfigureAwait (false); // await webSocket.SendAsync (outputBuffer, WebSocketMessageType.Text, true, token).ConfigureAwait (false);

View File

@ -38,7 +38,7 @@ namespace Tests
}; };
Assert.IsTrue (listened); Assert.IsTrue (listened);
Assert.IsFalse (clicked); Assert.IsFalse (clicked);
b.Receive (Message.Event (b.Id, "onclick")); b.Receive (Message.Event (b.Id, "click"));
Assert.IsTrue (clicked); Assert.IsTrue (clicked);
} }
} }