Created
June 28, 2019 11:08
-
-
Save buesing/689694c94e1e730509e6bb75ce5dd896 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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