From f2820944f1cd69a72d93b715712f76cfd896d2a4 Mon Sep 17 00:00:00 2001 From: Serge Victor Date: Wed, 21 Sep 2016 10:54:09 +0000 Subject: [PATCH 1/2] Adding possibility of websockets configuration Up to now, websockets configuration was possible for the daemon only, this patch passes that information to the client via json file produced with the payment request. --- lib/wallet.py | 2 ++ lib/www/index.html | 6 ++++-- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/lib/wallet.py b/lib/wallet.py index a97c3605..6209ad82 100644 --- a/lib/wallet.py +++ b/lib/wallet.py @@ -1052,6 +1052,8 @@ class Abstract_Wallet(PrintError): out['request_url'] = os.path.join(baseurl, key) out['URI'] += '&r=' + out['request_url'] out['index_url'] = os.path.join(baseurl, 'index.html') + '?id=' + key + out['websocket_server'] = config.get('websocket_server', 'localhost') + out['websocket_port'] = config.get('websocket_port', 9999) return out def get_request_status(self, key): diff --git a/lib/www/index.html b/lib/www/index.html index a1e2a702..2908913f 100644 --- a/lib/www/index.html +++ b/lib/www/index.html @@ -38,7 +38,9 @@ if (id) { var current; var max = 100; var initial = data.time; - var duration = data.exp; + var duration = data.exp; + var websocket_server = data.websocket_server; + var websocket_port = data.websocket_port; if(duration){ var current = 100 * (Math.floor(Date.now()/1000) - initial)/duration; $("#progressbar").progressbar({ @@ -64,7 +66,7 @@ if (id) { }); }; -var ws = new WebSocket("wss://" + window.location.hostname + ":9999/"); +var ws = new WebSocket("wss://" + websocket_server + ":" + websocket_port +"/"); ws.onopen = function() { ws.send('id:' + id); }; From 90fe6292cab98510e09d2acb3f93982a516ca209 Mon Sep 17 00:00:00 2001 From: Serge Victor Date: Wed, 21 Sep 2016 12:45:50 +0000 Subject: [PATCH 2/2] $.getJSON is asynchronous, obviosly. --- lib/www/index.html | 33 ++++++++++++++++++--------------- 1 file changed, 18 insertions(+), 15 deletions(-) diff --git a/lib/www/index.html b/lib/www/index.html index 2908913f..4135ce44 100644 --- a/lib/www/index.html +++ b/lib/www/index.html @@ -34,13 +34,13 @@ if (id) { $("

").text(data.amount/100000000 + "BTC").appendTo($("p#amount")); $("a").attr("href", data.URI); $("

").text("Powered by Electrum").appendTo($("p#powered")); + var websocket_server = data.websocket_server; + var websocket_port = data.websocket_port; $(function () { var current; var max = 100; var initial = data.time; var duration = data.exp; - var websocket_server = data.websocket_server; - var websocket_port = data.websocket_port; if(duration){ var current = 100 * (Math.floor(Date.now()/1000) - initial)/duration; $("#progressbar").progressbar({ @@ -59,6 +59,22 @@ if (id) { var interval = setInterval(update, 1000); } }); + + var wss_address = "wss://" + websocket_server + ":" + websocket_port +"/"); + console.log("Opening WSS: " + wss_address) + var ws = new WebSocket(wss_address); + + ws.onopen = function() { + ws.send('id:' + id); + }; + ws.onmessage = function (evt) { + var received_msg = evt.data; + if(received_msg == 'paid'){ + $("#container").html("This invoice has been paid."); + } + else alert("Message is received:"+ received_msg); + }; + }) .fail(function() { console.log("error fail"); @@ -66,19 +82,6 @@ if (id) { }); }; -var ws = new WebSocket("wss://" + websocket_server + ":" + websocket_port +"/"); -ws.onopen = function() { - ws.send('id:' + id); -}; -ws.onmessage = function (evt) { - var received_msg = evt.data; - if(received_msg == 'paid'){ - $("#container").html("This invoice has been paid."); - } - else alert("Message is received:"+ received_msg); -}; - - // See http://stackoverflow.com/questions/29186154/chrome-clicking-mailto-links-closes-websocket-connection $(document).on('click', 'a[href^="bitcoin:"]', function (e) { e.preventDefault();