Weerly.WebSocketWrapper
1.0.0
dotnet add package Weerly.WebSocketWrapper --version 1.0.0
NuGet\Install-Package Weerly.WebSocketWrapper -Version 1.0.0
<PackageReference Include="Weerly.WebSocketWrapper" Version="1.0.0" />
<PackageVersion Include="Weerly.WebSocketWrapper" Version="1.0.0" />
<PackageReference Include="Weerly.WebSocketWrapper" />
paket add Weerly.WebSocketWrapper --version 1.0.0
#r "nuget: Weerly.WebSocketWrapper, 1.0.0"
#:package Weerly.WebSocketWrapper@1.0.0
#addin nuget:?package=Weerly.WebSocketWrapper&version=1.0.0
#tool nuget:?package=Weerly.WebSocketWrapper&version=1.0.0
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
MapWsRouteandMapWsRouteAsync. - 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:
string income: The incoming WebSocket message.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
incomemessage 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
stringresponse.
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
incomemessage synchronously and returns a single response. - No
callbackis 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.Aboutwithcallback). - Synchronous route: Adds simple, single-response logic for WebSocket operation (
Test.About).
Error Handling
Invalid Namespace:
- Ensure the specified
classNamespaceor handler class is correctly provided and exists at runtime.
- Ensure the specified
Callback Misuse (
MapWsRouteAsync):- Ensure progressive updates in the async handler use
await callback()properly.
- Ensure progressive updates in the async handler use
Wrong Method Signature:
- Ensure handler method signatures match the route type (
AsyncorNon-Async) and required arguments.
- Ensure handler method signatures match the route type (
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 | Versions 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. |
-
.NETStandard 2.0
- Microsoft.AspNetCore.Http.Abstractions (>= 2.1.1)
- Newtonsoft.Json (>= 13.0.1)
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