-
-
Notifications
You must be signed in to change notification settings - Fork 253
Fix ping implement #1411
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Fix ping implement #1411
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -40,22 +40,28 @@ function ping (options) { | |||||||||||||||||||||||||||
client.once('server_info', function (packet) { | ||||||||||||||||||||||||||||
const data = JSON.parse(packet.response) | ||||||||||||||||||||||||||||
const start = Date.now() | ||||||||||||||||||||||||||||
const maxTime = setTimeout(() => { | ||||||||||||||||||||||||||||
const maxTimer = setTimeout(() => { | ||||||||||||||||||||||||||||
clearTimeout(closeTimer) | ||||||||||||||||||||||||||||
client.end() | ||||||||||||||||||||||||||||
resolve(data) | ||||||||||||||||||||||||||||
}, options.noPongTimeout) | ||||||||||||||||||||||||||||
const time = BigInt(Date.now()) | ||||||||||||||||||||||||||||
client.once('ping', function (packet) { | ||||||||||||||||||||||||||||
data.latency = Date.now() - start | ||||||||||||||||||||||||||||
clearTimeout(maxTime) | ||||||||||||||||||||||||||||
clearTimeout(closeTimer) | ||||||||||||||||||||||||||||
client.end() | ||||||||||||||||||||||||||||
resolve(data) | ||||||||||||||||||||||||||||
if (BigInt(packet.time) === time) { | ||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This comparison assumes packet.time can be converted to BigInt and will equal the BigInt time variable. If packet.time is undefined, null, or not a valid number, BigInt(packet.time) will throw an error. Consider adding proper validation before the BigInt conversion.
Suggested change
Copilot uses AI. Check for mistakes. Positive FeedbackNegative Feedback |
||||||||||||||||||||||||||||
// pong payload should be the same as ping payload | ||||||||||||||||||||||||||||
clearTimeout(maxTimer) | ||||||||||||||||||||||||||||
clearTimeout(closeTimer) | ||||||||||||||||||||||||||||
client.end() | ||||||||||||||||||||||||||||
resolve(data) | ||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||
}) | ||||||||||||||||||||||||||||
client.write('ping', { time: [0, 0] }) | ||||||||||||||||||||||||||||
client.write('ping', { time }) | ||||||||||||||||||||||||||||
}) | ||||||||||||||||||||||||||||
client.on('state', function (newState) { | ||||||||||||||||||||||||||||
if (newState === states.STATUS) { client.write('ping_start', {}) } | ||||||||||||||||||||||||||||
if (newState === states.STATUS) { | ||||||||||||||||||||||||||||
client.write('ping_start', {}) | ||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||
}) | ||||||||||||||||||||||||||||
// TODO: refactor with src/client/setProtocol.js | ||||||||||||||||||||||||||||
client.on('connect', function () { | ||||||||||||||||||||||||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using BigInt for Date.now() may cause issues since Date.now() returns a regular number. The comparison on line 51 expects BigInt(packet.time) to equal time, but if packet.time is a regular number, this comparison will always fail. Consider using a regular number or ensure packet.time is also a BigInt.
Copilot uses AI. Check for mistakes.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
in protodef, i64 are read as an array but with a valueOf() that allows the array to become a BigInt when used in operations. So casting with the BigInt constructor should indeed implicitly call .valueOf() and make this test pass