forked from CiscoDevNet/xapi-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path8-message.js
More file actions
32 lines (22 loc) · 754 Bytes
/
8-message.js
File metadata and controls
32 lines (22 loc) · 754 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
/**
* Illustrates how to pass messages among local macros
*
* see 12-primary and 12-secondary examples for communication through remote codecs
*/
const xapi = require('xapi');
xapi.event.on("Message Send Text", (text) => {
// Decode if necessary (example: message sent over xAPI cloud)
let decoded = text.replace(/\'/g, '"');
// Parse JSON and print
let data = JSON.parse(decoded);
console.log(`Received score: ${data.score}, for player: ${data.player}`);
});
let data = {
score: 5,
player: "Stève"
}
// Serialize as JSON
const serialized = JSON.stringify(data);
// [CONFIRM] Encoding is needed only if sending over HTTP
const encoded = serialized.replace(/"/g, "\'");
xapi.command("Message Send", { Text: encoded });