ping.js 824 B

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