Skip to content

A library for working with WebSockets in ASP.NET Core

License

Notifications You must be signed in to change notification settings

VoDACode/VoDA.WebSockets

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Stand With Ukraine

nuget

Description

This library provides an easy way to use WebSockets in your ASP.NET project. The usage method is similar to ASP.NET MVC controllers, but for WebSocket.

Quick Start

  • Install the NuGet package into your project.

  • Create a new class that inherits from BaseWebSocketController.

    • You can add a RouteAttribute attribute to your class for custom routing.
  • Add methods to your class that will handle WebSocket events.

Example

[Route("/ws")]
public class MyWebSocketController : BaseWebSocketController
{
    [WebSocketPath("/")]
    public async Task OnOpenAsync()
    {
        await Client.SendAsync("Hello!");
    }

    [WebSocketCyclic]
    [WebSocketPath("echo")]
    public async Task OnMessageAsync(string message)
    {
        await Client.SendAsync(message);
    }

    [WebSocketPath("{id}")]
    public async Task OnCyclicAsync(string id)
    {
        await Client.SendAsync("Hello " + id);
        await Client.SendAsync("Bye " + id);
    }
}