@@ -12,7 +12,7 @@ namespace Parse.Infrastructure.Execution;
1212/// Represents a WebSocket client that allows connecting to a WebSocket server, sending messages, and receiving messages.
1313/// Implements the <c>IWebSocketClient</c> interface for WebSocket operations.
1414/// </summary>
15- class TextWebSocketClient : IWebSocketClient
15+ class TextWebSocketClient ( int bufferSize ) : IWebSocketClient
1616{
1717 /// <summary>
1818 /// A private instance of the ClientWebSocket class used to manage the WebSocket connection.
@@ -43,6 +43,8 @@ class TextWebSocketClient : IWebSocketClient
4343
4444 private readonly object connectionLock = new object ( ) ;
4545
46+ private int BufferSize { get ; } = bufferSize ;
47+
4648 /// <summary>
4749 /// Opens a WebSocket connection to the specified server URI and starts listening for messages.
4850 /// If the connection is already open or in a connecting state, this method does nothing.
@@ -54,14 +56,19 @@ class TextWebSocketClient : IWebSocketClient
5456 /// </returns>
5557 public async Task OpenAsync ( string serverUri , CancellationToken cancellationToken = default )
5658 {
59+ ClientWebSocket webSocketToConnect = null ;
5760 lock ( connectionLock )
5861 {
5962 webSocket ??= new ClientWebSocket ( ) ;
63+ if ( webSocket . State != WebSocketState . Open && webSocket . State != WebSocketState . Connecting )
64+ {
65+ webSocketToConnect = webSocket ;
66+ }
6067 }
6168
62- if ( webSocket . State != WebSocketState . Open && webSocket . State != WebSocketState . Connecting )
69+ if ( webSocketToConnect is not null )
6370 {
64- await webSocket . ConnectAsync ( new Uri ( serverUri ) , cancellationToken ) ;
71+ await webSocketToConnect . ConnectAsync ( new Uri ( serverUri ) , cancellationToken ) ;
6572 StartListening ( cancellationToken ) ;
6673 }
6774 }
@@ -74,12 +81,17 @@ public async Task OpenAsync(string serverUri, CancellationToken cancellationToke
7481 /// <returns>
7582 /// A task representing the asynchronous operation of closing the WebSocket connection.
7683 /// </returns>
77- public async Task CloseAsync ( CancellationToken cancellationToken = default ) =>
78- await webSocket ? . CloseAsync ( WebSocketCloseStatus . NormalClosure , String . Empty , cancellationToken ) ! ;
84+ public async Task CloseAsync ( CancellationToken cancellationToken = default )
85+ {
86+ if ( webSocket is not null )
87+ {
88+ await webSocket ? . CloseAsync ( WebSocketCloseStatus . NormalClosure , String . Empty , cancellationToken ) ! ;
89+ }
90+ }
7991
8092 private async Task ListenForMessages ( CancellationToken cancellationToken )
8193 {
82- byte [ ] buffer = new byte [ 1024 * 4 ] ;
94+ byte [ ] buffer = new byte [ BufferSize ] ;
8395
8496 try
8597 {
@@ -183,6 +195,10 @@ private void StartListening(CancellationToken cancellationToken)
183195 public async Task SendAsync ( string message , CancellationToken cancellationToken = default )
184196 {
185197 ArgumentNullException . ThrowIfNull ( webSocket ) ;
198+ if ( webSocket . State != WebSocketState . Open )
199+ {
200+ throw new InvalidOperationException ( $ "WebSocket is not in Open state. Current state: { webSocket . State } ") ;
201+ }
186202 await webSocket . SendAsync ( Encoding . UTF8 . GetBytes ( message ) , WebSocketMessageType . Text , true , cancellationToken ) ;
187203 }
188204}
0 commit comments