This repository was archived by the owner on Jan 19, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathclient.js
More file actions
92 lines (75 loc) · 2.09 KB
/
Copy pathclient.js
File metadata and controls
92 lines (75 loc) · 2.09 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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import BackgroundTimer from "react-native-background-timer";
import { Buffer } from "buffer";
import dgram from "react-native-udp";
setTimeout = (fn, ms = 0) => {
const numberMs = Number(ms);
if (isNaN(numberMs)) return BackgroundTimer.setTimeout(fn, 0);
return BackgroundTimer.setTimeout(fn, numberMs);
};
clearTimeout = (fn, ms = 0) => {
const numberMs = Number(ms);
if (isNaN(numberMs)) return BackgroundTimer.clearTimeout(fn, 0);
return BackgroundTimer.clearTimeout(fn, numberMs);
};
/**
* Gets the current time from the parsed NTP Server.
* @param {String} server IP/Hostname of the NTP server
* @param {Number} port Port of the NTP server
*/
export const getNetworkTime = (server, port, callback) => {
let client = dgram.createSocket({
type: "udp4",
debug: false,
});
//client.bind(0);
let data = Buffer.alloc(48);
data[0] = 0x1b;
for (let i = 1; i < 48; i++) {
data[i] = 0;
}
let timeout = setTimeout(() => {
client.close();
callback("timed out waiting for response", null);
}, 10000);
let error = false;
client.on("error", err => {
if (error) {
return;
}
callback(err, null);
error = true;
clearTimeout(timeout);
});
client.once("message", msg => {
clearTimeout(timeout);
client.close();
let offsetTransmitTime = 40,
intpart = 0,
fractpart = 0;
for (var i = 0; i <= 3; i++) {
intpart = 256 * intpart + msg[offsetTransmitTime + i];
}
for (i = 4; i <= 7; i++) {
fractpart = 256 * fractpart + msg[offsetTransmitTime + i];
}
let milliseconds = intpart * 1000 + (fractpart * 1000) / 0x100000000;
let date = new Date("Jan 01 1900 GMT");
date.setUTCMilliseconds(date.getUTCMilliseconds() + milliseconds);
callback(null, date);
});
client.once("listening", () => {
client.send(data, 0, data.length, port, server, err => {
if (err) {
if (error) {
return;
}
clearTimeout(timeout);
callback(err, null);
error = true;
client.close();
return;
}
});
});
client.bind(0);
};