node检测一个ip是否ping通,可否这样写呢?
var client = net.createConnection(ip, port);
client.on('connect', function () {
success();
});
client.on('error', function(e) {
fail();
});
4 回复
没人问题呢
我一般是这样做的:
调用request库,然后
request.head(‘http://’ + ip, function(error, response, body){ if (error) { //…
} else {
//...
}
});
net-ping模块挺不错的.
var ping = require ("net-ping");
var session = ping.createSession ();
session.pingHost (target, function (error, target) {
if (error)
console.log (target + ": " + error.toString ());
else
console.log (target + ": Alive");
});