An open API service indexing awesome lists of open source software.

https://github.com/byronap/websockets

.NET library that simplifies the usage of client websockets
https://github.com/byronap/websockets

dotnet dotnet-core netstandard websocket websockets

Last synced: 3 months ago
JSON representation

.NET library that simplifies the usage of client websockets

Awesome Lists containing this project

README

        

# ByronAP.Net.WebSockets

ByronAP.Net.WebSockets is a .NET library that simplifies the usage of client websockets.

[![Sponsor](https://img.shields.io/static/v1?label=Sponsor&message=%E2%9D%A4&logo=GitHub&color=%23fe8e86)](https://github.com/sponsors/ByronAP)
[![NuGet version (SoftCircuits.Silk)](https://img.shields.io/nuget/v/ByronAP.Net.WebSockets.svg?style=flat-square)](https://www.nuget.org/packages/ByronAP.Net.WebSockets/)
[![FOSSA Status](https://app.fossa.com/api/projects/git%2Bgithub.com%2FByronAP%2Fwebsockets.svg?type=shield)](https://app.fossa.com/projects/git%2Bgithub.com%2FByronAP%2Fwebsockets?ref=badge_shield)

## Usage

```c#
using System;
using System.Threading.Tasks;

namespace WebSocketUsageDemo
{
class Program
{
const string WebSocketHost = "wss://echo.websocket.org";
const string TestMessage = "Hi, this is a websocket text message.";

static async Task Main()
{
// Create an instance of the WebSocketOptions class
var options = new ByronAP.Net.WebSockets.WebSocketOptions(WebSocketHost);
// setup options as needed

// create a client instance and make sure it is automatically disposed of by a using statement
using var client = new ByronAP.Net.WebSockets.WebSocketClient(options);

// hookup to the events we want to receive
client.ConnectionStateChanged += Client_ConnectionStateChanged;
client.MessageReceived += Client_MessageReceived;
client.DataReceived += Client_DataReceived;

// start the connection and ensure it connected sucessfully
var connResult = await client.ConnectAsync();
if(!connResult.Item1)
{
// connection failed
Console.WriteLine($"{DateTime.Now} ERROR: {connResult.Item2}");
await Task.Delay(2000);
return;
}

// send our test message
Console.WriteLine($"{DateTime.Now} Message Sent: {TestMessage}");
await client.SendTextAsync(TestMessage);

// wait a bit before exiting the app
await Task.Delay(2000);
}

///
/// This gets called when the state of the connection changes
///
///
///
///
private static void Client_ConnectionStateChanged(object sender, System.Net.WebSockets.WebSocketState newWebSocketState, System.Net.WebSockets.WebSocketState oldWebSocketState)
{
Console.WriteLine($"{DateTime.Now} State Changed: from {oldWebSocketState} to {newWebSocketState}");
}

///
/// This gets called when a text message is received
///
///
///
private static void Client_MessageReceived(object sender, string message)
{
Console.WriteLine($"{DateTime.Now} Message Received: {message}");
}

///
/// This gets called when binary data is received
///
///
///
private static void Client_DataReceived(object sender, byte[] data)
{
Console.WriteLine($"{DateTime.Now} Data Received: {data.Length} bytes");
}
}
}
```

## Contributing
Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

## License
[MIT](https://choosealicense.com/licenses/mit/)