之前在node中应用了一个ping类库,也就用了,以为是socket封装,之前一波学习ping之后突然想起来看看。一翻,失望了。基于系统ping。我们看下关键代码:
/**
* Return a path to the ping executable in the system
* @param {string} platform - Name of the platform
* @param {bool} v6 - Ping via ipv6 or not
* @return {string} - Executable path for system command ping
* @throw if given platform is not supported
*/
factory.getExecutablePath = function (platform, v6) {
if (!this.isPlatformSupport(platform)) {
throw new Error(util.format('Platform |%s| is not support', platform));
}
var ret = null;
if (platform === 'aix') {
ret = '/usr/sbin/ping';
} else if (factory.isLinux(platform)) {
ret = v6 ? 'ping6' : 'ping';
} else if (factory.isWindow(platform)) {
ret = process.env.SystemRoot + '/system32/ping.exe';
} else if (factory.isMacOS(platform)) {
ret = v6 ? '/sbin/ping6' : '/sbin/ping';
}
return ret;
};
不过这只能怪我的理所当然,没仔细去读readme,毕竟人家有写着呢。
DESCRIPTION
node-ping is a simple wrapper for the system ping utility
没差别。都是是前端http-ping,方法是img.load
// https://github.com/alfg/ping.js/blob/master/src/ping.js 23 line
var self = this;
self.wasSuccess = false;
self.img = new Image();
self.img.onload = onload;
self.img.onerror = onerror;
var timer;
var start = new Date();
function onload(e) {
self.wasSuccess = true;
pingCheck.call(self, e);
}
nospaceships/node-net-ping
这应该是我要的库,基于raw-socket。支持ipv4和ipv6(有空我也去抄一波)。如果要学习写ping的,习惯js的可以学习下这个项目。
场景建议:
今天发现孤陋寡闻了,linux 中的ping 和win 的还是有差别,比如linux 中,-f 和 -i 是可以调整ping 间隔。所以上诉场景建议还需在分系统。
https://linux.die.net/man/8/ping