Henko.JsonWebSocket 2.1.1

dotnet add package Henko.JsonWebSocket --version 2.1.1
NuGet\Install-Package Henko.JsonWebSocket -Version 2.1.1
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="Henko.JsonWebSocket" Version="2.1.1" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Henko.JsonWebSocket --version 2.1.1
#r "nuget: Henko.JsonWebSocket, 2.1.1"
#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 Henko.JsonWebSocket as a Cake Addin
#addin nuget:?package=Henko.JsonWebSocket&version=2.1.1

// Install Henko.JsonWebSocket as a Cake Tool
#tool nuget:?package=Henko.JsonWebSocket&version=2.1.1

JsonWebSocket.Net

Simple .NET Standard library for sending json and bson messages through WebSocket.

As it is built on the .NET Standard platform it can be used to easily build communication channels between Legacy .NET Framework applications and .NET Core plaform apps or Universal Windows Platform apps. You can also use it to build WebSocket server for web browser based or other clients.

NuGet Package

https://www.nuget.org/packages/Henko.JsonWebSocket/

WebSocket server example usage

You can build simple WebSocket servers with a few lines of code both on the .NET Framework platform and .NET Core platform using Asp.Net Core.

using JsonWebSocket;

public static IWebHost BuildWebHost(string[] args) =>
  WebHost.CreateDefaultBuilder(args)
    .UseWebSockets()
    .Map("/ws", ws => ws.UseJsonWebSockets(socket =>
    {
      Console.WriteLine("[{0}] Client connected.", socket.RemoteEndPoint);
      socket.OnReceivedBinaryBson += async (data) =>
      {
        Console.WriteLine("[{0}] Received binary Bson: {1}", socket.RemoteEndPoint, data);
        await socket.SendBinaryBson(data);
      };
      socket.OnDisconnected += () =>
      {
        Console.WriteLine("[{0}] Client disconnected.", socket.RemoteEndPoint);
      };
    }))
    .UseUrls("http://0.0.0.0:5000/")
    .Build();

WebSocket client example usage

Connect to any WebSocket server from a .Net application easily.

using JsonWebSocket;

static async Task Connect(string url)
{
  using (var socket = new JsonSocketClient())
  {
    socket.OnReceived += (data) =>
    {
      Console.WriteLine("Received: {0} {1}", data.Type, data.Data);
    };
    await socket.Connect(url);
    Console.WriteLine("Connected to {0}.", url);
    await Task.WhenAll(
      socket.StartReceiving(),
      StartSending(socket)
    );
    Console.WriteLine("Disconnected.");
  }
}

static async Task StartSending(JsonSocket socket)
{
    while (socket.Connected)
    {
        dynamic data = new { name = "Sample", time = DateTime.Now };
        await socket.SendBinaryBson(data);
        await Task.Delay(1000);
    }
}

JsonSocket

This class represents the WebSocket connection in both directions. You can use this to send or receive text, json or binary bson data and to get information about the connection.

bool Connected { get; }
HttpContext Context { get; }
WebSocket Socket { get; }
string ConnectionId { get; }
EndPoint RemoteEndPoint { get; }

Task StartReceiving(int bufferSize = 102400);

Task SendTextJson(object data);
Task SendTextString(string data);
Task SendBinaryBson(object data);
Task SendBinaryData(byte[] data);
Task SendClose();

event Action<ReceivedData> OnReceived;
event Action<object> OnReceivedTextJson;
event Action<string> OnReceivedTextString;
event Action<object> OnReceivedBinaryBson;
event Action<byte[]> OnReceivedBinaryData;
event Action OnReceivedClose;
event Action<Exception> OnReceiveError;
event Action OnDisconnected;
Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  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. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
2.1.1 1,390 11/20/2017
2.1.0 999 11/20/2017
2.0.0 1,430 11/19/2017

Fixed bug when received close message.