CoolWebSocket is a 0 bullshit and straightforward ClientWebSocket wrapper that aims at making WebSockets easy and friendly to use, whilst getting the advantages from ClientWebSocket.
It is lightweight and simple and does not rely on any extra libraries. The codebase actually fits in one file.
You can install via the nuget package or via the following command:
dotnet add package CoolWebSocketClient
Basic usage
var ws = new CoolWebSocket();
await ws.Open(new Uri("wss://echo.websocket.org"));
// when connected
ws.OnOpen += () =>
{
Console.WriteLine("Connected to server");
};
// when a message is received, use messagetype to check if its binary or a text
// and use ReadString() to parse if it is the case.
ws.OnMessage += (messageType, message) =>
{
Console.WriteLine("Received the following message: " + ws.ReadString(message));
};
// when disconnected or kicked
ws.OnClose += (closeStatus, closeMessage) => {
Console.WriteLine($"Disconnected from server ({closeStatus}): {closeMessage}");
};
// send a string message, binary messages can also be sent
await ws.Send("Hello world!");
...
// close the websocket when done and disconnect (you can also specify a code and reason)
await ws.Close();
Changing options (headers, security and more)
// ClientWebSocket options are available here
ws.Options...
// example
ws.Options.SetRequestHeader("x-hello-world", "coolwebsocketclient");
🚀 If you have an issue or idea, let me know in the Issues section.
☕ Want to support me? You can send me a coffee on ko.fi: https://ko-fi.com/coloride.
(real)coloride - 2024, Licensed MIT.