added alternative to EventSource for incompatible browsers
This commit is contained in:
parent
a4ad464710
commit
d323d28dc9
|
@ -3,9 +3,9 @@
|
|||
|
||||
<link rel="stylesheet" href="lightningTip.css">
|
||||
|
||||
<script async defer src="lightningTip.js"></script>
|
||||
<script async defer src="https://cdn.rawgit.com/kazuhikoarase/qrcode-generator/886a247e/js/qrcode.js"></script>
|
||||
<script async defer src="https://cdn.rawgit.com/emn178/js-sha256/189bb9b0/build/sha256.min.js"></script>
|
||||
<script async defer src="https://cdn.rawgit.com/kazuhikoarase/qrcode-generator/886a247e/js/qrcode.js"></script>
|
||||
<script async defer src="lightningTip.js"></script>
|
||||
</head>
|
||||
|
||||
<div id="lightningTip">
|
||||
|
|
|
@ -1,7 +1,4 @@
|
|||
function resizeInput(element) {
|
||||
element.style.height = "auto";
|
||||
element.style.height = (element.scrollHeight) + "px";
|
||||
}
|
||||
var requestUrl = window.location.protocol + "//" + window.location.hostname + ":8081/";
|
||||
|
||||
// To prohibit multiple requests at the same time
|
||||
var running = false;
|
||||
|
@ -13,7 +10,6 @@ var defaultGetInvoice;
|
|||
|
||||
// TODO: show error when invoice expires
|
||||
// TODO: maybe don't show full invoice
|
||||
// TODO: proper url handling window.location.protocol + window.location.hostname + ":8081/getinvoice"
|
||||
// TODO: show price in dollar?
|
||||
function getInvoice() {
|
||||
if (running === false) {
|
||||
|
@ -41,7 +37,6 @@ function getInvoice() {
|
|||
|
||||
console.log("Got hash of invoice: " + hash);
|
||||
|
||||
// TODO: find alternative for Edge and IE
|
||||
console.log("Starting listening for invoice to get settled");
|
||||
|
||||
listenInvoiceSettled(hash);
|
||||
|
@ -87,7 +82,7 @@ function getInvoice() {
|
|||
|
||||
};
|
||||
|
||||
request.open("POST", "http://localhost:8081/getinvoice", true);
|
||||
request.open("POST", requestUrl + "getinvoice", true);
|
||||
request.send(data);
|
||||
|
||||
var button = document.getElementById("lightningTipGetInvoice");
|
||||
|
@ -114,16 +109,52 @@ function getInvoice() {
|
|||
}
|
||||
|
||||
function listenInvoiceSettled(hash) {
|
||||
var eventSrc = new EventSource("http://localhost:8081/eventsource");
|
||||
if (EventSource === undefined) {
|
||||
var eventSrc = new EventSource(requestUrl + "eventsource");
|
||||
|
||||
eventSrc.onmessage = function (event) {
|
||||
if (event.data === hash) {
|
||||
var wrapper = document.getElementById("lightningTip");
|
||||
eventSrc.onmessage = function (event) {
|
||||
if (event.data === hash) {
|
||||
eventSrc.close();
|
||||
|
||||
wrapper.innerHTML = "<p id=\"lightningTipLogo\">⚡</p>";
|
||||
wrapper.innerHTML += "<a id='lightningTipThankYou'>Thank you for your tip!</a>";
|
||||
}
|
||||
};
|
||||
showThankYouScreen();
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
} else {
|
||||
console.warn("Your browser does not support EventSource. Sending a request to the server every two second to check if the invoice is settled");
|
||||
|
||||
var interval = setInterval(function () {
|
||||
var request = new XMLHttpRequest();
|
||||
|
||||
request.onreadystatechange = function () {
|
||||
if (request.readyState === 4 && request.status === 200) {
|
||||
var json = JSON.parse(request.responseText);
|
||||
|
||||
if (json.Settled) {
|
||||
clearInterval(interval);
|
||||
|
||||
showThankYouScreen();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
request.open("POST", requestUrl + "invoicesettled", true);
|
||||
request.send(JSON.stringify({"InvoiceHash": hash}))
|
||||
|
||||
}, 2000);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
function showThankYouScreen() {
|
||||
var wrapper = document.getElementById("lightningTip");
|
||||
|
||||
wrapper.innerHTML = "<p id=\"lightningTipLogo\">⚡</p>";
|
||||
wrapper.innerHTML += "<a id='lightningTipThankYou'>Thank you for your tip!</a>";
|
||||
}
|
||||
|
||||
function starTimer(duration, element) {
|
||||
|
@ -140,6 +171,7 @@ function starTimer(duration, element) {
|
|||
}
|
||||
|
||||
}, 1000);
|
||||
|
||||
}
|
||||
|
||||
function showTimer(duration, element) {
|
||||
|
@ -156,6 +188,7 @@ function showTimer(duration, element) {
|
|||
} else {
|
||||
element.innerHTML = minutes + ":" + seconds;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
function addLeadingZeros(value) {
|
||||
|
@ -211,6 +244,7 @@ function createQRCode(typeNumber) {
|
|||
|
||||
createQRCode(typeNumber + 1);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
function copyToClipboard(element) {
|
||||
|
@ -238,4 +272,9 @@ function showErrorMessage(message) {
|
|||
button.innerHTML = defaultGetInvoice;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
function resizeInput(element) {
|
||||
element.style.height = "auto";
|
||||
element.style.height = (element.scrollHeight) + "px";
|
||||
}
|
||||
|
|
|
@ -15,6 +15,8 @@ import (
|
|||
|
||||
const eventChannel = "invoiceSettled"
|
||||
|
||||
const couldNotParseError = "Could not parse values from request"
|
||||
|
||||
var eventSrv *eventsource.Server
|
||||
|
||||
var pendingInvoices []PendingInvoice
|
||||
|
@ -30,20 +32,29 @@ func (pending PendingInvoice) Id() string { return "" }
|
|||
func (pending PendingInvoice) Event() string { return "" }
|
||||
func (pending PendingInvoice) Data() string { return pending.Hash }
|
||||
|
||||
type invoiceRequest struct {
|
||||
Amount int64
|
||||
Message string
|
||||
}
|
||||
|
||||
type invoiceResponse struct {
|
||||
Invoice string
|
||||
Expiry int64
|
||||
}
|
||||
|
||||
type invoiceRequest struct {
|
||||
Amount int64
|
||||
Message string
|
||||
type invoiceSettledRequest struct {
|
||||
InvoiceHash string
|
||||
}
|
||||
|
||||
type invoiceSettledResponse struct {
|
||||
Settled bool
|
||||
}
|
||||
|
||||
type errorResponse struct {
|
||||
Error string
|
||||
}
|
||||
|
||||
// TODO: write how to build and install in README
|
||||
// TODO: add option to show URI of Lightning node
|
||||
func main() {
|
||||
initLog()
|
||||
|
@ -61,6 +72,9 @@ func main() {
|
|||
http.HandleFunc("/getinvoice", getInvoiceHandler)
|
||||
http.HandleFunc("/eventsource", eventSrv.Handler(eventChannel))
|
||||
|
||||
// Alternative for browsers which don't support EventSource (Internet Explorer and Edge)
|
||||
http.HandleFunc("/invoicesettled", invoiceSettledHandler)
|
||||
|
||||
log.Info("Subscribing to invoices")
|
||||
|
||||
go func() {
|
||||
|
@ -93,7 +107,7 @@ func main() {
|
|||
|
||||
}
|
||||
|
||||
// Callbacks when an invoice gets settled
|
||||
// Callback for backend
|
||||
func publishInvoiceSettled(invoice string, eventSrv *eventsource.Server) {
|
||||
for index, pending := range pendingInvoices {
|
||||
if pending.Invoice == invoice {
|
||||
|
@ -110,8 +124,48 @@ func publishInvoiceSettled(invoice string, eventSrv *eventsource.Server) {
|
|||
|
||||
}
|
||||
|
||||
func invoiceSettledHandler(writer http.ResponseWriter, request *http.Request) {
|
||||
errorMessage := couldNotParseError
|
||||
|
||||
if request.Method == http.MethodPost {
|
||||
var body invoiceSettledRequest
|
||||
|
||||
data, _ := ioutil.ReadAll(request.Body)
|
||||
|
||||
err := json.Unmarshal(data, &body)
|
||||
|
||||
if err == nil {
|
||||
if body.InvoiceHash != "" {
|
||||
settled := true
|
||||
|
||||
for _, pending := range pendingInvoices {
|
||||
if pending.Hash == body.InvoiceHash {
|
||||
settled = false
|
||||
|
||||
break
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
writer.Write(marshalJson(invoiceSettledResponse{
|
||||
Settled: settled,
|
||||
}))
|
||||
|
||||
return
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
log.Error(errorMessage)
|
||||
|
||||
writeError(writer, errorMessage)
|
||||
}
|
||||
|
||||
func getInvoiceHandler(writer http.ResponseWriter, request *http.Request) {
|
||||
errorMessage := "Could not parse values from request"
|
||||
errorMessage := couldNotParseError
|
||||
|
||||
if request.Method == http.MethodPost {
|
||||
var body invoiceRequest
|
||||
|
|
Loading…
Reference in New Issue