mirror of
https://github.com/btcpayserver/btcpayserver.git
synced 2025-02-22 14:22:40 +01:00
Add missing file
This commit is contained in:
parent
dea747a9a9
commit
6674d76d6d
1 changed files with 32 additions and 0 deletions
32
BTCPayServer/WebsocketExtensions.cs
Normal file
32
BTCPayServer/WebsocketExtensions.cs
Normal file
|
@ -0,0 +1,32 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Net.WebSockets;
|
||||
using System.Text;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace BTCPayServer
|
||||
{
|
||||
public static class WebsocketExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// NGINX closes websocket connections after 1 min if there is no activity, so here we do some busy work every 30s
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public static async Task<WebSocketReceiveResult> ReceiveAndPingAsync(this WebSocket webSocket, ArraySegment<byte> buffer, CancellationToken cancellationToken = default)
|
||||
{
|
||||
var waiting = Task.Delay(TimeSpan.FromSeconds(30), cancellationToken);
|
||||
var receiving = webSocket.ReceiveAsync(buffer, cancellationToken);
|
||||
wait:
|
||||
var completed = await Task.WhenAny(waiting, receiving);
|
||||
if (completed == waiting)
|
||||
{
|
||||
await webSocket.SendAsync(Encoding.UTF8.GetBytes("ping"), WebSocketMessageType.Text, true, cancellationToken);
|
||||
waiting = Task.Delay(TimeSpan.FromSeconds(30), cancellationToken);
|
||||
goto wait;
|
||||
}
|
||||
return receiving.Result;
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Reference in a new issue