Last active
August 29, 2015 14:04
-
-
Save Dr4K4n/ba5df1a0f094c99988ce to your computer and use it in GitHub Desktop.
JavaScript Backend Ping Online Check Worker (AngularJS)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var pingWorker = new Worker('ping_worker.js'); | |
pingWorker.addEventListener('message', function(event) { | |
if($scope.online != event.data) { | |
$scope.online = event.data; | |
$scope.$apply(); | |
} | |
console.log('Are we online?', event.data); | |
}, false); | |
// worker starten | |
pingWorker.postMessage(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
self.checkOnline = function() { | |
var xhr = new XMLHttpRequest(); | |
xhr.open('GET', 'http://localhost:8080/portal/resources/monitor/version', true); | |
xhr.onreadystatechange = function (oEvent) { | |
if (xhr.readyState === 4) { | |
if (xhr.status === 200) { | |
self.postMessage(true); | |
} else { | |
self.postMessage(false); | |
} | |
} | |
}; | |
xhr.send(null); | |
} | |
self.addEventListener('message', function(event) { | |
checkOnline(); | |
setInterval("checkOnline()", 10000); | |
}, false); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment