yawaflua.WebSockets
1.0.1
dotnet add package yawaflua.WebSockets --version 1.0.1
NuGet\Install-Package yawaflua.WebSockets -Version 1.0.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="yawaflua.WebSockets" Version="1.0.1" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="yawaflua.WebSockets" Version="1.0.1" />
<PackageReference Include="yawaflua.WebSockets" />
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 yawaflua.WebSockets --version 1.0.1
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: yawaflua.WebSockets, 1.0.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.
#:package yawaflua.WebSockets@1.0.1
#: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=yawaflua.WebSockets&version=1.0.1
#tool nuget:?package=yawaflua.WebSockets&version=1.0.1
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
New WebSocket routing system
Features
- ASP.NET Core-style WebSocket routing 🛣️
- Method-based endpoint handlers (Like in ASP!) 🎯
- Simple integration with existing applications ⚡
Installation
Add the NuGet package to your project:
dotnet add package yawaflua.WebSockets
Quick Start
1. Create WebSocket Controller
public class ChatController : WebSocketController
{
[WebSocket("/chat")]
public override async Task OnMessageAsync(
IWebSocket webSocket,
HttpContext httpContext)
{
await webSocket.SendAsync("Message!");
}
}
2. Configure Services
public void ConfigureServices(IServiceCollection services)
{
services
.AddControllers()
.SettingUpWebSockets(); // ← Add WebSocket routing
services.AddSingleton<ChatController>();
}
3. Enable Middleware
public void Configure(IApplicationBuilder app)
{
app.ConnectWebSockets(); // ← Add WebSocket handling
app.UseRouting();
app.UseEndpoints(endpoints => endpoints.MapControllers());
}
Advanced Usage
Parameterized Routes
public class NotificationsController : WebSocketController
{
[WebSocket("/notifications/{userId}")]
public override async Task OnMessageAsync(
IWebSocket webSocket,
HttpContext httpContext)
{
var userId = httpContext.Request.RouteValues["userId"];
// Handle user-specific notifications
}
}
Method-Level Routes
[WebSocket("/game")]
public class GameController : WebSocketController
{
[WebSocket("join/{roomId}")]
public async Task JoinRoom(IWebSocket webSocket, HttpContext context)
{
// Handle room joining
}
[WebSocket("leave/{roomId}")]
public async Task LeaveRoom(IWebSocket webSocket, HttpContext context)
{
// Handle room leaving
}
}
Providing dependencies in controller
[WebSocket("/chat")]
public class ChatController : WebSocketController
{
public static DbContext dbContext;
public ChatController(DbContext dbContext)
{
ChatController.dbContext = dbContext;
}
[WebSocket("join/{roomId}")]
public async Task JoinRoom(WebSocket webSocket, HttpContext context)
{
await dbContext.Add(...);
// Next your logic etc
}
[WebSocket("leave/{roomId}")]
public async Task LeaveRoom(WebSocket webSocket, HttpContext context)
{
// Handle room leaving
}
}
Run any code on connection to WebSocket
services.AddSingleton(new WebSocketConfig()
{
OnOpenHandler = async (socket, context) =>
{
if (socket.WebSocketManager!.GetAllClients().Count(k =>
Equals(k.ConnectionInfo!.RemoteIpAddress!.MapToIPv4(),
socket.Client.ConnectionInfo!.RemoteIpAddress!.MapToIPv4())) >= 3)
{
await socket.CloseAsync(WebSocketCloseStatus.NormalClosure, "Too many users");
}
Console.WriteLine($"{socket.Client.Id} has been connected to {socket.Client.Path}");
}
})
Lifecycle Management
- Connection - Automatically handled by middleware
- Message Handling - Implement
OnMessageAsync
- Cleanup - Dispose resources in
IDisposable
interface
Best Practices
- Keep Controllers Light - Move business logic to services
- Use Dependency Injection - Inject services via constructor
- Handle Exceptions - Wrap operations in try/catch blocks
- Manage State - Use
HttpContext.Items
for request-scoped data
Troubleshooting
No Route Handling?
- Verify controller registration in DI:
services.AddSingleton<YourController>();
Connection Issues?
- Ensure middleware order:
app.ConnectWebSockets(); // Must be before UseRouting/UseEndpoints
Parameters Not Working?
- Check route template syntax:
[WebSocket("/correct/{paramName}")] // ✓ [WebSocket("/wrong/{param-name}")] // ✗
License
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net6.0 is compatible. 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 is compatible. 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 is compatible. 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 is compatible. 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. |
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
-
net6.0
- JetBrains.Annotations (> 2023.3.0)
- Microsoft.AspNetCore.WebSockets (> 2.1.7)
- Microsoft.Extensions.Hosting.Abstractions (> 6.0.0)
- System.Net.WebSockets (> 4.0.0)
-
net7.0
- JetBrains.Annotations (> 2023.3.0)
- Microsoft.AspNetCore.WebSockets (> 2.1.7)
- Microsoft.Extensions.Hosting.Abstractions (> 6.0.0)
- System.Net.WebSockets (> 4.0.0)
-
net8.0
- JetBrains.Annotations (> 2023.3.0)
- Microsoft.AspNetCore.WebSockets (> 2.1.7)
- Microsoft.Extensions.Hosting.Abstractions (> 6.0.0)
- System.Net.WebSockets (> 4.0.0)
-
net9.0
- JetBrains.Annotations (> 2023.3.0)
- Microsoft.AspNetCore.WebSockets (> 2.1.7)
- Microsoft.Extensions.Hosting.Abstractions (> 6.0.0)
- System.Net.WebSockets (> 4.0.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.