From 5d47c9adef55b8067f772fbab62ca9a017038e16 Mon Sep 17 00:00:00 2001 From: "Frank A. Krueger" Date: Fri, 9 Mar 2018 19:11:57 -0800 Subject: [PATCH] Wasm is working --- Ooui.Wasm/ooui-sample.cs | 4 ++++ Ooui/Client.js | 18 +++++++++++++----- Ooui/WebAssemblySession.cs | 23 ++++++++++++----------- 3 files changed, 29 insertions(+), 16 deletions(-) diff --git a/Ooui.Wasm/ooui-sample.cs b/Ooui.Wasm/ooui-sample.cs index d53d6ad..bcfb254 100644 --- a/Ooui.Wasm/ooui-sample.cs +++ b/Ooui.Wasm/ooui-sample.cs @@ -9,6 +9,10 @@ public class Program var l = new Label { Text = "Hello" }; var b = new Button ("Click Me"); var e = new Div (new Div (l), b); + var c = 0; + b.Click += (s, ee) => { + b.Text = $"Clicked {c} times"; + }; UI.SetGlobalElement ("main", e); return e.ToString (); diff --git a/Ooui/Client.js b/Ooui/Client.js index e89fbe2..028ca5e 100644 --- a/Ooui/Client.js +++ b/Ooui/Client.js @@ -322,6 +322,16 @@ function fixupValue (v) { // == WASM Support == +window["__oouiReceiveMessages"] = function (sessionId, messages) +{ + console.log ("RCV", messages); + messages.forEach (function (m) { + console.log ('Raw value from server', m.v); + m.v = fixupValue (m.v); + processMessage (m); + }); +}; + var Module = { onRuntimeInitialized: function () { console.log ("Done with WASM module instantiation."); @@ -405,22 +415,20 @@ var MonoRuntime = { var WebAssemblyApp = { init: function () { this.loading = document.getElementById ("loading"); - this.output = document.getElementById ("output"); this.findMethods (); var res = this.runApp ("1", "2"); - this.output.value = res; - this.output.hidden = false; this.loading.hidden = true; }, runApp: function (a, b) { try { + var rres = MonoRuntime.call_method (this.add_method, null, [MonoRuntime.mono_string (a), MonoRuntime.mono_string (b)]); + var res = MonoRuntime.conv_string (rres); MonoRuntime.call_method (this.ooui_method, null, [MonoRuntime.mono_string ("main"), MonoRuntime.mono_string ("main")]); - var res = MonoRuntime.call_method (this.add_method, null, [MonoRuntime.mono_string (a), MonoRuntime.mono_string (b)]); - return MonoRuntime.conv_string (res); + return res; } catch (e) { return e.msg; } diff --git a/Ooui/WebAssemblySession.cs b/Ooui/WebAssemblySession.cs index 47199ac..7ea2f20 100644 --- a/Ooui/WebAssemblySession.cs +++ b/Ooui/WebAssemblySession.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Text; namespace Ooui { @@ -17,16 +18,12 @@ namespace Ooui protected override void QueueMessage (Message message) { - WebAssembly.Runtime.InvokeJS ("console.log('q 0')"); base.QueueMessage (message); - WebAssembly.Runtime.InvokeJS ("console.log('q 1')"); TransmitQueuedMessages (); - WebAssembly.Runtime.InvokeJS ("console.log('q end')"); } void TransmitQueuedMessages () { - WebAssembly.Runtime.InvokeJS ("console.log('t 0')"); // // Dequeue as many messages as we can // @@ -36,19 +33,23 @@ namespace Ooui queuedMessages.Clear (); } - WebAssembly.Runtime.InvokeJS ("console.log('t 1')"); - if (messagesToSend.Count == 0) return; - WebAssembly.Runtime.InvokeJS ("console.log('t 2')"); - // // Now actually send the messages // - //var json = Newtonsoft.Json.JsonConvert.SerializeObject (messagesToSend); - WebAssembly.Runtime.InvokeJS ("alert(" + messagesToSend.Count + ")"); - WebAssembly.Runtime.InvokeJS ("console.log('t end')"); + var sb = new StringBuilder (); + var head = ""; + sb.Append ("["); + foreach (var m in messagesToSend) { + sb.Append (head); + sb.Append (m.ToJson ()); + head = ","; + } + sb.Append ("]"); + var json = sb.ToString (); + WebAssembly.Runtime.InvokeJS ("__oouiReceiveMessages(\"" + id + "\", " + json + ")"); } public void ReceiveMessageJson (string json)