Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1286)

Unified Diff: native_client_sdk/src/examples/api/messaging/example.js

Issue 781623002: [NaCl SDK] Messaging example (demonstrates postMessageAndAwaitResponse) (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 6 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: native_client_sdk/src/examples/api/messaging/example.js
diff --git a/native_client_sdk/src/examples/api/messaging/example.js b/native_client_sdk/src/examples/api/messaging/example.js
new file mode 100644
index 0000000000000000000000000000000000000000..4c5a6828cea023b76edbf6cf938324179d60157e
--- /dev/null
+++ b/native_client_sdk/src/examples/api/messaging/example.js
@@ -0,0 +1,39 @@
+// Copyright (c) 2014 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+function $(id) {
+ return document.querySelector(id);
+}
dmichael (off chromium) 2014/12/03 23:33:33 I wouldn't define $ in an example... you can just
binji 2014/12/03 23:56:50 Done.
+
+function attachListeners() {
+ var number1El = $('#addend1');
+ var number2El = $('#addend2');
+ var resultEl = $('#result');
+
+ document.getElementById('addAsync').addEventListener('click', function() {
+ var value1 = parseInt(number1El.value);
+ var value2 = parseInt(number2El.value);
+ common.naclModule.postMessage([value1, value2]);
+
+ // The result is returned in handleMessage below.
+ });
+
+ document.getElementById('addSync').addEventListener('click', function() {
+ var value1 = parseInt(number1El.value);
+ var value2 = parseInt(number2El.value);
+ var result =
+ common.naclModule.postMessageAndAwaitResponse([value1, value2]);
+
+ // This is the result returned from the module synchronously (i.e. when the
+ // addSync button is pressed)
+ resultEl.textContent = result;
+ });
+}
+
+// Called by the common.js module.
+function handleMessage(message_event) {
+ // This is the result returned from the module asynchronously (i.e. when the
+ // addAsync button is pressed)
+ result.textContent = message_event.data;
+}

Powered by Google App Engine
This is Rietveld 408576698