5
5
using System . Runtime . CompilerServices ;
6
6
using System . Text ;
7
7
using System . Threading ;
8
- using System . Threading . Tasks ;
9
8
using Microsoft . Extensions . Logging ;
10
9
11
10
namespace XUMM . NET . SDK . WebSocket ;
12
11
13
- public class XummWebSocket : IXummWebSocket , IAsyncDisposable
12
+ public class XummWebSocket : IXummWebSocket
14
13
{
15
14
private readonly ILogger < XummWebSocket > _logger ;
16
- private readonly ClientWebSocket _webSocket = new ( ) ;
17
15
private string _payloadUuid = default ! ;
18
16
19
17
public XummWebSocket ( ILogger < XummWebSocket > logger )
20
18
{
21
19
_logger = logger ;
22
20
}
23
21
24
- public async IAsyncEnumerable < string > SubscribeAsync ( string payloadUuid , [ EnumeratorCancellation ] CancellationToken cancellationToken )
22
+ public async IAsyncEnumerable < string > SubscribeAsync ( string payloadUuid ,
23
+ [ EnumeratorCancellation ] CancellationToken cancellationToken )
25
24
{
26
25
cancellationToken . ThrowIfCancellationRequested ( ) ;
27
26
28
27
_payloadUuid = payloadUuid ;
29
28
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 ) ;
31
31
32
- if ( _webSocket . State == WebSocketState . Open )
32
+ if ( webSocket . State == WebSocketState . Open )
33
33
{
34
- var buffer = new ArraySegment < byte > ( new byte [ 1024 ] ) ;
34
+ _logger . LogInformation ( "Payload {0}: Subscription active (WebSocket opened)." , _payloadUuid ) ;
35
35
36
- WriteLog ( "Subscription active (WebSocket opened)." ) ;
36
+ var buffer = new ArraySegment < byte > ( new byte [ 1024 ] ) ;
37
37
38
- while ( _webSocket . State == WebSocketState . Open )
38
+ while ( webSocket . State == WebSocketState . Open )
39
39
{
40
40
await using var ms = new MemoryStream ( ) ;
41
41
WebSocketReceiveResult ? result ;
@@ -44,13 +44,13 @@ public async IAsyncEnumerable<string> SubscribeAsync(string payloadUuid, [Enumer
44
44
{
45
45
do
46
46
{
47
- result = await _webSocket . ReceiveAsync ( buffer , cancellationToken ) ;
47
+ result = await webSocket . ReceiveAsync ( buffer , cancellationToken ) ;
48
48
ms . Write ( buffer . Array ! , buffer . Offset , result . Count ) ;
49
49
} while ( ! result . EndOfMessage && ! cancellationToken . IsCancellationRequested ) ;
50
50
}
51
51
catch ( OperationCanceledException )
52
52
{
53
- WriteLog ( " Subscription ended (WebSocket closed).") ;
53
+ _logger . LogInformation ( "Payload {0}: Subscription ended (WebSocket closed).", _payloadUuid ) ;
54
54
yield break ;
55
55
}
56
56
@@ -59,15 +59,4 @@ public async IAsyncEnumerable<string> SubscribeAsync(string payloadUuid, [Enumer
59
59
}
60
60
}
61
61
}
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
- }
73
62
}
0 commit comments