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 {
add => AddEventListener ("onclick", value);
remove => RemoveEventListener ("onclick", value);
add => AddEventListener ("click", value);
remove => RemoveEventListener ("click", value);
}
public Button ()

View File

@ -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);

View File

@ -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<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}"));
// await webSocket.SendAsync (outputBuffer, WebSocketMessageType.Text, true, token).ConfigureAwait (false);

View File

@ -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);
}
}