Created
May 18, 2015 08:57
-
-
Save gihankarunarathne/510da50d854f2840a29e to your computer and use it in GitHub Desktop.
Read a live JSON stream from Docker API
This file contains 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
function() { | |
var self = this; | |
var url = apiUrl + "/containers/" + containerId + "/stats"; | |
// var url = "/docker/containers/" + containerId + "/stats"; | |
console.log('URL ', url); | |
oboe(url) | |
.node('*', function(data) { | |
console.log('Data ', data); | |
}) | |
.done(function(things) { | |
console.log('Done ', things); | |
// we got it | |
}) | |
.fail(function() { | |
console.log('Fail'); | |
// we don't got it | |
}); | |
var xhr = new XMLHttpRequest(); | |
xhr.open("GET", url, true); | |
xhr.onprogress = function() { | |
try { | |
var data = JSON.parse(xhr.responseText.toString()); | |
console.log("PROGRESS:", data); | |
} catch (e) { | |
console.error('JSON parse error ', e.stack); | |
} | |
} | |
xhr.send(); | |
var jsonStream = new EventSource(url); | |
jsonStream.onmessage = function(e) { | |
var message = JSON.parse(e.data); | |
// handle message | |
console.log('Message ', message); | |
}; | |
$.ajax({ | |
type: 'GET', | |
url: url, | |
crossDomain: 'true' | |
}).done(function(data) { | |
console.log('Success'); | |
console.log(data); | |
}).fail(function(err) { | |
console.log('Fail'); | |
console.error(err); | |
}); | |
try { | |
var host = "ws://" + apiUrl + "/containers/" + containerId + "/stats"; | |
console.log('URL ', host); | |
self.socket = new WebSocket(host); | |
self.showState('<p class="event">Socket Status: ' + self.socket.readyState); | |
self.socket.onopen = function() { | |
self.showState('<p class="event">Socket Status: ' + self.socket.readyState + ' (open)'); | |
} | |
self.socket.onmessage = function(msg) { | |
self.showMessages('<p class="message">' + msg.data); | |
} | |
self.socket.onclose = function() { | |
self.showState('<p class="event">Socket Status: ' + self.socket.readyState + ' (Closed)'); | |
} | |
} catch (exception) { | |
self.showState('<p>Error' + exception); | |
} | |
}; //-- END function |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment