Skip to content

Antelcat/WebSocket

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

WebSocket

.NET wrap for WebSocket

Example

in Program.cs

var builder = WebApplication.CreateBuilder(args);

...
builder.Services.AddWebSockets();
...
    
var app = builder.Build();
...
// remember to call UseWebSockets() before MapWebSocket()
app.UseWebSockets(); 
app.MapWebSocket<SampleWebSocketClient>("/WebSocket");
...

for SampleWebSocketClient

which lifetime is Transient or Scoped, each WebSocket connection will create corresponding instance

// define your client model to receive message from WebSocket client 
public class SampleWebSocketClient : Antelcat.AspNetCore.Client
{
    protected override async Task OnConnectedAsync()
    {
        Console.WriteLine($"{Context.ConnectionId} connected");
    }

    protected override async Task OnDisconnectedAsync(Exception? exception)
    {
        Console.WriteLine($"{Context.ConnectionId} disconnected due to {exception}");
    }

    protected override async Task OnReceivedTextAsync(string text, CancellationToken token)
    {
        await this.SendAsync(text, token);
    }
}

Releases

No releases published

Packages

No packages published