Skip to content

Commit 55a0b2e

Browse files
authored
Simplify markdown inclusions
1 parent 781719f commit 55a0b2e

3 files changed

Lines changed: 7 additions & 185 deletions

File tree

docs/sponsors.md

Lines changed: 0 additions & 9 deletions
This file was deleted.

readme.md

Lines changed: 3 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ High-performance System.IO.Pipelines API adapter for System.Net.WebSockets
88
[![License](https://img.shields.io/github/license/devlooped/WebSocketPipe.svg?color=blue)](https://github.com/devlooped/WebSocketPipe/blob/main/license.txt)
99
[![Build](https://github.com/devlooped/WebSocketPipe/workflows/build/badge.svg?branch=main)](https://github.com/devlooped/WebSocketPipe/actions)
1010

11+
<!-- #content -->
1112
# Usage
1213

1314
```csharp
@@ -130,6 +131,7 @@ async Task Echo(IDuplexPipe pipe)
130131
}
131132
}
132133
```
134+
<!-- #content -->
133135

134136
# Dogfooding
135137

@@ -145,45 +147,4 @@ The versioning scheme for packages is:
145147
- PR builds: *42.42.42-pr*`[NUMBER]`
146148
- Branch builds: *42.42.42-*`[BRANCH]`.`[COMMITS]`
147149

148-
149-
<!-- include docs/footer.md -->
150-
# Sponsors
151-
152-
<!-- sponsors.md -->
153-
<!-- sponsors -->
154-
155-
<a href='https://github.com/KirillOsenkov'>
156-
<img src='https://github.com/devlooped/sponsors/raw/main/.github/avatars/KirillOsenkov.svg' alt='Kirill Osenkov' title='Kirill Osenkov'>
157-
</a>
158-
<a href='https://github.com/augustoproiete'>
159-
<img src='https://github.com/devlooped/sponsors/raw/main/.github/avatars/augustoproiete.svg' alt='C. Augusto Proiete' title='C. Augusto Proiete'>
160-
</a>
161-
<a href='https://github.com/sandrock'>
162-
<img src='https://github.com/devlooped/sponsors/raw/main/.github/avatars/sandrock.svg' alt='SandRock' title='SandRock'>
163-
</a>
164-
<a href='https://github.com/aws'>
165-
<img src='https://github.com/devlooped/sponsors/raw/main/.github/avatars/aws.svg' alt='Amazon Web Services' title='Amazon Web Services'>
166-
</a>
167-
<a href='https://github.com/MelbourneDeveloper'>
168-
<img src='https://github.com/devlooped/sponsors/raw/main/.github/avatars/MelbourneDeveloper.svg' alt='Christian Findlay' title='Christian Findlay'>
169-
</a>
170-
<a href='https://github.com/clarius'>
171-
<img src='https://github.com/devlooped/sponsors/raw/main/.github/avatars/clarius.svg' alt='Clarius Org' title='Clarius Org'>
172-
</a>
173-
<a href='https://github.com/MFB-Technologies-Inc'>
174-
<img src='https://github.com/devlooped/sponsors/raw/main/.github/avatars/MFB-Technologies-Inc.svg' alt='MFB Technologies, Inc.' title='MFB Technologies, Inc.'>
175-
</a>
176-
177-
<!-- sponsors -->
178-
179-
<!-- sponsors.md -->
180-
181-
<br>&nbsp;
182-
<a href="https://github.com/sponsors/devlooped" title="Sponsor this project">
183-
<img src="https://github.com/devlooped/sponsors/blob/main/sponsor.png" />
184-
</a>
185-
<br>
186-
187-
[Learn more about GitHub Sponsors](https://github.com/sponsors)
188-
189-
<!-- docs/footer.md -->
150+
<!-- include https://github.com/devlooped/sponsors/raw/main/footer.md -->

src/WebSocketPipe/readme.md

Lines changed: 4 additions & 134 deletions
Original file line numberDiff line numberDiff line change
@@ -1,135 +1,5 @@
1-
[![Version](https://img.shields.io/nuget/vpre/WebSocketPipe.svg?color=royalblue)](https://www.nuget.org/packages/WebSocketPipe)
2-
[![Downloads](https://img.shields.io/nuget/dt/WebSocketPipe.svg?color=green)](https://www.nuget.org/packages/WebSocketPipe)
3-
[![License](https://img.shields.io/github/license/devlooped/WebSocketPipe.svg?color=blue)](https://github.com/devlooped/WebSocketPipe/blob/main/license.txt)
4-
5-
# Usage
6-
7-
```csharp
8-
using Devlooped.Net;
9-
10-
var client = new ClientWebSocket();
11-
await client.ConnectAsync(serverUri, CancellationToken.None);
12-
13-
using IWebSocketPipe pipe = WebSocketPipe.Create(client, closeWhenCompleted: true);
14-
15-
// Start the pipe before hooking up the processing
16-
var run = pipe.RunAsync();
17-
```
18-
19-
The `IWebSocketPipe` interface extends [IDuplexPipe](https://docs.microsoft.com/en-us/dotnet/api/system.io.pipelines.iduplexpipe?view=dotnet-plat-ext-5.0),
20-
exposing `Input` and `Output` properties that can be used to
21-
read incoming messages and write outgoing ones.
22-
23-
For example, to read incoming data and write it to the console,
24-
we could write the following code:
25-
26-
```csharp
27-
await ReadIncoming(pipe.Input);
28-
29-
async Task ReadIncoming(PipeReader reader)
30-
{
31-
while (await reader.ReadAsync() is var result && !result.IsCompleted)
32-
{
33-
Console.WriteLine($"Received: {Encoding.UTF8.GetString(result.Buffer)}");
34-
reader.AdvanceTo(result.Buffer.End);
35-
}
36-
Console.WriteLine($"Done reading.");
37-
}
38-
```
39-
40-
Similarly, to write to the underlying websocket the input
41-
entered in the console, we use code like the following:
42-
43-
```csharp
44-
await SendOutgoing(pipe.Output);
45-
46-
async Task SendOutgoing(PipeWriter writer)
47-
{
48-
while (Console.ReadLine() is var line && line?.Length > 0)
49-
{
50-
Encoding.UTF8.GetBytes(line, writer);
51-
}
52-
await writer.CompleteAsync();
53-
Console.WriteLine($"Done writing.");
54-
}
55-
```
56-
57-
If we wanted to simultaneously read and write and wait for
58-
completion of both operations, we could just wait for both
59-
tasks:
60-
61-
```csharp
62-
// Wait for completion of processing code
63-
await Task.WhenAny(
64-
ReadIncoming(pipe.Input),
65-
SendOutgoing(pipe.Output));
66-
```
67-
68-
Note that completing the `PipeWriter` automatically causes the
69-
reader to reveive a completed result and exit the loop. In addition,
70-
the overall `IWebSocketPipe.RunAsync` task will also run to completion.
71-
72-
73-
The `IWebSocketPipe` takes care of gracefully closing the connection
74-
when the input or output are completed, if `closeWhenCompleted` is set
75-
to true when creating it.
76-
77-
Alternatively, it's also possible to complete the entire pipe explicitly,
78-
while setting an optional socket close status and status description for
79-
the server to act on:
80-
81-
```csharp
82-
await pipe.CompleteAsync(WebSocketCloseStatus.NormalClosure, "Done processing");
83-
```
84-
85-
Specifying a close status will always close the underlying socket.
86-
87-
You can also use it on the server. This example is basically taken from
88-
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)
89-
and adapted to use a `WebSocketPipe` to read/write to the client:
90-
91-
```csharp
92-
app.Use(async (context, next) =>
93-
{
94-
if (context.Request.Path == "/ws")
95-
{
96-
if (context.WebSockets.IsWebSocketRequest)
97-
{
98-
using var websocket = await context.WebSockets.AcceptWebSocketAsync();
99-
using var pipe = WebSocketPipe.Create(websocket, true);
100-
await Task.WhenAll(Echo(pipe), pipe.RunAsync(context.RequestAborted));
101-
}
102-
else
103-
{
104-
context.Response.StatusCode = (int) HttpStatusCode.BadRequest;
105-
}
106-
}
107-
else
108-
{
109-
await next();
110-
}
111-
});
112-
```
113-
114-
The sample `Echo` method is simply:
115-
116-
```csharp
117-
async Task Echo(IDuplexPipe pipe)
118-
{
119-
while (await pipe.Input.ReadAsync() is var result && !result.IsCompleted)
120-
{
121-
// Just assume we get a single-segment entry, for simplicity
122-
await pipe.Output.WriteAsync(result.Buffer.First);
123-
pipe.Input.AdvanceTo(result.Buffer.End);
124-
}
125-
}
126-
```
127-
128-
129-
## Sponsors
130-
131-
[![sponsored](https://raw.githubusercontent.com/devlooped/oss/main/assets/images/sponsors.svg)](https://github.com/sponsors/devlooped) [![clarius](https://raw.githubusercontent.com/clarius/branding/main/logo/byclarius.svg)](https://github.com/clarius)[![clarius](https://raw.githubusercontent.com/clarius/branding/main/logo/logo.svg)](https://github.com/clarius)
132-
133-
*[get mentioned here too](https://github.com/sponsors/devlooped)!*
134-
1+
<!-- include ../../readme.md#content -->
2+
<!-- include https://github.com/devlooped/sponsors/raw/main/footer.md -->
1353

4+
<!-- Exclude from auto-expansion in CI -->
5+
<!-- exclude -->

0 commit comments

Comments
 (0)