Skip to content

Instantly share code, notes, and snippets.

@buesing
Created June 28, 2019 11:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save buesing/689694c94e1e730509e6bb75ce5dd896 to your computer and use it in GitHub Desktop.
Save buesing/689694c94e1e730509e6bb75ce5dd896 to your computer and use it in GitHub Desktop.
sendPings() {
this.pingInterval = setInterval(() => {
this.pings[this.pingNumber] = Date.now();
this.pingRecord.set(`player-${this.isHost ? 1 : 2}-ping-${this.pingNumber}`, {
index: this.pingNumber,
ping: true,
});
this.pingNumber += 1;
if (this.pingNumber >= 20) {
clearInterval(this.pingInterval);
}
}, 1000);
}
receivedPong(data) {
const rtt = Date.now() - this.pings[data.index];
this.roundTripTimes.push(rtt);
this.roundTripTimes.sort((a, b) => a - b);
// get median of all received roundtrips, divide by 2 to get the one-way-latency
this.latency = this.roundTripTimes[Math.floor(this.roundTripTimes.length / 2)] / 2;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment