|
| 1 | + |
| 2 | +#  SwebSocket |
| 3 | + |
| 4 | +   |
| 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) |
0 commit comments