aboutsummaryrefslogtreecommitdiffstats
path: root/js/bitcoin_pos.js
diff options
context:
space:
mode:
authorAdam Soltys <[email protected]>2012-10-24 22:28:59 -0700
committerAdam Soltys <[email protected]>2012-10-24 22:28:59 -0700
commit4f145a9c55272800571e776841e4d8eb2c0878fb (patch)
treeb8fc3c67d0622ae881bb87897c79e8d4695d373d /js/bitcoin_pos.js
parentab4107bd5384de79c39e9e290babe377df1ecf2a (diff)
downloadcoinos-server-4f145a9c55272800571e776841e4d8eb2c0878fb.tar.gz
coinos-server-4f145a9c55272800571e776841e4d8eb2c0878fb.zip
Set retry interval for socket connection
Diffstat (limited to 'js/bitcoin_pos.js')
-rw-r--r--js/bitcoin_pos.js84
1 files changed, 44 insertions, 40 deletions
diff --git a/js/bitcoin_pos.js b/js/bitcoin_pos.js
index 396a371e..9a2ac153 100644
--- a/js/bitcoin_pos.js
+++ b/js/bitcoin_pos.js
@@ -1,4 +1,5 @@
$(function() {
+ var websocket = null;
var address = getParameterByName('address');
var exchange = 0;
@@ -9,6 +10,7 @@ $(function() {
$('div.container-fluid').css('width', Math.min(screen.width, 600) + 'px');
$('#address').html(address);
setupQR();
+ setInterval(setupSocket, 5000);
$.getJSON('ticker.json', function (data) {
exchange = 1000 / data.out;
@@ -16,13 +18,13 @@ $(function() {
exchange = Math.ceil(exchange * 100) / 100;
$('#exchange').val(exchange.toFixed(2));
updateTotal();
- setupSocket();
});
$('#amount').keyup(updateTotal);
$('#amount').focus();
$('#amount').focus(function() {
$('#received').hide();
+ $('#payment').fadeIn('slow');
$(this).val('');
updateTotal();
});
@@ -31,55 +33,57 @@ $(function() {
var amount = parseFloat($('#amount').val());
var total = amount / exchange;
total = Math.ceil(total * 10000) / 10000;
- if (!$.isNumeric(total) || total == 0) {
- $('#payment').fadeOut('slow');
- } else {
- $('#payment').fadeIn('slow');
- $('#total').html(total.toString());
- displayQR('bitcoin:' + address + '?amount=' + total.toString());
+
+ if (!$.isNumeric(total)) {
+ total = '';
}
+
+ $('#total').html(total.toString());
+ displayQR('bitcoin:' + address + '?amount=' + total.toString());
}
function setupSocket() {
- var websocket = new ReconnectingWebSocket("ws://api.blockchain.info:8335/inv");
+ if (!websocket || websocket.readystate != 1) {
+ websocket = new WebSocket("ws://api.blockchain.info:8335/inv");
- websocket.onopen = function() {
- websocket.send('{"op":"addr_sub", "addr":"' + address + '"}');
- }
+ 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;
- }
- });
+ 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;
+ }
+ });
- $.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');
}
- });
- 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
+ }
+ );
}
-
- $.get('record_transaction.php',
- {
- address: from_address,
- date: moment().format("YYYY-MM-DD HH:mm:ss"),
- received: received,
- exchange: exchange
- }
- );
}
}
});