I have an intranet app that periodically checks status of a set of printers by sending it a "Host Status" command. If status returned is "No Response" I display the printer with a red background that user can click and get some additional info. This additional info is the part I need help with.
When clicked, I send a ping command using printer's IP. If I get a "timeout" I have the answer: printer is offline. But I might get a response which means printer is online; "No Response" could be a port issue. However, it seems I can't ping IP:Port since ping uses ICMP and has no concept of port.
How can I check for this in JS/jQuery?
This is what I am using to test for ping response (and IP:Port which I think is useless):
function ping(ip, callback) {
if (!this.inUse) {
this.status = 'unchecked';
this.inUse = true;
this.callback = callback;
this.ip = ip;
var _that = this;
this.img = new Image();
this.img.onload = function () {
_that.inUse = false;
_that.callback('responded');
};
this.img.onerror = function (e) {
if (_that.inUse) {
_that.inUse = false;
_that.callback('responded', e);
}
};
this.start = new Date().getTime();
this.img.src = "http://" + ip + "/?cachebreaker=" + new Date().getTime();
this.timer = setTimeout(function () {
if (_that.inUse) {
_that.inUse = false;
_that.callback('timeout');
}
}, 1500);
}
}
Somewhere in script checking status:
$(document).on('click', '.extLink', function () {
var ip = $(this).data("ip");
...
new ping(ip, function (status, e) {
if (status == 'responded') {
$('<p>Printer is reachable. Ping was successful.<br />Checking port 9100.</p>').appendTo('#divInfo');
new ping(ip + ":9100", function (status, e) { // not sure about this
if (status == 'timeout') {
$('<p>Port 9100 is blocked.</p>').appendTo('#divInfo');
}
});
}
else if (status == 'timeout') {
$('<p>Printer is not reachable. Ping failed.</p>').appendTo('#divInfo');
}
});
...
});