-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdemo.js
62 lines (49 loc) · 1.4 KB
/
demo.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
var Channel = require('./lib/channel');
var readline = require('readline');
var rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
rl.question('What cluster would you like to join? ', function(name) {
var channel = Channel.create(name);
channel.connect().then(connectHandler, errorHandler);
});
rl.on('SIGINT', function() {
rl.close();
console.log('\nGoodbye');
process.exit();
});
function connectHandler(channel) {
var cluster = channel.cluster;
console.log("Connected to " + cluster);
channel.on('message', function(message) {
console.log('\nreceived message: ' + message);
prompt();
});
channel.on('viewChanged', function(members) {
console.log('\ncluster members are: ' + members);
prompt();
});
channel.on('memberAdded', function(member, members) {
if (member !== channel.address) {
console.log('\nnew cluster member is ' + member);
channel.send(member, channel.address + ' greets you');
prompt();
}
});
channel.on('memberRemoved', function(member, members) {
console.log('\nremoved cluster member ' + member);
prompt();
});
function prompt() {
var p = 'Enter a message for all nodes on ' + cluster + ': ';
rl.question(p, function(answer) {
if (answer) channel.publish(answer);
prompt();
});
}
prompt();
}
function errorHandler(reason) {
console.error(reason);
}