Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/cristipufu/ninja-websocket-net
Lightweight, user-friendly WebSocket APIs for .NET
https://github.com/cristipufu/ninja-websocket-net
autoreconnect csharp discord dotnet infura keepalive websocket websocket-client websockets
Last synced: 28 days ago
JSON representation
Lightweight, user-friendly WebSocket APIs for .NET
- Host: GitHub
- URL: https://github.com/cristipufu/ninja-websocket-net
- Owner: cristipufu
- License: mit
- Created: 2022-02-04T19:49:56.000Z (almost 3 years ago)
- Default Branch: master
- Last Pushed: 2023-11-20T06:48:35.000Z (12 months ago)
- Last Synced: 2024-10-04T15:46:59.647Z (about 1 month ago)
- Topics: autoreconnect, csharp, discord, dotnet, infura, keepalive, websocket, websocket-client, websockets
- Language: C#
- Homepage:
- Size: 48.8 KB
- Stars: 9
- Watchers: 3
- Forks: 3
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Ninja WebSocket
[![NuGet](https://img.shields.io/nuget/v/Ninja.WebSocketClient)](https://www.nuget.org/packages/Ninja.WebSocketClient)
[![GitHub](https://img.shields.io/github/license/ninjastacktech/ninja-websocket-net)](https://github.com/ninjastacktech/ninja-websocket-net/blob/master/LICENSE)NinjaWebSocket is an easy-to-use .NET6 WebSocket client with auto-reconnect and keep-alive capabilities.
Lightweight library, user-friendly API, inspired by the javascript WebSocket API and SignalR.
## snippets
The repo also contains two usage examples:
- [Connecting a bot to a Discord websocket channel](https://github.com/ninjastacktech/ninja-websocket-net/blob/master/test/Ninja.WebSocket.DemoConsole/DiscordWebSocketClient.cs)
- [Subscribing to Infura/Ethereum transaction events](https://github.com/ninjastacktech/ninja-websocket-net/blob/master/test/Ninja.WebSocket.DemoConsole/EthereumWebSocketClient.cs)Basic usage:
```C#
var ws = new NinjaWebSocket("")
.SetOptions(options => options.SetRequestHeader("X-Ninja-WebSocket", "hello world"))
.SetKeepAlive(keepAliveIntervalSeconds: 5)
.SetAutomaticReconnect(autoReconnectIntervalSeconds: 5);ws.OnConnected += async () =>
{
// Notify users the connection was established.
Console.WriteLine("Connected.");// Send messages
await ws.SendAsync("hello world!");
};ws.OnReceived += data =>
{
Console.WriteLine(Encoding.UTF8.GetString(data!.Value.ToArray()));return Task.CompletedTask;
};ws.OnKeepAlive += () =>
{
Console.WriteLine("Ping.");return Task.CompletedTask;
};ws.OnReconnecting += (ex) =>
{
// Notify users the connection was lost and the client is reconnecting.
Console.WriteLine($"Reconnecting: {ex?.Message}");return Task.CompletedTask;
};ws.OnClosed += (ex) =>
{
// Notify users the connection has been closed.
Console.WriteLine($"Closed: {ex?.Message}");return Task.CompletedTask;
};// Connect to the ws and start listening
await ws.StartAsync();```
---
### MIT License