Skip to content

Commit 1dbb65c

Browse files
committed
📝 Add server-side example
1 parent 3788cfc commit 1dbb65c

2 files changed

Lines changed: 88 additions & 2 deletions

File tree

readme.md

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,4 +86,47 @@ the server to act on:
8686
await pipe.CompleteAsync(WebSocketCloseStatus.NormalClosure, "Done processing");
8787
```
8888

89-
Specifying a close status will always close the underlying socket.
89+
Specifying a close status will always close the underlying socket.
90+
91+
You can also use it on the server. This example is basically taken from
92+
the documentation on [WebSockets in ASP.NET Core](https://docs.microsoft.com/en-us/aspnet/core/fundamentals/websockets?view=aspnetcore-5.0#configure-the-middleware)
93+
and adapted to use a `WebSocketPipe` to read/write to the client:
94+
95+
```csharp
96+
app.Use(async (context, next) =>
97+
{
98+
if (context.Request.Path == "/ws")
99+
{
100+
if (context.WebSockets.IsWebSocketRequest)
101+
{
102+
using var websocket = await context.WebSockets.AcceptWebSocketAsync(
103+
context.WebSockets.WebSocketRequestedProtocols.FirstOrDefault());
104+
105+
using var pipe = WebSocketPipe.Create(websocket, options);
106+
await Task.WhenAll(Echo(pipe), pipe.RunAsync(context.RequestAborted));
107+
}
108+
else
109+
{
110+
context.Response.StatusCode = (int) HttpStatusCode.BadRequest;
111+
}
112+
}
113+
else
114+
{
115+
await next();
116+
}
117+
});
118+
```
119+
120+
The sample `Echo` method is simply:
121+
122+
```csharp
123+
async Task Echo(IDuplexPipe pipe)
124+
{
125+
while (await pipe.Input.ReadAsync() is var result && !result.IsCompleted)
126+
{
127+
// Just assume we get a single-segment entry, for simplicity
128+
await pipe.Output.WriteAsync(result.Buffer.First);
129+
pipe.Input.AdvanceTo(result.Buffer.End);
130+
}
131+
}
132+
```

src/WebSocketPipe/readme.md

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,4 +83,47 @@ the server to act on:
8383
await pipe.CompleteAsync(WebSocketCloseStatus.NormalClosure, "Done processing");
8484
```
8585

86-
Specifying a close status will always close the underlying socket.
86+
Specifying a close status will always close the underlying socket.
87+
88+
You can also use it on the server. This example is basically taken from
89+
the documentation on [WebSockets in ASP.NET Core](https://docs.microsoft.com/en-us/aspnet/core/fundamentals/websockets?view=aspnetcore-5.0#configure-the-middleware)
90+
and adapted to use a `WebSocketPipe` to read/write to the client:
91+
92+
```csharp
93+
app.Use(async (context, next) =>
94+
{
95+
if (context.Request.Path == "/ws")
96+
{
97+
if (context.WebSockets.IsWebSocketRequest)
98+
{
99+
using var websocket = await context.WebSockets.AcceptWebSocketAsync(
100+
context.WebSockets.WebSocketRequestedProtocols.FirstOrDefault());
101+
102+
using var pipe = WebSocketPipe.Create(websocket, options);
103+
await Task.WhenAll(Echo(pipe), pipe.RunAsync(context.RequestAborted));
104+
}
105+
else
106+
{
107+
context.Response.StatusCode = (int) HttpStatusCode.BadRequest;
108+
}
109+
}
110+
else
111+
{
112+
await next();
113+
}
114+
});
115+
```
116+
117+
The sample `Echo` method is simply:
118+
119+
```csharp
120+
async Task Echo(IDuplexPipe pipe)
121+
{
122+
while (await pipe.Input.ReadAsync() is var result && !result.IsCompleted)
123+
{
124+
// Just assume we get a single-segment entry, for simplicity
125+
await pipe.Output.WriteAsync(result.Buffer.First);
126+
pipe.Input.AdvanceTo(result.Buffer.End);
127+
}
128+
}
129+
```

0 commit comments

Comments
 (0)