-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
137 lines (128 loc) · 3.45 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
127
128
129
130
131
132
133
134
135
136
137
var phaseSettings = {
'initializing':{
pingDelay: 100,
pingCount: 3,
nextPhase:'gathering'
},
'gathering':{
pingDelay:1000,
pingCount:3,
nextPhase:'working'
},
'working':{
pingDelay:20000,
pingCount: +Infinity
}
}
var acceptablePing = 2300
var maxHistorySize = 23
var TimeSync = function(connection,settings) {
this.connection=connection
this.settings=settings || {
phases:phaseSettings,
acceptablePing: acceptablePing,
maxHistorySize: maxHistorySize
}
this.started=false
this.pingHistory = []
this.phase = 'initializing'
this.timeDiff = 0
this.timeoutFunc = this.timeout.bind(this)
this.handleFunc= this.handleSyncPong.bind(this)
this.pingCounter = 0
this.lastPingTS=0
this.lastPongTS=0
this.connection.on("connected",(function() {
this.pingHistory=[]
this.lastPingTS=0
this.lastPongTS=0
}).bind(this))
}
TimeSync.prototype.start = function() {
this.phase='initializing'
this.timeDiff = 0
this.started=true
this.connection.messageHandlers['timeSync']=this.handleFunc
this.timeoutFunc()
}
TimeSync.prototype.stop = function() {
this.started=false
delete this.connection.messageHandlers['timeSync']
}
TimeSync.prototype.handleSyncPong = function(msg) {
var ts=Date.now()
this.lastPongTS=ts
var ping=ts-msg.client_send_ts
var halfPing=ping/2
var serverTime=msg.server_recv_ts+halfPing
var pongData = {
sendTS: msg.client_send_ts,
recvTS: ts,
serverTS: serverTime,
ping: ping,
timeDiff: serverTime-ts
}
this.pingHistory.push(pongData)
if(this.pingHistory.length>this.settings.maxHistorySize) this.pingHistory.shift()
this.calculateStatistics()
}
TimeSync.prototype.calculateStatistics = function() {
var historySize=this.pingHistory.length
var pingSum=0
var pingMax=0
var pingMin=+Infinity
for(var i=0; i<historySize; i++) {
var ping=this.pingHistory[i].ping
pingSum+=ping
pingMax=Math.max(pingMax,ping)
pingMin=Math.min(pingMin,ping)
}
var pingAvg=pingSum/historySize
var timeDiffSum=0
var timeDiffCount=0
for(var i=0; i<historySize; i++) {
var ph=this.pingHistory[i]
if(ph.ping<=pingAvg) {
timeDiffCount++
timeDiffSum+=ph.timeDiff
}
}
var timeDiffAvg=timeDiffSum/timeDiffCount
if(this.phase=='working') console.info("Time Synchronization Stats: timeDiff = "+timeDiffAvg+" pingAvg = "+pingAvg+
" pingMax = "+pingMax+" pingMin = "+pingMin)
this.timeDiff=timeDiffAvg
}
TimeSync.prototype.sendSyncPing = function() {
if(!this.connection.connected) return;
this.lastPingTS=Date.now()
this.connection.send({
type:"timeSync",
client_send_ts:Date.now()
})
}
TimeSync.prototype.timeout = function() {
if(!this.started) return;
var phs = this.settings.phases[this.phase]
// console.error("TIMESYNC",this.connection.connected())
if(this.connection.connected) {
if(this.lastPingTS-this.lastPongTS>this.settings.acceptablePing) { /// CONNECTION TIMEOUT
this.connection.reconnect()
this.phase='initializing'
}
this.sendSyncPing()
this.pingCounter++
// console.error(this.settings.phases,this.phase)
if (phs.pingCount < this.pingCounter) {
this.phase = phs.nextPhase
phs = this.settings.phases[this.phase]
}
}
setTimeout(this.timeoutFunc,phs.pingDelay)
}
TimeSync.prototype.serverToLocal = function(ts) {
return ts-this.timeDiff
}
TimeSync.prototype.localToServer = function(ts) {
return ts+this.timeDiff
}
module.exports = TimeSync