Skip to content

Commit be6e36a

Browse files
Disposed ClientWebSocket properly
1 parent 01c9ab4 commit be6e36a

File tree

1 file changed

+11
-22
lines changed

1 file changed

+11
-22
lines changed

src/XUMM.NET.SDK/WebSocket/XummWebSocket.cs

Lines changed: 11 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5,37 +5,37 @@
55
using System.Runtime.CompilerServices;
66
using System.Text;
77
using System.Threading;
8-
using System.Threading.Tasks;
98
using Microsoft.Extensions.Logging;
109

1110
namespace XUMM.NET.SDK.WebSocket;
1211

13-
public class XummWebSocket : IXummWebSocket, IAsyncDisposable
12+
public class XummWebSocket : IXummWebSocket
1413
{
1514
private readonly ILogger<XummWebSocket> _logger;
16-
private readonly ClientWebSocket _webSocket = new();
1715
private string _payloadUuid = default!;
1816

1917
public XummWebSocket(ILogger<XummWebSocket> logger)
2018
{
2119
_logger = logger;
2220
}
2321

24-
public async IAsyncEnumerable<string> SubscribeAsync(string payloadUuid, [EnumeratorCancellation] CancellationToken cancellationToken)
22+
public async IAsyncEnumerable<string> SubscribeAsync(string payloadUuid,
23+
[EnumeratorCancellation] CancellationToken cancellationToken)
2524
{
2625
cancellationToken.ThrowIfCancellationRequested();
2726

2827
_payloadUuid = payloadUuid;
2928

30-
await _webSocket.ConnectAsync(new Uri($"wss://xumm.app/sign/{_payloadUuid}"), CancellationToken.None);
29+
using var webSocket = new ClientWebSocket();
30+
await webSocket.ConnectAsync(new Uri($"wss://xumm.app/sign/{_payloadUuid}"), CancellationToken.None);
3131

32-
if (_webSocket.State == WebSocketState.Open)
32+
if (webSocket.State == WebSocketState.Open)
3333
{
34-
var buffer = new ArraySegment<byte>(new byte[1024]);
34+
_logger.LogInformation("Payload {0}: Subscription active (WebSocket opened).", _payloadUuid);
3535

36-
WriteLog("Subscription active (WebSocket opened).");
36+
var buffer = new ArraySegment<byte>(new byte[1024]);
3737

38-
while (_webSocket.State == WebSocketState.Open)
38+
while (webSocket.State == WebSocketState.Open)
3939
{
4040
await using var ms = new MemoryStream();
4141
WebSocketReceiveResult? result;
@@ -44,13 +44,13 @@ public async IAsyncEnumerable<string> SubscribeAsync(string payloadUuid, [Enumer
4444
{
4545
do
4646
{
47-
result = await _webSocket.ReceiveAsync(buffer, cancellationToken);
47+
result = await webSocket.ReceiveAsync(buffer, cancellationToken);
4848
ms.Write(buffer.Array!, buffer.Offset, result.Count);
4949
} while (!result.EndOfMessage && !cancellationToken.IsCancellationRequested);
5050
}
5151
catch (OperationCanceledException)
5252
{
53-
WriteLog("Subscription ended (WebSocket closed).");
53+
_logger.LogInformation("Payload {0}: Subscription ended (WebSocket closed).", _payloadUuid);
5454
yield break;
5555
}
5656

@@ -59,15 +59,4 @@ public async IAsyncEnumerable<string> SubscribeAsync(string payloadUuid, [Enumer
5959
}
6060
}
6161
}
62-
63-
private void WriteLog(string logMessage)
64-
{
65-
_logger?.LogInformation("Payload {0}: {1}", _payloadUuid, logMessage);
66-
}
67-
68-
public async ValueTask DisposeAsync()
69-
{
70-
await _webSocket.CloseAsync(WebSocketCloseStatus.Empty, string.Empty, CancellationToken.None);
71-
WriteLog("Subscription ended (WebSocket closed).");
72-
}
7362
}

0 commit comments

Comments
 (0)