-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathexample.js
44 lines (35 loc) · 952 Bytes
/
example.js
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
33
34
35
36
37
38
39
40
41
42
43
44
const {Mycelium} = require('./index');
const readline = require("readline");
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
const MYCELIUM_IP = process.env.MYCELIUM_IP;
const MYCELIUM_PORT = process.env.MYCELIUM_PORT;
const JWT = process.env.JWT;
const mycelium = new Mycelium();
let isLoggedIn = false;
const prompt = () => {
rl.question('press enter to broadcast a notification.', (txt) => {
mycelium.broadcast('broadcast', txt);
prompt();
});
};
mycelium.on('message', (msg) => {
console.log(`received ${msg.event} event`);
console.log(JSON.stringify(msg, null, 2));
if (msg.event === 'LoginResponse') {
isLoggedIn = true;
}
if (isLoggedIn) {
prompt();
}
});
mycelium.on('error', (err) => {
console.log(err);
});
mycelium.on('connect', () => {
console.log('connected to mycelium!');
mycelium.login(JWT);
});
mycelium.connect(MYCELIUM_IP, MYCELIUM_PORT);