From 70fd095f6084802fd48f4cc0c8f605f85a168a46 Mon Sep 17 00:00:00 2001 From: Adam Soltys Date: Wed, 24 Oct 2012 15:35:47 -0700 Subject: Use reconnecting websocket --- .gitignore | 2 +- index.html | 1 + js/bitcoin_pos.js | 102 +++++++++++++++++++-------------------- js/qr.js | 4 +- js/reconnecting-websocket.min.js | 2 + transactions.json | 1 - 6 files changed, 55 insertions(+), 57 deletions(-) create mode 100644 js/reconnecting-websocket.min.js delete mode 100644 transactions.json diff --git a/.gitignore b/.gitignore index a6f7ae91..0f104277 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,5 @@ ticker.json -transactions.csv +transactions.json .htpasswd .rvmrc db/migrate.rb diff --git a/index.html b/index.html index 4c9d17cc..7d260270 100644 --- a/index.html +++ b/index.html @@ -10,6 +10,7 @@ + diff --git a/js/bitcoin_pos.js b/js/bitcoin_pos.js index 6bd0b0a7..f05407d3 100644 --- a/js/bitcoin_pos.js +++ b/js/bitcoin_pos.js @@ -1,54 +1,14 @@ $(function() { - var websocket = new WebSocket("ws://api.blockchain.info:8335/inv"); - var exchange = 0; - var total = 0; - var received = 0; var address = getParameterByName('address'); - + var exchange = 0; + if (address == "") window.location = 'index.html?address=1KDaKixtYqPdDNsu3fMeAHHB52R2WHvXCQ' - setupqr(); $('#received').hide() $('div.container-fluid').css('width', Math.min(screen.width, 600) + 'px'); $('#address').html(address); - - websocket.onopen = function() { - websocket.send('{"op":"addr_sub", "addr":"' + address + '"}'); - } - - websocket.onmessage = function(e) { - var results = eval('(' + e.data + ')'); - var from_address = ''; - - $.each(results.x.out, function(i, v) { - if (v.addr == address) { - received += v.value / 100000000; - } - }); - - $.each(results.x.inputs, function(i, v) { - from_address = v.prev_out.addr - if (v.prev_out.addr == address) { - input -= v.prev_out.value / 100000000; - } - }); - - if (total <= received) { - $('#amount').blur(); - $('#payment').hide(); - $('#received').fadeIn('slow'); - } - - $.get('record_transaction.php', - { - address: from_address, - date: moment().format("YYYY-MM-DD HH:mm:ss"), - received: received, - exchange: exchange - } - ); - } + setupQR(); $.getJSON('ticker.json', function (data) { exchange = 1000 / data.out; @@ -56,6 +16,7 @@ $(function() { exchange = Math.ceil(exchange * 100) / 100; $('#exchange').val(exchange.toFixed(2)); updateTotal(); + setupSocket(); }); $('#amount').keyup(updateTotal); @@ -69,13 +30,56 @@ $(function() { function updateTotal() { var amount = parseFloat($('#amount').val()); - total = amount / exchange; + var total = amount / exchange; total = Math.ceil(total * 10000) / 10000; if (!$.isNumeric(total)) total = ''; $('#total').html(total.toString()); - doqr('bitcoin:' + address + '?amount=' + total.toString()); + + displayQR('bitcoin:' + address + '?amount=' + total.toString()); } + function setupSocket() { + var websocket = new ReconnectingWebSocket("ws://api.blockchain.info:8335/inv"); + + websocket.onopen = function() { + websocket.send('{"op":"addr_sub", "addr":"' + address + '"}'); + } + + websocket.onmessage = function(e) { + var results = eval('(' + e.data + ')'); + var from_address = ''; + var total = 0; + var received = 0; + + $.each(results.x.out, function(i, v) { + if (v.addr == address) { + received += v.value / 100000000; + } + }); + + $.each(results.x.inputs, function(i, v) { + from_address = v.prev_out.addr + if (v.prev_out.addr == address) { + input -= v.prev_out.value / 100000000; + } + }); + + if (total <= received) { + $('#amount').blur(); + $('#payment').hide(); + $('#received').fadeIn('slow'); + } + + $.get('record_transaction.php', + { + address: from_address, + date: moment().format("YYYY-MM-DD HH:mm:ss"), + received: received, + exchange: exchange + } + ); + } + } }); function getParameterByName(name) { @@ -88,11 +92,3 @@ function getParameterByName(name) { else return decodeURIComponent(results[1].replace(/\+/g, " ")); } - -function getFormattedDate() { - var date = new Date(); - var str = date.getFullYear() + "-" + date.getMonth() + "-" + date.getDate() + " " + date.getHours() + ":" + date.getMinutes() + ":" + date.getSeconds(); - - return str; -} - diff --git a/js/qr.js b/js/qr.js index 6933e8ae..a7ee92d7 100644 --- a/js/qr.js +++ b/js/qr.js @@ -702,7 +702,7 @@ function genframe(instring) } var wd, ht, qrc; -function setupqr(){ +function setupQR(){ wd = 300; ht = 300; mp = document.getElementById("mapcanv"); @@ -715,7 +715,7 @@ function setupqr(){ qrc.fillRect(0,0,wd,ht); } -function doqr(value) { +function displayQR(value) { d = document; ecclevel = 1; qf = genframe(value); diff --git a/js/reconnecting-websocket.min.js b/js/reconnecting-websocket.min.js new file mode 100644 index 00000000..cf29747e --- /dev/null +++ b/js/reconnecting-websocket.min.js @@ -0,0 +1,2 @@ +// https://github.com/joewalnes/reconnecting-websocket/ +function ReconnectingWebSocket(a){function f(g){c=new WebSocket(a);if(b.debug||ReconnectingWebSocket.debugAll){console.debug("ReconnectingWebSocket","attempt-connect",a)}var h=c;var i=setTimeout(function(){if(b.debug||ReconnectingWebSocket.debugAll){console.debug("ReconnectingWebSocket","connection-timeout",a)}e=true;h.close();e=false},b.timeoutInterval);c.onopen=function(c){clearTimeout(i);if(b.debug||ReconnectingWebSocket.debugAll){console.debug("ReconnectingWebSocket","onopen",a)}b.readyState=WebSocket.OPEN;g=false;b.onopen(c)};c.onclose=function(h){clearTimeout(i);c=null;if(d){b.readyState=WebSocket.CLOSED;b.onclose(h)}else{b.readyState=WebSocket.CONNECTING;if(!g&&!e){if(b.debug||ReconnectingWebSocket.debugAll){console.debug("ReconnectingWebSocket","onclose",a)}b.onclose(h)}setTimeout(function(){f(true)},b.reconnectInterval)}};c.onmessage=function(c){if(b.debug||ReconnectingWebSocket.debugAll){console.debug("ReconnectingWebSocket","onmessage",a,c.data)}b.onmessage(c)};c.onerror=function(c){if(b.debug||ReconnectingWebSocket.debugAll){console.debug("ReconnectingWebSocket","onerror",a,c)}b.onerror(c)}}this.debug=false;this.reconnectInterval=1e3;this.timeoutInterval=2e3;var b=this;var c;var d=false;var e=false;this.url=a;this.readyState=WebSocket.CONNECTING;this.URL=a;this.onopen=function(a){};this.onclose=function(a){};this.onmessage=function(a){};this.onerror=function(a){};f(a);this.send=function(d){if(c){if(b.debug||ReconnectingWebSocket.debugAll){console.debug("ReconnectingWebSocket","send",a,d)}return c.send(d)}else{throw"INVALID_STATE_ERR : Pausing to reconnect websocket"}};this.close=function(){if(c){d=true;c.close()}};this.refresh=function(){if(c){c.close()}}}ReconnectingWebSocket.debugAll=false diff --git a/transactions.json b/transactions.json deleted file mode 100644 index b234cb9d..00000000 --- a/transactions.json +++ /dev/null @@ -1 +0,0 @@ -{"transactions":[{"address":"12345","date":"2012-09-09","received":"5","exchange":"10","value":50},{"address":"12345","date":"2012-09-09","received":"5","exchange":"10","value":50},{"address":"12345","date":"2012-09-09","received":"5","exchange":"60","value":300}]} -- cgit v1.2.3