Keep connection alive on checkout page by sending ping messages

This commit is contained in:
nicolas.dorier
2021-10-05 13:37:30 +09:00
parent a32ace1dcb
commit dea747a9a9
2 changed files with 5 additions and 2 deletions

View File

@@ -654,7 +654,7 @@ namespace BTCPayServer.Controllers
[Route("invoice/{invoiceId}/status/ws")] [Route("invoice/{invoiceId}/status/ws")]
[Route("invoice/{invoiceId}/{paymentMethodId}/status")] [Route("invoice/{invoiceId}/{paymentMethodId}/status")]
[Route("invoice/status/ws")] [Route("invoice/status/ws")]
public async Task<IActionResult> GetStatusWebSocket(string invoiceId) public async Task<IActionResult> GetStatusWebSocket(string invoiceId, CancellationToken cancellationToken)
{ {
if (!HttpContext.WebSockets.IsWebSocketRequest) if (!HttpContext.WebSockets.IsWebSocketRequest)
return NotFound(); return NotFound();
@@ -670,7 +670,7 @@ namespace BTCPayServer.Controllers
leases.Add(_EventAggregator.Subscribe<Events.InvoiceEvent>(async o => await NotifySocket(webSocket, o.Invoice.Id, invoiceId))); leases.Add(_EventAggregator.Subscribe<Events.InvoiceEvent>(async o => await NotifySocket(webSocket, o.Invoice.Id, invoiceId)));
while (true) while (true)
{ {
var message = await webSocket.ReceiveAsync(DummyBuffer, default(CancellationToken)); var message = await webSocket.ReceiveAndPingAsync(DummyBuffer, default(CancellationToken));
if (message.MessageType == WebSocketMessageType.Close) if (message.MessageType == WebSocketMessageType.Close)
break; break;
} }

View File

@@ -276,9 +276,12 @@
} }
ws_uri += "//" + loc.host; ws_uri += "//" + loc.host;
ws_uri += loc.pathname + "/status/ws?invoiceId=" + this.srvModel.invoiceId; ws_uri += loc.pathname + "/status/ws?invoiceId=" + this.srvModel.invoiceId;
try { try {
socket = new WebSocket(ws_uri); socket = new WebSocket(ws_uri);
socket.onmessage = function (e) { socket.onmessage = function (e) {
if (e.data === "ping")
return;
self.fetchData(); self.fetchData();
}; };
socket.onerror = function (e) { socket.onerror = function (e) {