Send events back to Ooui from JS

This commit is contained in:
Frank A. Krueger 2018-03-09 19:39:01 -08:00
parent 5d47c9adef
commit 03781339f4
No known key found for this signature in database
GPG Key ID: 0471C67474FFE664
4 changed files with 44 additions and 31 deletions

View File

@ -9,9 +9,10 @@ public class Program
var l = new Label { Text = "Hello" }; var l = new Label { Text = "Hello" };
var b = new Button ("Click Me"); var b = new Button ("Click Me");
var e = new Div (new Div (l), b); var e = new Div (new Div (l), b);
var c = 0; int count = 0;
b.Click += (s, ee) => { b.Click += (s, ee) => {
b.Text = $"Clicked {c} times"; count++;
b.Text = $"Clicked {count} times";
}; };
UI.SetGlobalElement ("main", e); UI.SetGlobalElement ("main", e);

View File

@ -39,6 +39,7 @@
"System.Reflection.Extensions.dll", "System.Reflection.Extensions.dll",
"System.Runtime.dll", "System.Runtime.dll",
"System.Runtime.Extensions.dll", "System.Runtime.Extensions.dll",
"System.Runtime.Serialization.dll",
"System.Runtime.Serialization.Primitives.dll", "System.Runtime.Serialization.Primitives.dll",
"System.Xml.dll", "System.Xml.dll",
"System.Xml.ReaderWriter.dll", "System.Xml.ReaderWriter.dll",

View File

@ -6,6 +6,17 @@ const nodes = {};
const hasText = {}; const hasText = {};
let socket = null; let socket = null;
let wasmSession = null;
function send (json) {
if (debug) console.log ("Send", json);
if (socket != null) {
socket.send (json);
}
else if (wasmSession != null) {
WebAssemblyApp.receiveMessagesJson (wasmSession, json);
}
}
const mouseEvents = { const mouseEvents = {
click: true, click: true,
@ -117,8 +128,7 @@ function ooui (rootElementPath) {
}; };
saveSize (em.v); saveSize (em.v);
const ems = JSON.stringify (em); const ems = JSON.stringify (em);
if (socket != null) send (ems);
socket.send (ems);
if (debug) console.log ("Event", em); if (debug) console.log ("Event", em);
} }
}()); }());
@ -130,10 +140,6 @@ function oouiWasm (mainAsmName, mainNamspace, mainClassName, mainMethodNmae, ass
var initialSize = getSize (); var initialSize = getSize ();
function send (json) {
if (debug) console.log ("Send", json);
}
function resizeHandler() { function resizeHandler() {
const em = { const em = {
m: "event", m: "event",
@ -269,8 +275,7 @@ function msgListen (m) {
}; };
} }
const ems = JSON.stringify (em); const ems = JSON.stringify (em);
if (socket != null) send (ems);
socket.send (ems);
if (debug) console.log ("Event", em); if (debug) console.log ("Event", em);
if (em.k === "submit") if (em.k === "submit")
e.preventDefault (); e.preventDefault ();
@ -324,9 +329,9 @@ function fixupValue (v) {
window["__oouiReceiveMessages"] = function (sessionId, messages) window["__oouiReceiveMessages"] = function (sessionId, messages)
{ {
console.log ("RCV", messages); if (debug) console.log ("WebAssembly Receive", messages);
messages.forEach (function (m) { messages.forEach (function (m) {
console.log ('Raw value from server', m.v); // console.log ('Raw value from server', m.v);
m.v = fixupValue (m.v); m.v = fixupValue (m.v);
processMessage (m); processMessage (m);
}); });
@ -334,13 +339,13 @@ window["__oouiReceiveMessages"] = function (sessionId, messages)
var Module = { var Module = {
onRuntimeInitialized: function () { onRuntimeInitialized: function () {
console.log ("Done with WASM module instantiation."); if (debug) console.log ("Done with WASM module instantiation.");
Module.FS_createPath ("/", "managed", true, true); Module.FS_createPath ("/", "managed", true, true);
var pending = 0; var pending = 0;
this.assemblies.forEach (function(asm_name) { this.assemblies.forEach (function(asm_name) {
console.log ("Loading", asm_name); if (debug) console.log ("Loading", asm_name);
++pending; ++pending;
fetch ("managed/" + asm_name, { credentials: 'same-origin' }).then (function (response) { fetch ("managed/" + asm_name, { credentials: 'same-origin' }).then (function (response) {
if (!response.ok) if (!response.ok)
@ -357,7 +362,7 @@ var Module = {
}, },
bclLoadingDone: function () { bclLoadingDone: function () {
console.log ("Done loading the BCL."); if (debug) console.log ("Done loading the BCL.");
MonoRuntime.init (); MonoRuntime.init ();
} }
}; };
@ -374,7 +379,7 @@ var MonoRuntime = {
this.load_runtime ("managed", 1); this.load_runtime ("managed", 1);
console.log ("Done initializing the runtime."); if (debug) console.log ("Done initializing the runtime.");
WebAssemblyApp.init (); WebAssemblyApp.init ();
}, },
@ -425,38 +430,48 @@ var WebAssemblyApp = {
runApp: function (a, b) { runApp: function (a, b) {
try { try {
var rres = MonoRuntime.call_method (this.add_method, null, [MonoRuntime.mono_string (a), MonoRuntime.mono_string (b)]); var sessionId = "main";
var rres = MonoRuntime.call_method (this.main_method, null, [MonoRuntime.mono_string (a), MonoRuntime.mono_string (b)]);
var res = MonoRuntime.conv_string (rres); var res = MonoRuntime.conv_string (rres);
MonoRuntime.call_method (this.ooui_method, null, [MonoRuntime.mono_string ("main"), MonoRuntime.mono_string ("main")]); MonoRuntime.call_method (this.ooui_StartWebAssemblySession_method, null, [MonoRuntime.mono_string (sessionId), MonoRuntime.mono_string ("main")]);
wasmSession = sessionId;
return res; return res;
} catch (e) { } catch (e) {
return e.msg; return e.msg;
} }
}, },
receiveMessagesJson: function (sessionId, json) {
MonoRuntime.call_method (this.ooui_ReceiveWebAssemblySessionMessageJson_method, null, [MonoRuntime.mono_string (sessionId), MonoRuntime.mono_string (json)]);
},
findMethods: function () { findMethods: function () {
this.ooui_module = MonoRuntime.assembly_load ("Ooui") this.ooui_module = MonoRuntime.assembly_load ("Ooui");
if (!this.ooui_module) if (!this.ooui_module)
throw "Could not find Ooui.dll"; throw "Could not find Ooui.dll";
this.ooui_class = MonoRuntime.find_class (this.ooui_module, "Ooui", "UI") this.ooui_class = MonoRuntime.find_class (this.ooui_module, "Ooui", "UI");
if (!this.ooui_class) if (!this.ooui_class)
throw "Could not find UI class in Ooui module"; throw "Could not find UI class in Ooui module";
this.ooui_method = MonoRuntime.find_method (this.ooui_class, "StartWebAssemblySession", -1) this.ooui_StartWebAssemblySession_method = MonoRuntime.find_method (this.ooui_class, "StartWebAssemblySession", -1);
if (!this.ooui_method) if (!this.ooui_StartWebAssemblySession_method)
throw "Could not find StartWebAssemblySession method"; throw "Could not find StartWebAssemblySession method";
this.main_module = MonoRuntime.assembly_load (mainAsmName) this.ooui_ReceiveWebAssemblySessionMessageJson_method = MonoRuntime.find_method (this.ooui_class, "ReceiveWebAssemblySessionMessageJson", -1);
if (!this.ooui_ReceiveWebAssemblySessionMessageJson_method)
throw "Could not find ReceiveWebAssemblySessionMessageJson method";
this.main_module = MonoRuntime.assembly_load (mainAsmName);
if (!this.main_module) if (!this.main_module)
throw "Could not find Main Module " + mainAsmName + ".dll"; throw "Could not find Main Module " + mainAsmName + ".dll";
this.math_class = MonoRuntime.find_class (this.main_module, "", "Program") this.main_class = MonoRuntime.find_class (this.main_module, "", "Program")
if (!this.math_class) if (!this.main_class)
throw "Could not find Program class in main module"; throw "Could not find Program class in main module";
this.add_method = MonoRuntime.find_method (this.math_class, "Main", -1) this.main_method = MonoRuntime.find_method (this.main_class, "Main", -1)
if (!this.add_method) if (!this.main_method)
throw "Could not find Main method"; throw "Could not find Main method";
}, },
}; };

View File

@ -55,7 +55,6 @@ namespace Ooui
public void ReceiveMessageJson (string json) public void ReceiveMessageJson (string json)
{ {
try { try {
Info ("RECEIVED: " + json);
var message = Newtonsoft.Json.JsonConvert.DeserializeObject<Message> (json); var message = Newtonsoft.Json.JsonConvert.DeserializeObject<Message> (json);
element.Receive (message); element.Receive (message);
} }
@ -66,7 +65,6 @@ namespace Ooui
public void StartSession () public void StartSession ()
{ {
WebAssembly.Runtime.InvokeJS ("console.log('was start session 0')");
// //
// Start watching for changes in the element // Start watching for changes in the element
// //
@ -79,9 +77,7 @@ namespace Ooui
element.Style.Width = initialWidth; element.Style.Width = initialWidth;
element.Style.Height = initialHeight; element.Style.Height = initialHeight;
} }
WebAssembly.Runtime.InvokeJS ("console.log('was start session 1')");
QueueMessage (Message.Call ("document.body", "appendChild", element)); QueueMessage (Message.Call ("document.body", "appendChild", element));
WebAssembly.Runtime.InvokeJS ("console.log('was start session end')");
} }
public void StopSession () public void StopSession ()