-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
43 lines (39 loc) · 1.31 KB
/
server.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
const dgram = require('dgram');
let selfAdddress = null;
class DiscoveryServer {
constructor(config) {
this.config = config;
}
start() {
const discoverySocket = dgram.createSocket('udp4');
discoverySocket.on('error', (err) => {
console.log(err);
throw err;
});
discoverySocket.on('listening', () => {
selfAdddress = discoverySocket.address();
console.log(`listening ${selfAdddress.address}:${selfAdddress.port}`);
});
discoverySocket.on('message', (msg, rinfo) => {
console.log(`got a msg from :${JSON.stringify(rinfo)}`);
const reply = msg.toString();
console.log(reply);
const response = new Buffer(`ip:${selfAdddress.address},response:${reply}`);
discoverySocket.send(response, rinfo.port, rinfo.address, (err, num) => {
if (err) {
console.error(err);
throw err;
}
console.log('send response');
});
});
discoverySocket.bind({
port: 1234,
address:'0.0.0.0',
}, () => {
discoverySocket.addMembership('239.255.255.250');
});
}
};
const server = new DiscoveryServer('111');
server.start();