diff options
| author | Adam Soltys <[email protected]> | 2012-10-24 15:35:47 -0700 |
|---|---|---|
| committer | Adam Soltys <[email protected]> | 2012-10-24 15:35:47 -0700 |
| commit | 70fd095f6084802fd48f4cc0c8f605f85a168a46 (patch) | |
| tree | 66460fc9b7cfa1070ee5348fbed9c85d26a92767 /js/bitcoin_pos.js | |
| parent | 94e17c0d9fb972cfa0bac3267a1761520eee6fd5 (diff) | |
| download | coinos-server-70fd095f6084802fd48f4cc0c8f605f85a168a46.tar.gz coinos-server-70fd095f6084802fd48f4cc0c8f605f85a168a46.zip | |
Use reconnecting websocket
Diffstat (limited to 'js/bitcoin_pos.js')
| -rw-r--r-- | js/bitcoin_pos.js | 102 |
1 files changed, 49 insertions, 53 deletions
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; -} - |
