Skip to content

Commit 8a1d2cc

Browse files
committed
Fix ping implement
1 parent 6c2204a commit 8a1d2cc

File tree

1 file changed

+10
-6
lines changed

1 file changed

+10
-6
lines changed

src/ping.js

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,19 +40,23 @@ function ping (options) {
4040
client.once('server_info', function (packet) {
4141
const data = JSON.parse(packet.response)
4242
const start = Date.now()
43-
const maxTime = setTimeout(() => {
43+
const maxTimer = setTimeout(() => {
4444
clearTimeout(closeTimer)
4545
client.end()
4646
resolve(data)
4747
}, options.noPongTimeout)
48+
const time = BigInt(Date.now())
4849
client.once('ping', function (packet) {
4950
data.latency = Date.now() - start
50-
clearTimeout(maxTime)
51-
clearTimeout(closeTimer)
52-
client.end()
53-
resolve(data)
51+
if (BigInt(packet.time) === time) {
52+
// pong payload should be the same as ping payload
53+
clearTimeout(maxTimer)
54+
clearTimeout(closeTimer)
55+
client.end()
56+
resolve(data)
57+
}
5458
})
55-
client.write('ping', { time: [0, 0] })
59+
client.write('ping', { time })
5660
})
5761
client.on('state', function (newState) {
5862
if (newState === states.STATUS) { client.write('ping_start', {}) }

0 commit comments

Comments
 (0)