Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
JSON Parsing
/* Sets up a socket using javaScript that then listens
for transations and writes them to the screen. */
var wSocket = "ws://ws.blockchain.info/inv";
function getBlockchainJSON() {
webSocket = new WebSocket(wSocket);
webSocket.onopen = function() { onOpen() };
webSocket.onmessage = function(evt) { onMsg(evt) };
};
function onOpen() {
webSocket.send('{"op":"unconfirmed_sub"}');
;}
function onMsg(evt) {
var data = parseJSON(evt.data);
writeBlockchainToScreen(data);
};
function parseJSON(data) {
var bcObject = JSON.parse(data);
var size = bcObject.size;
return size;
};
function writeBlockchainToScreen(data) {
var transaction = document.createElement("div");
var text = document.createTextNode(data);
transaction.id = "transaction";
transaction.appendChild(text);
var getId = document.getElementById("realTimeTransactions");
getId.appendChild(transaction);
};
window.addEventListener("load", getBlockchainJSON , false);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.