OLD | NEW |
1 .. _message-system: | 1 .. _message-system: |
2 | 2 |
3 ################ | 3 ################ |
4 Messaging System | 4 Messaging System |
5 ################ | 5 ################ |
6 | 6 |
7 .. contents:: | 7 .. contents:: |
8 :local: | 8 :local: |
9 :backlinks: none | 9 :backlinks: none |
10 :depth: 2 | 10 :depth: 2 |
(...skipping 340 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
351 | 351 |
352 The Native Client module sends messages back to the JavaScript code | 352 The Native Client module sends messages back to the JavaScript code |
353 using ``PostMessage()``. The Native Client module always returns | 353 using ``PostMessage()``. The Native Client module always returns |
354 its values in the form of a ``pp::Var`` that can be processed by the | 354 its values in the form of a ``pp::Var`` that can be processed by the |
355 browser's JavaScript. In this example, the message is posted at the | 355 browser's JavaScript. In this example, the message is posted at the |
356 end of the Native Client module's ``HandleMessage()`` function: | 356 end of the Native Client module's ``HandleMessage()`` function: |
357 | 357 |
358 .. naclcode:: | 358 .. naclcode:: |
359 | 359 |
360 PostMessage(var_reply); | 360 PostMessage(var_reply); |
| 361 |
| 362 |
| 363 Sending and receiving other ``pp::Var`` types |
| 364 --------------------------------------------- |
| 365 |
| 366 Besides strings, ``pp::Var`` can represent other types of JavaScript |
| 367 objects. For example, messages can be JavaScript objects. These |
| 368 richer types can make it easier to implement an application's |
| 369 messaging protocol. |
| 370 |
| 371 To send a dictionary from the NaCl module to JavaScript simply create |
| 372 a ``pp::VarDictionary`` and then call ``PostMessage`` with the |
| 373 dictionary. |
| 374 |
| 375 .. naclcode:: |
| 376 |
| 377 pp::VarDictionary dictionary; |
| 378 dictionary.Set(pp::Var("command"), pp::Var(next_command)); |
| 379 dictionary.Set(pp::Var("param_int"), pp::Var(123)); |
| 380 pp::VarArray an_array; |
| 381 an_array.Set(0, pp::Var("string0")); |
| 382 an_array.Set(1, pp::Var("string1")) |
| 383 dictionary.Set(pp::Var("param_array"), an_array); |
| 384 PostMessage(dictionary); |
| 385 |
| 386 |
| 387 Here is how to create a similar object in JavaScript and send it to |
| 388 the NaCl module: |
| 389 |
| 390 .. naclcode:: |
| 391 |
| 392 var dictionary = { |
| 393 command: next_command, |
| 394 param_int: 123, |
| 395 param_array: ['string0', 'string1'] |
| 396 } |
| 397 nacl_module.postMessage(dictionary); |
| 398 |
| 399 |
| 400 To receive a dictionary-typed message in the NaCl module, test that |
| 401 the message is truly a dictionary type, then convert the message |
| 402 with the ``pp::VarDictionary`` class. |
| 403 |
| 404 .. naclcode:: |
| 405 |
| 406 virtual void HandleMessage(const pp::Var& var) { |
| 407 if (var.is_dictionary()) { |
| 408 pp::VarDictionary dictionary(var); |
| 409 // Use the dictionary |
| 410 pp::VarArray keys = dictionary.GetKeys(); |
| 411 // ... |
| 412 } else { |
| 413 // ... |
| 414 } |
| 415 } |
OLD | NEW |