Ping a server in javascript
maybe not 'ping exactly' but seems to work
by Emmanuel Garcia
HTML
<ul data-bind="foreach:servers">
<li> <a href="#" data-bind="text:name,attr:{href: 'http://'+name}">tester</a> <span data-bind="text:status,css:status"></span>
</li>
</ul>
CSS
.responded {
color: green;
}
.checking,
.unchecked {
color: #FF8C00;
}
.timeout {
color: red;
}
}
JavaScript
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;
this.timer = setTimeout(function() {
if (_that.inUse) {
_that.inUse = false;
_that.callback('timeout');
}
}, 7500);
}
}
var PingModel = function(servers) {
var self = this;
var myServers = [];
ko.utils.arrayForEach(servers, function(location) {
myServers.push({
name: location,
status: ko.observable('unchecked')
});
});
self.servers = ko.observableArray(myServers);
ko.utils.arrayForEach(self.servers(), function(s) {
s.status('checking');
new ping(s.name, function(status, e) {
s.status(status);
});
});
};
var komodel = new PingModel([
'raintreevacationclub.com',
'travelraintree.com',
'news.rvcmembers.com',
'raintreereview.com',
'ravacnet.com',
'https://www.xwolski.com',
'https://www.semars.com.mx',
'castanedautodetail.com',
'galeriaomaralonso.tk',
'galeriaomaralonso.com',
'multideas.com.mx',
'multideas.mx',
'multideas.ga',
'galeriedesartistes.com',
'travelraintree.tk',
'jardinazul.com.mx'
]);
ko.applyBindings(komodel);