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
- Host: GitHub
- URL: https://github.com/byronap/websockets
- Owner: ByronAP
- License: mit
- Created: 2021-07-06T00:19:22.000Z (almost 4 years ago)
- Default Branch: dev
- Last Pushed: 2023-05-16T23:53:20.000Z (about 2 years ago)
- Last Synced: 2025-02-14T23:04:51.512Z (3 months ago)
- Topics: dotnet, dotnet-core, netstandard, websocket, websockets
- Language: C#
- Homepage:
- Size: 115 KB
- Stars: 0
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Funding: .github/FUNDING.yml
- License: LICENSE.md
Awesome Lists containing this project
README
# ByronAP.Net.WebSockets
ByronAP.Net.WebSockets is a .NET library that simplifies the usage of client websockets.
[](https://github.com/sponsors/ByronAP)
[](https://www.nuget.org/packages/ByronAP.Net.WebSockets/)
[](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/)