@dashevo/dashcore-p2p
Version:
Interface to the dash P2P network for Dashcore
42 lines (35 loc) • 1.1 kB
JavaScript
;
var Message = require('../message');
var inherits = require('util').inherits;
var dashcore = require('@dashevo/dashcore-lib');
var utils = require('../utils');
var $ = dashcore.util.preconditions;
var _ = dashcore.deps._;
var BufferUtil = dashcore.util.buffer;
var BufferReader = dashcore.encoding.BufferReader;
/**
* A message to confirm that a connection is still valid.
* @param {Number} arg - A nonce for the Ping message
* @param {Object=} options
* @extends Message
* @constructor
*/
function PingMessage(arg, options) {
Message.call(this, options);
this.command = 'ping';
$.checkArgument(
_.isUndefined(arg) || (BufferUtil.isBuffer(arg) && arg.length === 8),
'First argument is expected to be an 8 byte buffer'
);
this.nonce = arg || utils.getNonce();
}
inherits(PingMessage, Message);
PingMessage.prototype.setPayload = function(payload) {
var parser = new BufferReader(payload);
this.nonce = parser.read(8);
utils.checkFinished(parser);
};
PingMessage.prototype.getPayload = function() {
return this.nonce;
};
module.exports = PingMessage;