| OLD | NEW |
| 1 {{+bindTo:partials.standard_nacl_article}} | 1 {{+bindTo:partials.standard_nacl_article}} |
| 2 | 2 |
| 3 <section id="messaging-system"> | 3 <section id="messaging-system"> |
| 4 <span id="message-system"></span><h1 id="messaging-system"><span id="message-sys
tem"></span>Messaging System</h1> | 4 <span id="message-system"></span><h1 id="messaging-system"><span id="message-sys
tem"></span>Messaging System</h1> |
| 5 <div class="contents local" id="contents" style="display: none"> | 5 <div class="contents local" id="contents" style="display: none"> |
| 6 <ul class="small-gap"> | 6 <ul class="small-gap"> |
| 7 <li><a class="reference internal" href="#reference-information" id="id2">Referen
ce information</a></li> | 7 <li><a class="reference internal" href="#reference-information" id="id2">Referen
ce information</a></li> |
| 8 <li><p class="first"><a class="reference internal" href="#introduction-to-the-me
ssaging-system" id="id3">Introduction to the messaging system</a></p> | 8 <li><p class="first"><a class="reference internal" href="#introduction-to-the-me
ssaging-system" id="id3">Introduction to the messaging system</a></p> |
| 9 <ul class="small-gap"> | 9 <ul class="small-gap"> |
| 10 <li><a class="reference internal" href="#design-of-the-messaging-system" id="id4
">Design of the messaging system</a></li> | 10 <li><a class="reference internal" href="#design-of-the-messaging-system" id="id4
">Design of the messaging system</a></li> |
| (...skipping 25 matching lines...) Expand all Loading... |
| 36 Native Client application. It introduces the concept of asynchronous | 36 Native Client application. It introduces the concept of asynchronous |
| 37 programming and the basic steps required to set up a Native Client module | 37 programming and the basic steps required to set up a Native Client module |
| 38 that sends messages to and receive messages from JavaScript. This chapter | 38 that sends messages to and receive messages from JavaScript. This chapter |
| 39 assumes you are familiar with the material presented in the | 39 assumes you are familiar with the material presented in the |
| 40 <a class="reference internal" href="/native-client/devguide/coding/application-s
tructure.html"><em>Application Structure</em></a> chapter.</p> | 40 <a class="reference internal" href="/native-client/devguide/coding/application-s
tructure.html"><em>Application Structure</em></a> chapter.</p> |
| 41 <aside class="note"> | 41 <aside class="note"> |
| 42 The “Hello, World” example for getting started with NaCl is used her
e to | 42 The “Hello, World” example for getting started with NaCl is used her
e to |
| 43 illustrate basic programming techniques. You can find this code in | 43 illustrate basic programming techniques. You can find this code in |
| 44 the <code>/getting_started/part2</code> directory in the Native Client SDK downl
oad. | 44 the <code>/getting_started/part2</code> directory in the Native Client SDK downl
oad. |
| 45 </aside> | 45 </aside> |
| 46 <section id="reference-information"> | |
| 47 <h2 id="reference-information">Reference information</h2> | 46 <h2 id="reference-information">Reference information</h2> |
| 48 <p>For reference information related to the Pepper messaging API, see the | 47 <p>For reference information related to the Pepper messaging API, see the |
| 49 following documentation:</p> | 48 following documentation:</p> |
| 50 <ul class="small-gap"> | 49 <ul class="small-gap"> |
| 51 <li><a class="reference external" href="/native-client/pepper_stable/cpp/classpp
_1_1_instance">pp::Instance class</a> | 50 <li><a class="reference external" href="/native-client/pepper_stable/cpp/classpp
_1_1_instance">pp::Instance class</a> |
| 52 HandleMessage(), PostMessage())</li> | 51 HandleMessage(), PostMessage())</li> |
| 53 <li><a class="reference external" href="/native-client/pepper_stable/cpp/classpp
_1_1_module">pp::Module class</a></li> | 52 <li><a class="reference external" href="/native-client/pepper_stable/cpp/classpp
_1_1_module">pp::Module class</a></li> |
| 54 <li><a class="reference external" href="/native-client/pepper_stable/cpp/classpp
_1_1_var">pp::Var class</a></li> | 53 <li><a class="reference external" href="/native-client/pepper_stable/cpp/classpp
_1_1_var">pp::Var class</a></li> |
| 55 </ul> | 54 </ul> |
| 56 </section><section id="introduction-to-the-messaging-system"> | |
| 57 <h2 id="introduction-to-the-messaging-system">Introduction to the messaging syst
em</h2> | 55 <h2 id="introduction-to-the-messaging-system">Introduction to the messaging syst
em</h2> |
| 58 <p>Native Client modules and JavaScript communicate by sending messages to each | 56 <p>Native Client modules and JavaScript communicate by sending messages to each |
| 59 other. The most basic form of a message is a string. Messages support many | 57 other. The most basic form of a message is a string. Messages support many |
| 60 JavaScript types, including ints, arrays, array buffers, and dictionaries (see | 58 JavaScript types, including ints, arrays, array buffers, and dictionaries (see |
| 61 <a class="reference external" href="/native-client/pepper_stable/cpp/classpp_1_1
_var">pp::Var</a>, | 59 <a class="reference external" href="/native-client/pepper_stable/cpp/classpp_1_1
_var">pp::Var</a>, |
| 62 <a class="reference external" href="/native-client/pepper_stable/cpp/classpp_1_1
_var_array_buffer">pp:VarArrayBuffer</a>, and the | 60 <a class="reference external" href="/native-client/pepper_stable/cpp/classpp_1_1
_var_array_buffer">pp:VarArrayBuffer</a>, and the |
| 63 general <a class="reference external" href="/native-client/pepper_stable/c/struc
t_p_p_b___messaging__1__0">messaging system documentation</a>). It’s up t
o | 61 general <a class="reference external" href="/native-client/pepper_stable/c/struc
t_p_p_b___messaging__1__0">messaging system documentation</a>). It’s up t
o |
| 64 you to decide on the type of message and define how to process the messages on | 62 you to decide on the type of message and define how to process the messages on |
| 65 both the JavaScript and Native Client side. For the “Hello, World” e
xample, we | 63 both the JavaScript and Native Client side. For the “Hello, World” e
xample, we |
| 66 will work with string-typed messages only.</p> | 64 will work with string-typed messages only.</p> |
| 67 <p>When JavaScript posts a message to the Native Client module, the | 65 <p>When JavaScript posts a message to the Native Client module, the |
| 68 Pepper <code>HandleMessage()</code> function is invoked on the module | 66 Pepper <code>HandleMessage()</code> function is invoked on the module |
| 69 side. Similarly, the Native Client module can post a message to | 67 side. Similarly, the Native Client module can post a message to |
| 70 JavaScript, and this message triggers a JavaScript event listener for | 68 JavaScript, and this message triggers a JavaScript event listener for |
| 71 <code>message</code> events in the DOM. (See the W3C specification on | 69 <code>message</code> events in the DOM. (See the W3C specification on |
| 72 <a class="reference external" href="http://www.w3.org/TR/DOM-Level-2-Events/even
ts.html">Document Object Model Events</a> for more | 70 <a class="reference external" href="http://www.w3.org/TR/DOM-Level-2-Events/even
ts.html">Document Object Model Events</a> for more |
| 73 information.) In the “Hello, World” example, the JavaScript function
s for | 71 information.) In the “Hello, World” example, the JavaScript function
s for |
| 74 posting and handling messages are named <code>postMessage()</code> and | 72 posting and handling messages are named <code>postMessage()</code> and |
| 75 <code>handleMessage()</code> (but any names could be used). On the Native Client | 73 <code>handleMessage()</code> (but any names could be used). On the Native Client |
| 76 C++ side, the Pepper Library functions for posting and handling | 74 C++ side, the Pepper Library functions for posting and handling |
| 77 messages are:</p> | 75 messages are:</p> |
| 78 <ul class="small-gap"> | 76 <ul class="small-gap"> |
| 79 <li><code>void pp::Instance::PostMessage(const Var &message)</code></li> | 77 <li><code>void pp::Instance::PostMessage(const Var &message)</code></li> |
| 80 <li><code>virtual void pp::Instance::HandleMessage(const Var &message)</code
></li> | 78 <li><code>virtual void pp::Instance::HandleMessage(const Var &message)</code
></li> |
| 81 </ul> | 79 </ul> |
| 82 <p>If you want to receive messages from JavaScript, you need to implement the | 80 <p>If you want to receive messages from JavaScript, you need to implement the |
| 83 <code>pp::Instance::HandleMessage()</code> function in your Native Client module
.</p> | 81 <code>pp::Instance::HandleMessage()</code> function in your Native Client module
.</p> |
| 84 <section id="design-of-the-messaging-system"> | |
| 85 <h3 id="design-of-the-messaging-system">Design of the messaging system</h3> | 82 <h3 id="design-of-the-messaging-system">Design of the messaging system</h3> |
| 86 <p>The Native Client messaging system is analogous to the system used by | 83 <p>The Native Client messaging system is analogous to the system used by |
| 87 the browser to allow web workers to communicate (see the <a class="reference ext
ernal" href="http://www.w3.org/TR/workers">W3 web | 84 the browser to allow web workers to communicate (see the <a class="reference ext
ernal" href="http://www.w3.org/TR/workers">W3 web |
| 88 worker specification</a>). The Native | 85 worker specification</a>). The Native |
| 89 Client messaging system is designed to keep the web page responsive while the | 86 Client messaging system is designed to keep the web page responsive while the |
| 90 Native Client module is performing potentially heavy processing in the | 87 Native Client module is performing potentially heavy processing in the |
| 91 background. When JavaScript sends a message to the Native Client | 88 background. When JavaScript sends a message to the Native Client |
| 92 module, the <code>postMessage()</code> call returns as soon as it sends its mess
age | 89 module, the <code>postMessage()</code> call returns as soon as it sends its mess
age |
| 93 to the Native Client module. The JavaScript does not wait for a reply | 90 to the Native Client module. The JavaScript does not wait for a reply |
| 94 from Native Client, thus avoiding bogging down the main JavaScript | 91 from Native Client, thus avoiding bogging down the main JavaScript |
| 95 thread. On the JavaScript side, you set up an event listener to | 92 thread. On the JavaScript side, you set up an event listener to |
| 96 respond to the message sent by the Native Client module when it has | 93 respond to the message sent by the Native Client module when it has |
| 97 finished the requested processing and returns a message.</p> | 94 finished the requested processing and returns a message.</p> |
| 98 <p>This asynchronous processing model keeps the main thread free while | 95 <p>This asynchronous processing model keeps the main thread free while |
| 99 avoiding the following problems:</p> | 96 avoiding the following problems:</p> |
| 100 <ul class="small-gap"> | 97 <ul class="small-gap"> |
| 101 <li>The JavaScript engine hangs while waiting for a synchronous call to return.<
/li> | 98 <li>The JavaScript engine hangs while waiting for a synchronous call to return.<
/li> |
| 102 <li>The browser pops up a dialog when a JavaScript entry point takes longer | 99 <li>The browser pops up a dialog when a JavaScript entry point takes longer |
| 103 than a few moments.</li> | 100 than a few moments.</li> |
| 104 <li>The application hangs while waiting for an unresponsive Native Client module
.</li> | 101 <li>The application hangs while waiting for an unresponsive Native Client module
.</li> |
| 105 </ul> | 102 </ul> |
| 106 </section></section><section id="communication-tasks-in-the-hello-world-example"
> | |
| 107 <h2 id="communication-tasks-in-the-hello-world-example">Communication tasks in t
he “Hello, World” example</h2> | 103 <h2 id="communication-tasks-in-the-hello-world-example">Communication tasks in t
he “Hello, World” example</h2> |
| 108 <p>The following sections describe how the “Hello, World” example po
sts | 104 <p>The following sections describe how the “Hello, World” example po
sts |
| 109 and handles messages on both the JavaScript side and the Native Client | 105 and handles messages on both the JavaScript side and the Native Client |
| 110 side of the application.</p> | 106 side of the application.</p> |
| 111 <section id="javascript-code"> | |
| 112 <h3 id="javascript-code">JavaScript code</h3> | 107 <h3 id="javascript-code">JavaScript code</h3> |
| 113 <p>The JavaScript code and HTML in the “Hello, World” example can be | 108 <p>The JavaScript code and HTML in the “Hello, World” example can be |
| 114 found in the <code>example.js</code>, <code>common.js</code>, and <code>index.ht
ml</code> files. | 109 found in the <code>example.js</code>, <code>common.js</code>, and <code>index.ht
ml</code> files. |
| 115 The important steps are:</p> | 110 The important steps are:</p> |
| 116 <ol class="arabic simple"> | 111 <ol class="arabic simple"> |
| 117 <li>Sets up an event listener to listen for <code>message</code> events from the | 112 <li>Sets up an event listener to listen for <code>message</code> events from the |
| 118 Native Client module.</li> | 113 Native Client module.</li> |
| 119 <li>Implements an event handler that the event listener invokes to handle | 114 <li>Implements an event handler that the event listener invokes to handle |
| 120 incoming <code>message</code> events.</li> | 115 incoming <code>message</code> events.</li> |
| 121 <li>Calls <code>postMessage()</code> to communicate with the NaCl module, | 116 <li>Calls <code>postMessage()</code> to communicate with the NaCl module, |
| 122 after the page loads.</li> | 117 after the page loads.</li> |
| 123 </ol> | 118 </ol> |
| 124 <section id="step-1-from-common-js"> | |
| 125 <h4 id="step-1-from-common-js">Step 1: From common.js</h4> | 119 <h4 id="step-1-from-common-js">Step 1: From common.js</h4> |
| 126 <pre class="prettyprint"> | 120 <pre class="prettyprint"> |
| 127 function attachDefaultListeners() { | 121 function attachDefaultListeners() { |
| 128 // The NaCl module embed is created within the listenerDiv | 122 // The NaCl module embed is created within the listenerDiv |
| 129 var listenerDiv = document.getElementById('listener'); | 123 var listenerDiv = document.getElementById('listener'); |
| 130 // ... | 124 // ... |
| 131 | 125 |
| 132 // register the handleMessage function as the message event handler. | 126 // register the handleMessage function as the message event handler. |
| 133 listenerDiv.addEventListener('message', handleMessage, true); | 127 listenerDiv.addEventListener('message', handleMessage, true); |
| 134 // ... | 128 // ... |
| 135 } | 129 } |
| 136 </pre> | 130 </pre> |
| 137 </section><section id="step-2-from-example-js"> | |
| 138 <h4 id="step-2-from-example-js">Step 2: From example.js</h4> | 131 <h4 id="step-2-from-example-js">Step 2: From example.js</h4> |
| 139 <pre class="prettyprint"> | 132 <pre class="prettyprint"> |
| 140 // This function is called by common.js when a message is received from the | 133 // This function is called by common.js when a message is received from the |
| 141 // NaCl module. | 134 // NaCl module. |
| 142 function handleMessage(message) { | 135 function handleMessage(message) { |
| 143 // In the example, we simply log the data that's received in the message. | 136 // In the example, we simply log the data that's received in the message. |
| 144 var logEl = document.getElementById('log'); | 137 var logEl = document.getElementById('log'); |
| 145 logEl.textContent += message.data; | 138 logEl.textContent += message.data; |
| 146 } | 139 } |
| 147 | 140 |
| 148 // In the index.html we have set up the appropriate divs: | 141 // In the index.html we have set up the appropriate divs: |
| 149 <body {attrs}> | 142 <body {attrs}> |
| 150 <!-- ... --> | 143 <!-- ... --> |
| 151 <div id="listener"></div> | 144 <div id="listener"></div> |
| 152 <div id="log"></div> | 145 <div id="log"></div> |
| 153 </body> | 146 </body> |
| 154 </pre> | 147 </pre> |
| 155 </section><section id="step-3-from-example-js"> | |
| 156 <h4 id="step-3-from-example-js">Step 3: From example.js</h4> | 148 <h4 id="step-3-from-example-js">Step 3: From example.js</h4> |
| 157 <pre class="prettyprint"> | 149 <pre class="prettyprint"> |
| 158 // From example.js, Step 3: | 150 // From example.js, Step 3: |
| 159 function moduleDidLoad() { | 151 function moduleDidLoad() { |
| 160 // After the NaCl module has loaded, common.naclModule is a reference to the | 152 // After the NaCl module has loaded, common.naclModule is a reference to the |
| 161 // NaCl module's <embed> element. | 153 // NaCl module's <embed> element. |
| 162 // | 154 // |
| 163 // postMessage sends a message to it. | 155 // postMessage sends a message to it. |
| 164 common.naclModule.postMessage('hello'); | 156 common.naclModule.postMessage('hello'); |
| 165 } | 157 } |
| 166 </pre> | 158 </pre> |
| 167 </section></section><section id="native-client-module"> | |
| 168 <h3 id="native-client-module">Native Client module</h3> | 159 <h3 id="native-client-module">Native Client module</h3> |
| 169 <p>The C++ code in the Native Client module of the “Hello, World” ex
ample:</p> | 160 <p>The C++ code in the Native Client module of the “Hello, World” ex
ample:</p> |
| 170 <ol class="arabic simple"> | 161 <ol class="arabic simple"> |
| 171 <li>Implements <code>pp::Instance::HandleMessage()</code> to handle messages sen
t | 162 <li>Implements <code>pp::Instance::HandleMessage()</code> to handle messages sen
t |
| 172 by the JavaScript.</li> | 163 by the JavaScript.</li> |
| 173 <li>Processes incoming messages. This example simply checks that JavaScript | 164 <li>Processes incoming messages. This example simply checks that JavaScript |
| 174 has sent a “hello” message and not some other message.</li> | 165 has sent a “hello” message and not some other message.</li> |
| 175 <li>Calls <code>PostMessage()</code> to send an acknowledgement back to the Java
Script | 166 <li>Calls <code>PostMessage()</code> to send an acknowledgement back to the Java
Script |
| 176 code. The acknowledgement is a string in the form of a <code>Var</code> that th
e | 167 code. The acknowledgement is a string in the form of a <code>Var</code> that th
e |
| 177 JavaScript code can process. In general, a <code>pp::Var</code> can be several | 168 JavaScript code can process. In general, a <code>pp::Var</code> can be several |
| (...skipping 16 matching lines...) Expand all Loading... |
| 194 std::string message = var_message.AsString(); | 185 std::string message = var_message.AsString(); |
| 195 if (message == kHelloString) { | 186 if (message == kHelloString) { |
| 196 // === Step 3: Send the reply. === | 187 // === Step 3: Send the reply. === |
| 197 // If it matches, send our response back to JavaScript. | 188 // If it matches, send our response back to JavaScript. |
| 198 pp::Var var_reply(kReplyString); | 189 pp::Var var_reply(kReplyString); |
| 199 PostMessage(var_reply); | 190 PostMessage(var_reply); |
| 200 } | 191 } |
| 201 } | 192 } |
| 202 }; | 193 }; |
| 203 </pre> | 194 </pre> |
| 204 </section></section><section id="messaging-in-javascript-code-more-details"> | |
| 205 <h2 id="messaging-in-javascript-code-more-details">Messaging in JavaScript code:
More details.</h2> | 195 <h2 id="messaging-in-javascript-code-more-details">Messaging in JavaScript code:
More details.</h2> |
| 206 <p>This section describes in more detail the messaging system code in the | 196 <p>This section describes in more detail the messaging system code in the |
| 207 JavaScript portion of the “Hello, World” example.</p> | 197 JavaScript portion of the “Hello, World” example.</p> |
| 208 <section id="setting-up-an-event-listener-and-handler"> | |
| 209 <h3 id="setting-up-an-event-listener-and-handler">Setting up an event listener a
nd handler</h3> | 198 <h3 id="setting-up-an-event-listener-and-handler">Setting up an event listener a
nd handler</h3> |
| 210 <p>The following JavaScript code sets up an event listener for messages | 199 <p>The following JavaScript code sets up an event listener for messages |
| 211 posted by the Native Client module. It then defines a message handler | 200 posted by the Native Client module. It then defines a message handler |
| 212 that simply logs the content of messages received from the module.</p> | 201 that simply logs the content of messages received from the module.</p> |
| 213 <section id="setting-up-the-message-handler-on-load"> | |
| 214 <h4 id="setting-up-the-message-handler-on-load">Setting up the ‘message
217; handler on load</h4> | 202 <h4 id="setting-up-the-message-handler-on-load">Setting up the ‘message
217; handler on load</h4> |
| 215 <pre class="prettyprint"> | 203 <pre class="prettyprint"> |
| 216 // From common.js | 204 // From common.js |
| 217 | 205 |
| 218 // Listen for the DOM content to be loaded. This event is fired when | 206 // Listen for the DOM content to be loaded. This event is fired when |
| 219 // parsing of the page's document has finished. | 207 // parsing of the page's document has finished. |
| 220 document.addEventListener('DOMContentLoaded', function() { | 208 document.addEventListener('DOMContentLoaded', function() { |
| 221 var body = document.body; | 209 var body = document.body; |
| 222 // ... | 210 // ... |
| 223 var loadFunction = common.domContentLoaded; | 211 var loadFunction = common.domContentLoaded; |
| (...skipping 13 matching lines...) Expand all Loading... |
| 237 } | 225 } |
| 238 } | 226 } |
| 239 | 227 |
| 240 function attachDefaultListeners() { | 228 function attachDefaultListeners() { |
| 241 var listenerDiv = document.getElementById('listener'); | 229 var listenerDiv = document.getElementById('listener'); |
| 242 // ... | 230 // ... |
| 243 listenerDiv.addEventListener('message', handleMessage, true); | 231 listenerDiv.addEventListener('message', handleMessage, true); |
| 244 // ... | 232 // ... |
| 245 } | 233 } |
| 246 </pre> | 234 </pre> |
| 247 </section><section id="implementing-the-handler"> | |
| 248 <h4 id="implementing-the-handler">Implementing the handler</h4> | 235 <h4 id="implementing-the-handler">Implementing the handler</h4> |
| 249 <pre class="prettyprint"> | 236 <pre class="prettyprint"> |
| 250 // From example.js | 237 // From example.js |
| 251 function handleMessage(message) { | 238 function handleMessage(message) { |
| 252 var logEl = document.getElementById('log'); | 239 var logEl = document.getElementById('log'); |
| 253 logEl.textContent += message.data; | 240 logEl.textContent += message.data; |
| 254 } | 241 } |
| 255 </pre> | 242 </pre> |
| 256 <p>Note that the <code>handleMessage()</code> function is handed a message_event | 243 <p>Note that the <code>handleMessage()</code> function is handed a message_event |
| 257 containing <code>data</code> that you can display or manipulate in JavaScript. T
he | 244 containing <code>data</code> that you can display or manipulate in JavaScript. T
he |
| 258 “Hello, World” application simply logs this data to the <code>log</c
ode> div.</p> | 245 “Hello, World” application simply logs this data to the <code>log</c
ode> div.</p> |
| 259 </section></section></section><section id="messaging-in-the-native-client-module
-more-details"> | |
| 260 <h2 id="messaging-in-the-native-client-module-more-details">Messaging in the Nat
ive Client module: More details.</h2> | 246 <h2 id="messaging-in-the-native-client-module-more-details">Messaging in the Nat
ive Client module: More details.</h2> |
| 261 <p>This section describes in more detail the messaging system code in | 247 <p>This section describes in more detail the messaging system code in |
| 262 the Native Client module portion of the “Hello, World” example.</p> | 248 the Native Client module portion of the “Hello, World” example.</p> |
| 263 <section id="implementing-handlemessage"> | |
| 264 <h3 id="implementing-handlemessage">Implementing HandleMessage()</h3> | 249 <h3 id="implementing-handlemessage">Implementing HandleMessage()</h3> |
| 265 <p>If you want the Native Client module to receive and handle messages | 250 <p>If you want the Native Client module to receive and handle messages |
| 266 from JavaScript, you need to implement a <code>HandleMessage()</code> function | 251 from JavaScript, you need to implement a <code>HandleMessage()</code> function |
| 267 for your module’s <code>pp::Instance</code> class. The | 252 for your module’s <code>pp::Instance</code> class. The |
| 268 <code>HelloWorldInstance::HandleMessage()</code> function examines the message | 253 <code>HelloWorldInstance::HandleMessage()</code> function examines the message |
| 269 posted from JavaScript. First it examines that the type of the | 254 posted from JavaScript. First it examines that the type of the |
| 270 <code>pp::Var</code> is indeed a string (not a double, etc.). It then | 255 <code>pp::Var</code> is indeed a string (not a double, etc.). It then |
| 271 interprets the data as a string with <code>var_message.AsString()</code>, and | 256 interprets the data as a string with <code>var_message.AsString()</code>, and |
| 272 checks that the string matches <code>kHelloString</code>. After examining the | 257 checks that the string matches <code>kHelloString</code>. After examining the |
| 273 message received from JavaScript, the code calls <code>PostMessage()</code> to | 258 message received from JavaScript, the code calls <code>PostMessage()</code> to |
| (...skipping 20 matching lines...) Expand all Loading... |
| 294 // Get the string message and compare it to "hello". | 279 // Get the string message and compare it to "hello". |
| 295 std::string message = var_message.AsString(); | 280 std::string message = var_message.AsString(); |
| 296 if (message == kHelloString) { | 281 if (message == kHelloString) { |
| 297 // If it matches, send our response back to JavaScript. | 282 // If it matches, send our response back to JavaScript. |
| 298 pp::Var var_reply(kReplyString); | 283 pp::Var var_reply(kReplyString); |
| 299 PostMessage(var_reply); | 284 PostMessage(var_reply); |
| 300 } | 285 } |
| 301 } | 286 } |
| 302 }; | 287 }; |
| 303 </pre> | 288 </pre> |
| 304 </section><section id="implementing-application-specific-functions"> | |
| 305 <h3 id="implementing-application-specific-functions">Implementing application-sp
ecific functions</h3> | 289 <h3 id="implementing-application-specific-functions">Implementing application-sp
ecific functions</h3> |
| 306 <p>While the “Hello, World” example is very simple, your Native Clie
nt | 290 <p>While the “Hello, World” example is very simple, your Native Clie
nt |
| 307 module will likely include application-specific functions to perform | 291 module will likely include application-specific functions to perform |
| 308 custom tasks in response to messages. For example the application | 292 custom tasks in response to messages. For example the application |
| 309 could be a compression and decompression service (two functions | 293 could be a compression and decompression service (two functions |
| 310 exported). The application could set up an application-specific | 294 exported). The application could set up an application-specific |
| 311 convention that messages coming from JavaScript are colon-separated | 295 convention that messages coming from JavaScript are colon-separated |
| 312 pairs of the form <code><command>:<data></code>. The Native Client
module | 296 pairs of the form <code><command>:<data></code>. The Native Client
module |
| 313 message handler can then split the incoming string along the <code>:</code> | 297 message handler can then split the incoming string along the <code>:</code> |
| 314 character to determine which command to execute. If the command is | 298 character to determine which command to execute. If the command is |
| 315 “compress”, then data to process is an uncompressed string. If the | 299 “compress”, then data to process is an uncompressed string. If the |
| 316 command is “uncompress”, then data to process is an already-compress
ed | 300 command is “uncompress”, then data to process is an already-compress
ed |
| 317 string. After processing the data asynchronously, the application then | 301 string. After processing the data asynchronously, the application then |
| 318 returns the result to JavaScript.</p> | 302 returns the result to JavaScript.</p> |
| 319 </section><section id="sending-messages-back-to-the-javascript-code"> | |
| 320 <h3 id="sending-messages-back-to-the-javascript-code">Sending messages back to t
he JavaScript code</h3> | 303 <h3 id="sending-messages-back-to-the-javascript-code">Sending messages back to t
he JavaScript code</h3> |
| 321 <p>The Native Client module sends messages back to the JavaScript code | 304 <p>The Native Client module sends messages back to the JavaScript code |
| 322 using <code>PostMessage()</code>. The Native Client module always returns | 305 using <code>PostMessage()</code>. The Native Client module always returns |
| 323 its values in the form of a <code>pp::Var</code> that can be processed by the | 306 its values in the form of a <code>pp::Var</code> that can be processed by the |
| 324 browser’s JavaScript. In this example, the message is posted at the | 307 browser’s JavaScript. In this example, the message is posted at the |
| 325 end of the Native Client module’s <code>HandleMessage()</code> function:</
p> | 308 end of the Native Client module’s <code>HandleMessage()</code> function:</
p> |
| 326 <pre class="prettyprint"> | 309 <pre class="prettyprint"> |
| 327 PostMessage(var_reply); | 310 PostMessage(var_reply); |
| 328 </pre> | 311 </pre> |
| 329 </section><section id="sending-and-receiving-other-pp-var-types"> | |
| 330 <h3 id="sending-and-receiving-other-pp-var-types">Sending and receiving other <c
ode>pp::Var</code> types</h3> | 312 <h3 id="sending-and-receiving-other-pp-var-types">Sending and receiving other <c
ode>pp::Var</code> types</h3> |
| 331 <p>Besides strings, <code>pp::Var</code> can represent other types of JavaScript | 313 <p>Besides strings, <code>pp::Var</code> can represent other types of JavaScript |
| 332 objects. For example, messages can be JavaScript objects. These | 314 objects. For example, messages can be JavaScript objects. These |
| 333 richer types can make it easier to implement an application’s | 315 richer types can make it easier to implement an application’s |
| 334 messaging protocol.</p> | 316 messaging protocol.</p> |
| 335 <p>To send a dictionary from the NaCl module to JavaScript simply create | 317 <p>To send a dictionary from the NaCl module to JavaScript simply create |
| 336 a <code>pp::VarDictionary</code> and then call <code>PostMessage</code> with the | 318 a <code>pp::VarDictionary</code> and then call <code>PostMessage</code> with the |
| 337 dictionary.</p> | 319 dictionary.</p> |
| 338 <pre class="prettyprint"> | 320 <pre class="prettyprint"> |
| 339 pp::VarDictionary dictionary; | 321 pp::VarDictionary dictionary; |
| (...skipping 23 matching lines...) Expand all Loading... |
| 363 if (var.is_dictionary()) { | 345 if (var.is_dictionary()) { |
| 364 pp::VarDictionary dictionary(var); | 346 pp::VarDictionary dictionary(var); |
| 365 // Use the dictionary | 347 // Use the dictionary |
| 366 pp::VarArray keys = dictionary.GetKeys(); | 348 pp::VarArray keys = dictionary.GetKeys(); |
| 367 // ... | 349 // ... |
| 368 } else { | 350 } else { |
| 369 // ... | 351 // ... |
| 370 } | 352 } |
| 371 } | 353 } |
| 372 </pre> | 354 </pre> |
| 373 </section></section></section> | 355 </section> |
| 374 | 356 |
| 375 {{/partials.standard_nacl_article}} | 357 {{/partials.standard_nacl_article}} |
| OLD | NEW |