- 安装依赖包
npm install messenger
使用
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
31var messenger = require('messenger');
// here we have 4 servers listening on 4 different ports
var server1 = messenger.createListener(8001);
var server2 = messenger.createListener(8002);
var server3 = messenger.createListener(8003);
var server4 = messenger.createListener('127.0.0.1:8004');
server1.on('a message came', function(m, data){
// note that m.data and data are equivalent
console.log('server 1 got data', data);
});
server2.on('a message came', function(m, data){
console.log('server 2 got data', data);
});
server3.on('a message came', function(m, data){
console.log('server 3 got data', data);
});
server4.on('a message came', function(m, data){
console.log('server 4 got data', data);
});
// a client that can be used to emit to all the servers
var client = messenger.createSpeaker(8001, 8002, 8003, 8004);
setInterval(function(){
client.shout('a message came', {some: data});
}, 1000);参考
github