Weerly.WebSocketWrapper 1.0.0

dotnet add package Weerly.WebSocketWrapper --version 1.0.0
                    
NuGet\Install-Package Weerly.WebSocketWrapper -Version 1.0.0
                    
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="Weerly.WebSocketWrapper" Version="1.0.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Weerly.WebSocketWrapper" Version="1.0.0" />
                    
Directory.Packages.props
<PackageReference Include="Weerly.WebSocketWrapper" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Weerly.WebSocketWrapper --version 1.0.0
                    
#r "nuget: Weerly.WebSocketWrapper, 1.0.0"
                    
#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.
#:package Weerly.WebSocketWrapper@1.0.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Weerly.WebSocketWrapper&version=1.0.0
                    
Install as a Cake Addin
#tool nuget:?package=Weerly.WebSocketWrapper&version=1.0.0
                    
Install as a Cake Tool

Weerly WebSocket Wrapper

Real-Time WebSocket Wrapper for ASP.NET Core Applications

Overview

Weerly WebSocket Wrapper is a lightweight .NET 6 library designed to simplify WebSocket routing and connection handling in ASP.NET Core applications. This library provides a structured way to define WebSocket routes and manage WebSocket communication efficiently.

This project provides a WebSocket wrapper for simplifying WebSocket communication and routing in .NET applications. It includes a flexible API for mapping WebSocket routes to handlers with both synchronous and asynchronous processing capabilities.

Features

  • Easy integration with ASP.NET Core applications
  • Fluent API for defining WebSocket routes
  • Supports different WebSocket processing strategies
  • Compatible with .NET Core starting from version standard 2.0
  • Dynamic WebSocket Route Mapping: Quickly define WebSocket routes with MapWsRoute and MapWsRouteAsync.
  • Asynchronous and Synchronous Handling: Support both long-running operations and simple message processing.
  • Custom Routing Logic: Routes can point to handler methods in specific classes or controllers.
  • Real-Time Communication: Deliver progressive updates using WebSocketCallback.

Installation

To install Weerly WebSocket Wrapper, add the package to your project:

Install-Package Weerly.WebSocketWrapper

Usage

To start using the WebSocket wrapper, configure routes using app.UseWebSocketRoutes by specifying the mapping method (MapWsRoute or MapWsRouteAsync).

also don`t forget to add app.UseWebSockets(); in your startup.cs or program.cs depending on your .NET version.

Example: Mapping WebSocket Routes

app.UseWebSocketRoutes(routes =>
{
    // Asynchronous WebSocket route
    routes.MapWsRouteAsync(
        name: "default",
        template: "Test/About",
        type: WebSocketEnums.CommonType.Class,
        classNamespace: "WebApp1.Models.TestClass"
    );

    // Synchronous WebSocket route
    routes.MapWsRoute(
        name: "sync",
        template: "Sync/Example",
        type: WebSocketEnums.CommonType.Class,
        classNamespace: "WebApp1.Models.TestClass"
    );
});

WebSocket Handlers

1. MapWsRouteAsync Handler

Asynchronous handlers are used for MapWsRouteAsync. They must have:

  • Two arguments:
    1. string income: The incoming WebSocket message.
    2. WebSocketCallback callback: A callback used to send updates back to the client.
  • Return type: Task (async functions).
Handler Example for MapWsRouteAsync
namespace WebApp1.Models.TestClass;

public class Test
{
    public async Task About(string income, WebSocketCallback callback)
    {
        Console.WriteLine("About method started...");

        // Example: Serialize and process input
        var serializedInput = JsonConvert.SerializeObject(income);
        var formattedInput = JsonConvert.SerializeObject(
            JsonConvert.DeserializeObject<object>(serializedInput), Formatting.None);

        // Send updates to WebSocket client
        for (var i = 0; i < 1000000; i++)
        {
            if (i > 0 && i % 1000 == 0)
            {
                await callback($"{i}"); // Send periodic updates
            }
        }

        Console.WriteLine("About method completed.");
    }
}
Explanation:
  • The handler processes the income message asynchronously.
  • Sends progress updates back to the WebSocket client during long-running operations using callback.

2. MapWsRoute Handler

Synchronous handlers are used for MapWsRoute. They must have:

  • One argument:
    • string income: The incoming WebSocket message.
  • Return type: A single string response.
Handler Example for MapWsRoute
namespace WebApp1.Models.TestClass;

public class Test
{
    public string About(string income)
    {
        Console.WriteLine("Processing About request...");

        // Example: Processing input and returning result
        for (var i = 0; i < 1000000; i++)
        {
            if (i > 0 && i % 1000 == 0)
            {
                return $"{i}"; // Return first result divisible by 1000
            }
        }

        return "Processing completed.";
    }
}
Explanation:
  • The handler processes the income message synchronously and returns a single response.
  • No callback is used since synchronous handlers are one-time, non-progressive operations.

Key Differences Between MapWsRouteAsync and MapWsRoute

Feature MapWsRouteAsync MapWsRoute
Arguments string income, WebSocketCallback callback string income
Response Type Real-time updates via callback Single response string
Use-Case Long-running or interactive processing Simple one-time processing

Full Usage Example

app.UseWebSocketRoutes(routes =>
{
    // Asynchronous WebSocket route example
    routes.MapWsRouteAsync(
        name: "analytics",
        template: "Analytics/Data",
        type: WebSocketEnums.CommonType.Class,
        classNamespace: "WebApp1.Models.TestClass"
    );

    // Synchronous WebSocket route example
    routes.MapWsRoute(
        name: "sync",
        template: "Sync/Example",
        type: WebSocketEnums.CommonType.Class,
        classNamespace: "WebApp1.Models.TestClass"
    );
});

Handlers for the routes:

  • Asynchronous route: Adds progressive updates for long-running WebSocket operations (Test.About with callback).
  • Synchronous route: Adds simple, single-response logic for WebSocket operation (Test.About).

Error Handling

  1. Invalid Namespace:

    • Ensure the specified classNamespace or handler class is correctly provided and exists at runtime.
  2. Callback Misuse (MapWsRouteAsync):

    • Ensure progressive updates in the async handler use await callback() properly.
  3. Wrong Method Signature:

    • Ensure handler method signatures match the route type (Async or Non-Async) and required arguments.

Supported Route Templates

The WebSocket wrapper supports dynamic route configurations with the following templates:

  • Class or Controller Templates: Example: "Controller/Action" or "Class/Method".
  • Custom Defined Routes: Tailor namespace routing to any required structure.

License

This WebSocket wrapper is licensed under the MIT License.

Author

Developed by Mykhailo Chumak.

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.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.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
1.0.0 186 2/16/2025

released library