ByronAP.Net.WebSockets 0.7.7

There is a newer version of this package available.
See the version list below for details.
dotnet add package ByronAP.Net.WebSockets --version 0.7.7
NuGet\Install-Package ByronAP.Net.WebSockets -Version 0.7.7
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="ByronAP.Net.WebSockets" Version="0.7.7" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add ByronAP.Net.WebSockets --version 0.7.7
#r "nuget: ByronAP.Net.WebSockets, 0.7.7"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install ByronAP.Net.WebSockets as a Cake Addin
#addin nuget:?package=ByronAP.Net.WebSockets&version=0.7.7

// Install ByronAP.Net.WebSockets as a Cake Tool
#tool nuget:?package=ByronAP.Net.WebSockets&version=0.7.7

ByronAP.Net.WebSockets

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

Usage

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

        /// <summary>
        /// This gets called when the state of the connection changes
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="newWebSocketState"></param>
        /// <param name="oldWebSocketState"></param>
        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}");
        }

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

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

License

MIT

Product Compatible and additional computed target framework versions.
.NET net5.0 is compatible.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on ByronAP.Net.WebSockets:

Package Downloads
CoinCapApi

CoinCap API Client Library

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
0.8.1.1 966 5/16/2023
0.8.0.1 1,356 12/15/2022
0.7.7 932 8/23/2021
0.7.6 956 7/6/2021
0.7.5 877 7/6/2021