Skip to content

A cool and easy to use WebSocket client based on ClientWebSocket.

License

Notifications You must be signed in to change notification settings

realcoloride/CoolWebSocketClient

Repository files navigation

CoolWebSocketClient

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.

Installation

You can install via the nuget package or via the following command:

dotnet add package CoolWebSocketClient

Usage

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");

Extra

🚀 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.

About

A cool and easy to use WebSocket client based on ClientWebSocket.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages