forked from morphling-robot/robot-pc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
udp.js
34 lines (27 loc) · 884 Bytes
/
udp.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
const dgram = require('dgram');
const subnetInfo = require('subnet-info');
const networkInterfaces = require('os').networkInterfaces();
const server = dgram.createSocket("udp4");
const ipList = [];
server.on("error", err => {
server.close();
});
server.on("message", (msg, rinfo) => {
console.log(`server got: ${msg} from ${rinfo.address}:${rinfo.port}`);
ipList.push(rinfo.address);
});
server.on("listening", () => {
server.setBroadcast(true);
});
server.bind(41234);
const robotId = Buffer.from("ORIGAKER-2018001");
for (const key in networkInterfaces) {
networkInterfaces[key].forEach(item => {
if (item.family == 'IPv4') {
if (item.cidr !== '127.0.0.1/8') {
const { broadcastAddress } = new subnetInfo(item.cidr).info();
server.send(robotId, 8080, broadcastAddress);
}
}
});
}