I've had a couple of problems in the past when implementing that script and couldn't figure out what was causing it.
Instead I've found a similar approach, which seems to work well, you can try it out.
function initWebSocket()
{
// init blockchain websocket (activity, blocks)
var blockchain = new WebSocket('ws://ws.blockchain.info/inv');
blockchain.onerror = function (error){ console.log('connection.onerror',error); };
blockchain.onopen = function ()
{
blockchain.send( JSON.stringify( {"op":"unconfirmed_sub"} ) ); // subscribe to uncofirmed activity
blockchain.send( JSON.stringify( {"op":"blocks_sub"} ) ); // subscribe to new blocks
};
blockchain.onmessage = function (message)
{
var response = JSON.parse(message.data);
var date = new Date(0);
date.setUTCSeconds( response.x.time );
if( response.op == "utx")
{
var amount = 0;
for(var i=0;i<response.x.out.length;i++)
amount += response.x.out[i].value;
// amount is in satoshi
// 1 BTC = 100,000,000 Satoshi (https://en.bitcoin.it/wiki/activity)
response.amount = amount / 100000000;
response.type = TYPE_TRANSACTION;
response.index = index++;
}
else if( response.op == "block" )
{
response.type = TYPE_BLOCK;
response.amount = Math.round( response.x.height / 10000 );
}
if( DEBUG )
console.log( response.op, response );
response.date = date;
};
You can find the source here:
https://gist.github.com/npedrini/6030317Thank you for your response! I'm really a newbie, so I tried copying & pasting the code on your github into an html file, but it stuck loading. I also tried putting your code into mine, but that didn't work either. How should I implement your code?
Thanks again for your help, I really appreciate it.