-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
26 lines (24 loc) · 928 Bytes
/
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
const path = require('path');
const dgram = require('dgram');
const fsExtra = require('fs-extra');
const udpSocket = dgram.createSocket('udp4');
const root = './logs/udp/';
udpSocket.bind(7777);
module.exports = () => {
const fp = root + path.sep + path.sep;
fsExtra.ensureDir(fp);
udpSocket.on('message', function(msg, rinfo) {
const filepath = fp + path.sep + getDate() + '.log';
fsExtra.appendFile(filepath, 'from IP:' + rinfo.address + '\r\n' + msg + '\r\n', err => {
if (err) {
fsExtra.appendFile(root + 'server-error.log', filepath + '\t' + msg, e => { console.log(e); });
}
});
});
udpSocket.on('error', function(err) {
fsExtra.appendFile(root + 'server-error.log', err.message + err.stack, e => { console.log(e); });
});
udpSocket.on('listening', function() {
console.log('udp log server is listening on port 7777.');
});
};