Skip to content

Commit 6902b4d

Browse files
split nuget and github README.md due to different feature support
1 parent 4bd7a50 commit 6902b4d

File tree

3 files changed

+148
-6
lines changed

3 files changed

+148
-6
lines changed

README.md

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,20 @@
11

2-
![SwebSocket Logo](https://raw.githubusercontent.com/luca-schlecker/SwebSocket/main/icon.png)
3-
4-
# SwebSocket
2+
<p align="center">
3+
<img src="https://raw.githubusercontent.com/luca-schlecker/SwebSocket/main/icon.png" />
4+
</p>
5+
<h1 align="center">SwebSocket</h1>
6+
<p align="center">
7+
<img src="https://img.shields.io/nuget/dt/SwebSocket" alt="NuGet Downloads" />
8+
<img src="https://img.shields.io/github/license/luca-schlecker/SwebSocket" alt="GitHub License" />
9+
<img src="https://img.shields.io/nuget/v/SwebSocket" alt="NuGet Version" />
10+
</p>
511

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

1319
## Disclaimer
1420

README.nuget.md

Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
2+
# ![SwebSocket Logo](https://raw.githubusercontent.com/luca-schlecker/SwebSocket/main/icon.png) SwebSocket
3+
4+
![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)
5+
6+
A handwritten, easy to use WebSocket Library that aims to:
7+
- Follow [RFC6455](https://datatracker.ietf.org/doc/html/rfc6455)
8+
- Be ease to use
9+
- Allow [Client](#client) and [Server](#server) use-cases
10+
- Integrate [Secure Connections](#secure-connection)
11+
- Be Portable (`netstandard2.1`)
12+
13+
## Disclaimer
14+
15+
> ⚠️ Warning ⚠️
16+
>
17+
> This is a fun-project and may contain severe errors. You should probably not use this in production.
18+
19+
## Examples
20+
### Client
21+
#### Echo Client (Manual)
22+
```cs
23+
using SwebSocket;
24+
25+
var ws = WebSocket
26+
.Connect()
27+
.To("wss://echo.websocket.org/")
28+
.Build();
29+
30+
// Discard first message
31+
_ = ws.Receive();
32+
33+
while (true) {
34+
var line = Console.ReadLine();
35+
if (line == null) break;
36+
ws.Send(line);
37+
var answer = ws.Receive();
38+
if (answer is TextMessage text)
39+
Console.WriteLine($"Received: {text.Text}");
40+
}
41+
```
42+
43+
#### Echo Client (Background Poller)
44+
```cs
45+
using SwebSocket;
46+
47+
var ws = WebSocket
48+
.Connect()
49+
.To("wss://echo.websocket.org/")
50+
.Build();
51+
52+
ws.OnMessage += (_, message) => {
53+
if (message is TextMessage text)
54+
Console.WriteLine($"Received: {text.Text}");
55+
};
56+
57+
BackgroundMessagePoller.Poll(ws);
58+
59+
while (true) {
60+
var line = Console.ReadLine();
61+
if (line == null) break;
62+
ws.Send(line);
63+
}
64+
```
65+
66+
> 💬 Important 💬
67+
>
68+
> The `WebSocket.OnMessage` event will (by default) **not** be raised.
69+
> You can raise it manually by calling `WebSocket.EmitMessage(Message)` or use a Poller which will call it under the hood.
70+
71+
### Server
72+
#### Echo Server (Manual)
73+
```cs
74+
using SwebSocket;
75+
76+
var listener = new Listener(IPAddress.Any, 3000);
77+
78+
while (true) {
79+
var ws = listener.Accept();
80+
81+
try {
82+
while (true) {
83+
var message = ws.Receive();
84+
ws.Send(message);
85+
}
86+
}
87+
catch { }
88+
}
89+
```
90+
91+
#### Echo Server (Blocking Poller)
92+
```cs
93+
using SwebSocket;
94+
95+
var listener = new Listener(IPAddress.Any, 3000);
96+
97+
while (true) {
98+
var ws = listener.Accept();
99+
100+
ws.OnMessage += (_, m) => ws.Send(m);
101+
new BlockingMessagePoller(ws).Poll();
102+
}
103+
```
104+
105+
### Secure Connection
106+
#### Client
107+
```cs
108+
using SwebSocket;
109+
110+
var listener = new Listener(IPAddress.Any, 3000)
111+
.UseSsl(SslOptions.NoSsl) // No Ssl
112+
.UseSsl(SslOptions.ForServer(identity)); // X509Certificate2 as Identity
113+
114+
// ...
115+
```
116+
#### Server
117+
```cs
118+
using SwebSocket;
119+
120+
var ws = WebSocket
121+
.Connect()
122+
.To("wss://127.0.0.1/")
123+
.UseSsl(SslOptions.ForClient(
124+
"custom authority", // The name to validate the certificate against
125+
caCertificates // X509Certificate2Collection
126+
))
127+
.Build();
128+
129+
// ...
130+
```
131+
132+
## Related Projects
133+
- [WebSocket-Sharp](https://github.com/sta/websocket-sharp)
134+
- [System.Net.WebSockets](https://learn.microsoft.com/en-us/dotnet/api/system.net.websockets)
135+
- [Fleck](https://github.com/statianzo/Fleck)
136+
- [WatsonWebsockte](https://github.com/jchristn/WatsonWebsocket)

SwebSocket.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<TargetFramework>netstandard2.1</TargetFramework>
55
<Nullable>enable</Nullable>
66
<LangVersion>10.0</LangVersion>
7-
<PackageReadmeFile>README.md</PackageReadmeFile>
7+
<PackageReadmeFile>README.nuget.md</PackageReadmeFile>
88
<Title>SwebSocket</Title>
99
<Description>An easy to use WebSocket library.</Description>
1010
<Authors>Luca Schlecker</Authors>
@@ -17,7 +17,7 @@
1717

1818
<ItemGroup>
1919
<PackageReference Include="HttpMachine.PCL" Version="4.0.3" />
20-
<None Include="README.md" Pack="true" PackagePath="/"/>
20+
<None Include="README.nuget.md" Pack="true" PackagePath="/"/>
2121
<None Include="icon.png" Pack="true" PackagePath="/"/>
2222
</ItemGroup>
2323

0 commit comments

Comments
 (0)