-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
127 lines (92 loc) · 3.03 KB
/
index.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
var Timer = require('wizcorp-timer.js');
var server = exports.server = new Timer();
var client = exports.client = new Timer();
/**
* Returns the difference in msec between server and client
*
* @returns {number} milliseconds difference
*/
exports.getOffset = function () {
return client.offset - server.offset;
};
/**
* Converts a timestamp from this device to one that is synchronized with the server
*
* @param {number} timestamp client-side timestamp
* @param {boolean} msecOut whether the given timestamp is milliseconds or not
* @returns {number} timestamp normalized to the server's clock
*/
exports.translate = function (timestamp, msecOut) {
return server.translate(timestamp, msecOut);
};
/**
* Converts an unbent server-side timestamp to one that is synchronized with this device
*
* @param {number} timestamp server-side timestamp
* @param {boolean} msecOut whether the given timestamp is milliseconds or not
* @returns {number} timestamp normalized to the client's clock
*/
exports.serverTimeToClientTime = function (timestamp, msecOut) {
return client.translate(timestamp, msecOut);
};
/**
* Returns the current time on this device
*
* @param {boolean} msecOut whether to return the time in milliseconds
* @returns {number} time on this device
*/
exports.getClientTime = function (msecOut) {
return client.now(msecOut);
};
/**
* Returns the current time on the server
*
* @param {boolean} msecOut whether to return the time in milliseconds
* @returns {number} time on the server
*/
exports.now = function (msecOut) {
return server.now(msecOut);
};
exports.msec = function () {
return server.msec();
};
exports.sec = function () {
return server.sec();
};
// aliases to match server behavior (deprecated)
exports.getServerTime = exports.now;
exports.clientTimeToServerTime = exports.translate;
/**
* Synchronizes the client clock with the server clock.
*
* @param {Function} cb called on completion
*/
exports.synchronize = function (cb) {
cb = cb || function () {};
if (!exports.hasOwnProperty('sync')) {
return cb(new Error('time.sync usercommand is not exposed.'));
}
exports.sync(Date.now(), function (error, config) {
if (error) {
return cb(error);
}
// positive delta: browser time is ahead of server time
// negative delta: server time is ahead of browser time
// Time according to server
server.configure(
config.timer.offset - config.delta, // server time is browser time + configured offset - delta
config.timer.accelerationFactor,
config.timer.startAt + config.delta // acceleration starts at server startTime + delta
);
// Time according to client (different delta, same time bending rules)
client.configure(
config.timer.offset, // browser time is browser time + configured offset
config.timer.accelerationFactor,
config.timer.startAt + config.delta // acceleration starts at server startTime + delta
);
cb();
});
};
exports.setup = function (cb) {
exports.synchronize(cb);
};