From 5c8e62bd90d901fd68b0718111d2d12457a08199 Mon Sep 17 00:00:00 2001 From: "nicolas.dorier" Date: Tue, 5 Oct 2021 14:14:22 +0900 Subject: [PATCH] Websocket ping message for notifications and vault --- BTCPayServer/Views/Shared/_Layout.cshtml | 2 ++ BTCPayServer/WebSocketHelper.cs | 2 +- BTCPayServer/wwwroot/js/vaultbridge.js | 2 ++ 3 files changed, 5 insertions(+), 1 deletion(-) diff --git a/BTCPayServer/Views/Shared/_Layout.cshtml b/BTCPayServer/Views/Shared/_Layout.cshtml index eed6c96dc..2b2b63a7a 100644 --- a/BTCPayServer/Views/Shared/_Layout.cshtml +++ b/BTCPayServer/Views/Shared/_Layout.cshtml @@ -207,6 +207,8 @@ try { socket = new WebSocket(ws_uri); socket.onmessage = function (e) { + if (e.data === "ping") + return; $.get(newDataEndpoint, function (data) { $("#notifications-nav-item").replaceWith($(data)); }); diff --git a/BTCPayServer/WebSocketHelper.cs b/BTCPayServer/WebSocketHelper.cs index 77ff529e3..b9d96fc86 100644 --- a/BTCPayServer/WebSocketHelper.cs +++ b/BTCPayServer/WebSocketHelper.cs @@ -37,7 +37,7 @@ namespace BTCPayServer var newSize = _Buffer.Array.Length; while (true) { - var message = await Socket.ReceiveAsync(buffer, cancellation); + var message = await Socket.ReceiveAndPingAsync(buffer, cancellation); if (message.MessageType == WebSocketMessageType.Close) { await CloseSocketAndThrow(WebSocketCloseStatus.NormalClosure, "Close message received from the peer", cancellation); diff --git a/BTCPayServer/wwwroot/js/vaultbridge.js b/BTCPayServer/wwwroot/js/vaultbridge.js index c43efb42f..9f2579623 100644 --- a/BTCPayServer/wwwroot/js/vaultbridge.js +++ b/BTCPayServer/wwwroot/js/vaultbridge.js @@ -20,6 +20,8 @@ var vault = (function () { }; this.socket.onmessage = function (event) { if (typeof event.data === "string") { + if (event.data === "ping") + return; var jsonObject = JSON.parse(event.data); if (jsonObject.hasOwnProperty("params")) { var request = new XMLHttpRequest();