66
77namespace NBomber . WebSockets ;
88
9+ /// <summary>
10+ /// Represents configuration settings for a WebSocket connection.
11+ /// </summary>
912public class WebSocketConfig
1013{
14+ /// <summary>
15+ /// Gets or sets the default buffer size (in bytes) for WebSocket communication.
16+ /// Default is 16 KB (16,384 bytes).
17+ /// </summary>
1118 public int DefaultBufferSize { get ; set ; } = 16384 ; // 16KB
1219}
1320
1421/// <summary>
1522/// Provides a client for connecting to WebSocket services.
16- /// WebSocket type is a wrapper over native .NET <see cref="ClientWebSocket"/> type.
23+ /// This class wraps the native .NET <see cref="ClientWebSocket"/> and handles sending,
24+ /// receiving, and managing WebSocket communication with stream-based buffering and message queuing.
1725/// </summary>
1826public class WebSocket ( WebSocketConfig config ) : IDisposable
1927{
@@ -23,7 +31,14 @@ public class WebSocket(WebSocketConfig config) : IDisposable
2331 private bool _isListenUpdates = false ;
2432 private long _msgReceivedCount ;
2533
34+ /// <summary>
35+ /// Gets the underlying <see cref="ClientWebSocket"/> instance used for communication.
36+ /// </summary>
2637 public ClientWebSocket Client { get ; } = new ( ) ;
38+
39+ /// <summary>
40+ /// Gets the total number of messages received by the client.
41+ /// </summary>
2742 public long MsgReceivedCount => _msgReceivedCount ;
2843
2944 private async Task StartListenOnUpdates ( )
@@ -71,38 +86,39 @@ private async Task StartListenOnUpdates()
7186 }
7287
7388 /// <summary>
74- /// This method should be used to connect to a WebSocket server asynchronously .
89+ /// Asynchronously connects to a WebSocket server using a string URL .
7590 /// </summary>
76- /// <param name="url">The URL of the WebSocket server to connect to .</param>
77- /// <param name="cancellationToken">A <see cref="CancellationToken"/> used to propagate notification that the operation should be canceled .</param>
78- /// <exception cref="WebSocketException">Thrown when an error occurs during WebSocket communication .</exception>
79- /// <exception cref="OperationCanceledException">Thrown if the receive operation is canceled by the provided cancellation token .</exception>
91+ /// <param name="url">The WebSocket server URL .</param>
92+ /// <param name="cancellationToken">Token to signal cancellation .</param>
93+ /// <exception cref="WebSocketException">Thrown on connection failure .</exception>
94+ /// <exception cref="OperationCanceledException">Thrown if the operation is canceled.</exception>
8095 public async Task Connect ( string url , CancellationToken cancellationToken = default )
8196 {
8297 await Client . ConnectAsync ( new Uri ( url ) , cancellationToken ) ;
8398 _ = StartListenOnUpdates ( ) ;
8499 }
85100
86101 /// <summary>
87- /// This method should be used to connect to a WebSocket server asynchronously .
102+ /// Asynchronously connects to a WebSocket server using a URI .
88103 /// </summary>
89- /// <param name="uri">The URI of the WebSocket server to connect to .</param>
90- /// <param name="cancellationToken">A <see cref="CancellationToken"/> used to propagate notification that the operation should be canceled .</param>
91- /// <exception cref="WebSocketException">Thrown when an error occurs during WebSocket communication .</exception>
92- /// <exception cref="OperationCanceledException">Thrown if the receive operation is canceled by the provided cancellation token .</exception>
104+ /// <param name="uri">The URI of the WebSocket server.</param>
105+ /// <param name="cancellationToken">Token to signal cancellation .</param>
106+ /// <exception cref="WebSocketException">Thrown on connection failure .</exception>
107+ /// <exception cref="OperationCanceledException">Thrown if the operation is canceled.</exception>
93108 public async Task Connect ( Uri uri , CancellationToken cancellationToken = default )
94109 {
95110 await Client . ConnectAsync ( uri , cancellationToken ) ;
96111 _ = StartListenOnUpdates ( ) ;
97112 }
98113
99114 /// <summary>
100- /// This method should be used to send data on WebSocket asynchronously .
115+ /// Sends a UTF-8 encoded text message over the WebSocket connection .
101116 /// </summary>
102117 /// <param name="text">The text to be sent. It will be encoded using UTF8 encoding.</param>
103- /// <param name="cancellationToken">A <see cref="CancellationToken"/> used to propagate notification that the operation should be canceled.</param>
104- /// <exception cref="WebSocketException">Thrown when an error occurs during WebSocket communication.</exception>
105- /// <exception cref="OperationCanceledException">Thrown if the receive operation is canceled by the provided cancellation token.</exception>
118+ /// <param name="cancellationToken">Token to signal cancellation.</param>
119+ /// <returns>A task representing the asynchronous send operation.</returns>
120+ /// <exception cref="WebSocketException">Thrown on send failure.</exception>
121+ /// <exception cref="OperationCanceledException">Thrown if the operation is canceled.</exception>
106122 public ValueTask Send ( string text , CancellationToken cancellationToken = default )
107123 {
108124 using var ms = MsStreamManager . GetStream ( ) ;
@@ -117,24 +133,26 @@ public ValueTask Send(string text, CancellationToken cancellationToken = default
117133 }
118134
119135 /// <summary>
120- /// This method should be used to send data on WebSocket asynchronously .
136+ /// Sends binary data over the WebSocket connection .
121137 /// </summary>
122- /// <param name="payload">The binary payload to be sent.</param>
123- /// <param name="cancellationToken">A <see cref="CancellationToken"/> used to propagate notification that the operation should be canceled.</param>
124- /// <exception cref="WebSocketException">Thrown when an error occurs during WebSocket communication.</exception>
125- /// <exception cref="OperationCanceledException">Thrown if the receive operation is canceled by the provided cancellation token.</exception>
138+ /// <param name="payload">The binary payload.</param>
139+ /// <param name="cancellationToken">Token to signal cancellation.</param>
140+ /// <returns>A task representing the asynchronous send operation.</returns>
141+ /// <exception cref="WebSocketException">Thrown on send failure.</exception>
142+ /// <exception cref="OperationCanceledException">Thrown if the operation is canceled.</exception>
126143 public ValueTask Send ( ReadOnlyMemory < byte > payload , CancellationToken cancellationToken = default )
127144 {
128145 return Client . SendAsync ( payload , WebSocketMessageType . Binary , true , cancellationToken ) ;
129146 }
130147
131148 /// <summary>
132- /// This method should be used to receive a WebSocket message from the connected WebSocket asynchronously.
133- /// This method returns <see cref="WebSocketResponse"/> that should be disposed of after usage.
149+ /// Asynchronously receives a WebSocket message. The returned <see cref="WebSocketResponse"/>
150+ /// should be disposed after usage to free the underlying stream .
134151 /// </summary>
135- /// <param name="cancellationToken">A <see cref="CancellationToken"/> used to propagate notification that the operation should be canceled.</param>
136- /// <exception cref="WebSocketException">Thrown when an error occurs during WebSocket communication.</exception>
137- /// <exception cref="OperationCanceledException">Thrown if the receive operation is canceled by the provided cancellation token.</exception>
152+ /// <param name="cancellationToken">Token to signal cancellation.</param>
153+ /// <returns>A task that returns a <see cref="WebSocketResponse"/>.</returns>
154+ /// <exception cref="WebSocketException">Thrown if the client is not listening or in an invalid state.</exception>
155+ /// <exception cref="OperationCanceledException">Thrown if the receive is canceled.</exception>
138156 public async ValueTask < WebSocketResponse > Receive ( CancellationToken cancellationToken = default )
139157 {
140158 try
@@ -152,18 +170,22 @@ public async ValueTask<WebSocketResponse> Receive(CancellationToken cancellation
152170 }
153171
154172 /// <summary>
155- /// This method should be used to close WebSocket connection asynchronously .
173+ /// Gracefully closes the WebSocket connection with the specified status .
156174 /// </summary>
157- /// <param name="closeStatus">The WebSocket close status.</param>
158- /// <param name="cancellationToken">A <see cref="CancellationToken"/> used to propagate notification that the operation should be canceled.</param>
159- /// <exception cref="WebSocketException">Thrown when an error occurs during WebSocket communication.</exception>
160- /// <exception cref="OperationCanceledException">Thrown if the receive operation is canceled by the provided cancellation token.</exception>
175+ /// <param name="closeStatus">The close status. Default is NormalClosure.</param>
176+ /// <param name="cancellationToken">Token to signal cancellation.</param>
177+ /// <returns>A task representing the asynchronous close operation.</returns>
178+ /// <exception cref="WebSocketException">Thrown on close failure.</exception>
179+ /// <exception cref="OperationCanceledException">Thrown if the operation is canceled.</exception>
161180 public async Task Close ( WebSocketCloseStatus closeStatus = WebSocketCloseStatus . NormalClosure , CancellationToken cancellationToken = default )
162181 {
163182 await Client . CloseAsync ( closeStatus , null , cancellationToken ) ;
164183 _cts . Cancel ( ) ;
165184 }
166185
186+ /// <summary>
187+ /// Disposes the WebSocket client and cancels ongoing operations.
188+ /// </summary>
167189 public void Dispose ( )
168190 {
169191 Client . Dispose ( ) ;
@@ -172,13 +194,24 @@ public void Dispose()
172194}
173195
174196/// <summary>
175- /// Represents a response from a WebSocket. After usage the instance should be disposed.
197+ /// Represents a response message from the WebSocket.
198+ /// The response contains message data and its type, and must be disposed to free resources.
176199/// </summary>
177200public readonly struct WebSocketResponse ( RecyclableMemoryStream memoryStream , WebSocketMessageType messageType ) : IDisposable
178201{
202+ /// <summary>
203+ /// Gets the message payload as a read-only byte memory block.
204+ /// </summary>
179205 public ReadOnlyMemory < byte > Data { get ; } = memoryStream . GetBuffer ( ) . AsMemory ( 0 , ( int ) memoryStream . Length ) ;
206+
207+ /// <summary>
208+ /// Gets the WebSocket message type (Text, Binary, or Close).
209+ /// </summary>
180210 public WebSocketMessageType MessageType { get ; } = messageType ;
181211
212+ /// <summary>
213+ /// Releases the memory stream associated with this response.
214+ /// </summary>
182215 public void Dispose ( )
183216 {
184217 memoryStream . Dispose ( ) ;
0 commit comments