-
Notifications
You must be signed in to change notification settings - Fork 0
/
UdpHandler.js
76 lines (66 loc) · 2.14 KB
/
UdpHandler.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
const util = require('util');
const dgram = require('dgram');
const fs = require('fs');
// file is included here:
//eval(fs.readFileSync('./server.js')+'');
const udpMsgQueue = [];
class UdpHandler{
constructor(ip, listenPort, sendPort, react){
this.listenPort = listenPort ? listenPort : 44444;
this.sendPort = sendPort ? sendPort : 11111;
this.client = dgram.createSocket('udp4');
this.ip = ip ? ip : '127.0.0.1';
this.setupClient(this.client, this.sendPort, react);
console.log("constructor: listen " + listenPort + " send to " + sendPort);
}
setupClient(client, sendPort, react){
client.on('listening', function() {
const address = client.address();
console.log('UDP client listening on ' + address.address + ':' + address.port);
});
client.on('message', function(message, remote) {
// console.log("receive:" + util.inspect(message.toString(), {showHidden: false, depth: null}) + " from " + remote.address);
console.log("receive:" + message);
if(react){
const msg = JSON.stringify({ name: "rokoko", arrival_time: Date.now() });
console.log("send\t" + util.inspect(msg, {showHidden: false, depth: null}) + "to " + remote.address + ":" + sendPort);
const data = Buffer.from(msg);
client.send(data, 0, data.length, sendPort, remote.address, function(err, bytes) {
if (err)
throw err;
});
}
// send to websocket with sync port
/*if(udpMsgQueue){
const newMsg = {
"src": -1,
"dst": "*",
"message": JSON.parse(message)
};
udpMsgQueue.push(newMsg);
console.log("newMsg:" + util.inspect(newMsg, {showHidden: false, depth: 2}) );
}*/
udpMsgQueue.push(message);
});
client.bind({
address: this.ip,
port: this.listenPort,
exclusive: true
});
}
sendMsg(msg, ip){
const data = Buffer.from(msg);
const port = this.sendPort;
this.client.send(data, 0, data.length, this.sendPort, ip, function(err, bytes) {
if (err)
throw err;
console.log('UDP message sent to ' + ip +':'+ port);
});
}
clearQueue(){
while(udpMsgQueue.length > 0)
udpMsgQueue.pop();
}
}
module.exports = UdpHandler;
module.exports.udpMsgQueue = udpMsgQueue;