ping.js 817 B

123456789101112131415161718192021222324252627282930313233343536
  1. 'use strict';
  2. const Command = require('./command');
  3. const CommandCode = require('../constants/commands');
  4. const Packet = require('../packets/packet');
  5. // TODO: time statistics?
  6. // usefull for queue size and network latency monitoring
  7. // store created,sent,reply timestamps
  8. class Ping extends Command {
  9. constructor(callback) {
  10. super();
  11. this.onResult = callback;
  12. }
  13. start(packet, connection) {
  14. const ping = new Packet(
  15. 0,
  16. Buffer.from([1, 0, 0, 0, CommandCode.PING]),
  17. 0,
  18. 5
  19. );
  20. connection.writePacket(ping);
  21. return Ping.prototype.pingResponse;
  22. }
  23. pingResponse() {
  24. // TODO: check it's OK packet. error check already done in caller
  25. if (this.onResult) {
  26. process.nextTick(this.onResult.bind(this));
  27. }
  28. return null;
  29. }
  30. }
  31. module.exports = Ping;