Skip to content

Commit e02d2dc

Browse files
committed
Add CreatePipe extension method for WebSocket
This makes the API slightly more discoverable. Fixes #8
1 parent 31b3f66 commit e02d2dc

2 files changed

Lines changed: 27 additions & 1 deletion

File tree

src/Tests/EndToEnd.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public async Task RunAsync()
1616
var client = new ClientWebSocket();
1717

1818
await client.ConnectAsync(server.Uri, CancellationToken.None);
19-
using var pipe = WebSocketPipe.Create(client, closeWhenCompleted: true);
19+
using var pipe = client.CreatePipe(closeWhenCompleted: true);
2020

2121
var read = Task.Run(async () =>
2222
{
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
using System.ComponentModel;
2+
using System.IO.Pipelines;
3+
using Devlooped.Net;
4+
5+
namespace System.Net.WebSockets;
6+
7+
/// <summary>
8+
/// Provides the <see cref="CreatePipe"/> extension method for
9+
/// reading/writing to a <see cref="WebSocket"/> using the <see cref="IDuplexPipe"/> (and
10+
/// <see cref="IWebSocketPipe"/>) API.
11+
/// API.
12+
/// </summary>
13+
[EditorBrowsable(EditorBrowsableState.Never)]
14+
public static class WebSocketExtensions
15+
{
16+
/// <summary>
17+
/// Creates a <see cref="IWebSocketPipe"/> over the given <paramref name="webSocket"/>.
18+
/// </summary>
19+
/// <param name="webSocket">The <see cref="WebSocket"/> to adapt.</param>
20+
/// <param name="closeWhenCompleted">Whether to close the <paramref name="webSocket"/> when
21+
/// either the <see cref="IDuplexPipe.Input"/> or <see cref="IDuplexPipe.Output"/> is
22+
/// completed, or when <see cref="IWebSocketPipe.CompleteAsync"/> is invoked.</param>
23+
public static IWebSocketPipe CreatePipe(this WebSocket webSocket, bool closeWhenCompleted = false)
24+
=> WebSocketPipe.Create(webSocket, closeWhenCompleted ? new WebSocketPipeOptions { CloseWhenCompleted = true } : WebSocketPipeOptions.Default);
25+
}
26+

0 commit comments

Comments
 (0)