OLD | NEW |
---|---|
1 .. _messaging_system: | 1 .. _messaging_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 dictionaries. | |
binji
2013/09/24 21:56:26
JavaScript doesn't really have dictionaries, just
jvoung (off chromium)
2013/09/24 22:35:53
Done.
| |
368 These 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 | |
372 create a ``pp::VarDictionary`` and then call ``PostMessage`` with | |
373 the 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); | |
binji
2013/09/24 21:56:26
Is it worth mentioning what this will look like in
jvoung (off chromium)
2013/09/24 22:35:53
Seems worth doing -- Done.
| |
385 | |
386 | |
387 To receive a dictionary-typed message in the NaCl module, test that | |
388 the message is truly a dictionary type, then convert the message | |
389 with the ``pp::VarDictionary`` class. | |
390 | |
391 .. naclcode:: | |
392 | |
393 virtual void HandleMessage(const pp::Var& var) { | |
394 if (var.is_dictionary()) { | |
395 pp::VarDictionary dictionary(var); | |
396 // Use the dictionary | |
397 pp::VarArray keys = dictionary.GetKeys(); | |
398 // ... | |
399 } else { | |
400 // ... | |
401 } | |
402 } | |
OLD | NEW |