Skip to content

Commit

Permalink
split nuget and github README.md due to different feature support
Browse files Browse the repository at this point in the history
  • Loading branch information
luca-schlecker committed Apr 30, 2024
1 parent 4bd7a50 commit 6902b4d
Show file tree
Hide file tree
Showing 3 changed files with 148 additions and 6 deletions.
14 changes: 10 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@

![SwebSocket Logo](https://raw.githubusercontent.com/luca-schlecker/SwebSocket/main/icon.png)

# SwebSocket
<p align="center">
<img src="https://raw.githubusercontent.com/luca-schlecker/SwebSocket/main/icon.png" />
</p>
<h1 align="center">SwebSocket</h1>
<p align="center">
<img src="https://img.shields.io/nuget/dt/SwebSocket" alt="NuGet Downloads" />
<img src="https://img.shields.io/github/license/luca-schlecker/SwebSocket" alt="GitHub License" />
<img src="https://img.shields.io/nuget/v/SwebSocket" alt="NuGet Version" />
</p>

A handwritten, easy to use WebSocket Library that aims to:
- Follow [RFC6455](https://datatracker.ietf.org/doc/html/rfc6455)
- Be ease to use
- Allow [Client](#client) and [Server](#server) use-cases
- Integrate [Secure Connections](#secure-connection)
- High Compatibility (`netstandard2.1`)
- Be Portable (`netstandard2.1`)

## Disclaimer

Expand Down
136 changes: 136 additions & 0 deletions README.nuget.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@

# ![SwebSocket Logo](https://raw.githubusercontent.com/luca-schlecker/SwebSocket/main/icon.png) SwebSocket

![NuGet Downloads](https://img.shields.io/nuget/dt/SwebSocket) ![GitHub License](https://img.shields.io/github/license/luca-schlecker/SwebSocket) ![NuGet Version](https://img.shields.io/nuget/v/SwebSocket)

A handwritten, easy to use WebSocket Library that aims to:
- Follow [RFC6455](https://datatracker.ietf.org/doc/html/rfc6455)
- Be ease to use
- Allow [Client](#client) and [Server](#server) use-cases
- Integrate [Secure Connections](#secure-connection)
- Be Portable (`netstandard2.1`)

## Disclaimer

> ⚠️ Warning ⚠️
>
> This is a fun-project and may contain severe errors. You should probably not use this in production.
## Examples
### Client
#### Echo Client (Manual)
```cs
using SwebSocket;

var ws = WebSocket
.Connect()
.To("wss://echo.websocket.org/")
.Build();

// Discard first message
_ = ws.Receive();

while (true) {
var line = Console.ReadLine();
if (line == null) break;
ws.Send(line);
var answer = ws.Receive();
if (answer is TextMessage text)
Console.WriteLine($"Received: {text.Text}");
}
```

#### Echo Client (Background Poller)
```cs
using SwebSocket;

var ws = WebSocket
.Connect()
.To("wss://echo.websocket.org/")
.Build();

ws.OnMessage += (_, message) => {
if (message is TextMessage text)
Console.WriteLine($"Received: {text.Text}");
};

BackgroundMessagePoller.Poll(ws);

while (true) {
var line = Console.ReadLine();
if (line == null) break;
ws.Send(line);
}
```

> 💬 Important 💬
>
> The `WebSocket.OnMessage` event will (by default) **not** be raised.
> You can raise it manually by calling `WebSocket.EmitMessage(Message)` or use a Poller which will call it under the hood.
### Server
#### Echo Server (Manual)
```cs
using SwebSocket;

var listener = new Listener(IPAddress.Any, 3000);

while (true) {
var ws = listener.Accept();

try {
while (true) {
var message = ws.Receive();
ws.Send(message);
}
}
catch { }
}
```

#### Echo Server (Blocking Poller)
```cs
using SwebSocket;

var listener = new Listener(IPAddress.Any, 3000);

while (true) {
var ws = listener.Accept();

ws.OnMessage += (_, m) => ws.Send(m);
new BlockingMessagePoller(ws).Poll();
}
```

### Secure Connection
#### Client
```cs
using SwebSocket;

var listener = new Listener(IPAddress.Any, 3000)
.UseSsl(SslOptions.NoSsl) // No Ssl
.UseSsl(SslOptions.ForServer(identity)); // X509Certificate2 as Identity
// ...
```
#### Server
```cs
using SwebSocket;

var ws = WebSocket
.Connect()
.To("wss://127.0.0.1/")
.UseSsl(SslOptions.ForClient(
"custom authority", // The name to validate the certificate against
caCertificates // X509Certificate2Collection
))
.Build();

// ...
```

## Related Projects
- [WebSocket-Sharp](https://github.com/sta/websocket-sharp)
- [System.Net.WebSockets](https://learn.microsoft.com/en-us/dotnet/api/system.net.websockets)
- [Fleck](https://github.com/statianzo/Fleck)
- [WatsonWebsockte](https://github.com/jchristn/WatsonWebsocket)
4 changes: 2 additions & 2 deletions SwebSocket.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<TargetFramework>netstandard2.1</TargetFramework>
<Nullable>enable</Nullable>
<LangVersion>10.0</LangVersion>
<PackageReadmeFile>README.md</PackageReadmeFile>
<PackageReadmeFile>README.nuget.md</PackageReadmeFile>
<Title>SwebSocket</Title>
<Description>An easy to use WebSocket library.</Description>
<Authors>Luca Schlecker</Authors>
Expand All @@ -17,7 +17,7 @@

<ItemGroup>
<PackageReference Include="HttpMachine.PCL" Version="4.0.3" />
<None Include="README.md" Pack="true" PackagePath="/"/>
<None Include="README.nuget.md" Pack="true" PackagePath="/"/>
<None Include="icon.png" Pack="true" PackagePath="/"/>
</ItemGroup>

Expand Down

0 comments on commit 6902b4d

Please sign in to comment.