-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathdis-udpserver.js
More file actions
71 lines (57 loc) · 1.58 KB
/
Copy pathdis-udpserver.js
File metadata and controls
71 lines (57 loc) · 1.58 KB
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
const dgram = require('dgram');
const argv = require('yargs').argv;
const dis = require("open-dis")
const DISUtils = require('./DISUtils');
var utils = new DISUtils();
const DEFAULT_PORT = 3000
/**
* Read port as commandline arguments - or take the default if not given:
*/
var port = argv.port || DEFAULT_PORT
var server = dgram.createSocket('udp4');
/**
* Messagehandler
*
*/
server.on('message', (msg, rinfo) => {
console.log(`server got msg from ${rinfo.address}:${rinfo.port}`);
try
{
var disMessage = utils.DISObjectFromBuffer(msg);
switch(disMessage.pduType)
{
case 1: // EntityState PDU:
var entityID = disMessage.entityID;
var location = disMessage.entityLocation;
var marking = disMessage.marking.getMarking();
console.log("Got EntityState:", entityID, "Location", location, "Marking: \'" + marking + "\'" )
break;
case 20: // Data PDU:
console.log("Got DataPDU:")
break;
default:
console.log("Got Other PDU:", )
}
}catch(e)
{
console.log("Exception:",disMessage.pduType)
}
});
/**
* Some debug message to show errors
*/
server.on('error', (err) => {
console.log(`server error:\n${err.stack}`);
server.close();
});
/**
* Some debug message to show, when the server started listening
*/
server.on('listening', () => {
const address = server.address();
console.log(`server listening ${address.address}:${address.port}`);
});
/**
* Actually start listening on port:
*/
server.bind(port);