From dea747a9a96dbd04578fd327e027abb81d24dfb5 Mon Sep 17 00:00:00 2001 From: "nicolas.dorier" Date: Tue, 5 Oct 2021 13:37:30 +0900 Subject: [PATCH] Keep connection alive on checkout page by sending ping messages --- BTCPayServer/Controllers/InvoiceController.UI.cs | 4 ++-- BTCPayServer/Views/Invoice/Checkout.cshtml | 3 +++ 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/BTCPayServer/Controllers/InvoiceController.UI.cs b/BTCPayServer/Controllers/InvoiceController.UI.cs index ac6b152d5..6f0690c6d 100644 --- a/BTCPayServer/Controllers/InvoiceController.UI.cs +++ b/BTCPayServer/Controllers/InvoiceController.UI.cs @@ -654,7 +654,7 @@ namespace BTCPayServer.Controllers [Route("invoice/{invoiceId}/status/ws")] [Route("invoice/{invoiceId}/{paymentMethodId}/status")] [Route("invoice/status/ws")] - public async Task GetStatusWebSocket(string invoiceId) + public async Task GetStatusWebSocket(string invoiceId, CancellationToken cancellationToken) { if (!HttpContext.WebSockets.IsWebSocketRequest) return NotFound(); @@ -670,7 +670,7 @@ namespace BTCPayServer.Controllers leases.Add(_EventAggregator.Subscribe(async o => await NotifySocket(webSocket, o.Invoice.Id, invoiceId))); while (true) { - var message = await webSocket.ReceiveAsync(DummyBuffer, default(CancellationToken)); + var message = await webSocket.ReceiveAndPingAsync(DummyBuffer, default(CancellationToken)); if (message.MessageType == WebSocketMessageType.Close) break; } diff --git a/BTCPayServer/Views/Invoice/Checkout.cshtml b/BTCPayServer/Views/Invoice/Checkout.cshtml index 8b21dc9c4..73b73c8e2 100644 --- a/BTCPayServer/Views/Invoice/Checkout.cshtml +++ b/BTCPayServer/Views/Invoice/Checkout.cshtml @@ -276,9 +276,12 @@ } ws_uri += "//" + loc.host; ws_uri += loc.pathname + "/status/ws?invoiceId=" + this.srvModel.invoiceId; + try { socket = new WebSocket(ws_uri); socket.onmessage = function (e) { + if (e.data === "ping") + return; self.fetchData(); }; socket.onerror = function (e) {