A simple ping module for NodeJS apps.
npm install ping-lite
cd node_modules/ping-lite && mocha
var Ping = require('ping-lite');
var ping = new Ping('8.8.8.8');
ping.send(function(err, ms) {
console.log(ping._host+' responded in '+ms+'ms.');
});
error
= When the._bin
throws an errorresult
= When the._bin
completes, no response returns null
- #send(callback) accepts an optional callback that returns an error if there's an issue with theh
._bin
, anull
if there's no response or anInteger
if the host responds. - #start(callback) calls
#send
every 5 seconds until#stop
is called - #stop() stops active pings
// send one ping & handle results with callbacks
var Ping = require('ping-lite');
var ping = new Ping('8.8.8.8');
ping.send(function(err, ms) {
console.log(this._host+' responded in '+ms+'ms.');
});
// send pings until stopped & handle results with callbacks
var Ping = require('ping-lite');
var ping = new Ping('8.8.8.8');
ping.start(function(err, ms) {
console.log(this._host+' responded in '+ms+'ms.');
});
setTimeout(function() {
ping.stop();
}, 20000);
// send one ping & handle results with events
var Ping = require('ping-lite');
var ping = new Ping('8.8.8.8');
ping.on('error', function(err) {
console.log('uhoh: ',err);
});
ping.on('result', function(err, ms) {
console.log(this._host+' responded in '+ms+'ms.');
});
ping.send(); // or ping.start();