Round-trip events
This commit is contained in:
parent
1033273484
commit
dbcc301cf6
|
@ -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 ()
|
||||
|
|
|
@ -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);
|
||||
|
|
10
Ooui/UI.cs
10
Ooui/UI.cs
|
@ -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);
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue