Skip to content

Latest commit

 

History

History
46 lines (36 loc) · 1.06 KB

README.md

File metadata and controls

46 lines (36 loc) · 1.06 KB

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);
    }
}