forked from Rob2048/VectorONE
-
Notifications
You must be signed in to change notification settings - Fork 1
/
timing.js
60 lines (42 loc) · 1008 Bytes
/
timing.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
const dgram = require('dgram');
const NS_PER_SEC = 1e9;
let socket = dgram.createSocket('udp4');
socket.bind(45455);
let prevTime = 0;
let startTime = getUS();
let prevIndex = 0;
function getUS()
{
let pt = process.hrtime();
return Math.floor((pt[0] * NS_PER_SEC + pt[1]) / 1000000);
}
socket.on('listening', () => {
});
let timeMap = {};
socket.on('message', (message, remote) => {
let t = getUS();
//let d = t - prevTime;
//prevTime = t;
let d = t - startTime;
/*
if (remote.address == '192.168.1.106') {
prevIndex = parseInt(message);
} else {
let indexD = parseInt(message) - prevIndex;
console.log(indexD);
}
*/
timeMap[remote.address] = parseInt(message);
//console.log(d + ' ' + message + ' ' + remote.address);
});
setInterval(() => {
console.log('-------------------------------------------');
let zeroTime = -1;
for (var key in timeMap) {
let t = timeMap[key];
if (zeroTime == -1) {
zeroTime = t;
}
console.log(key, t - zeroTime);
}
}, 500);