+1

WebSockets in ASP.NET Core: How to Use Them

Introduction

WebSockets enable real-time full-duplex communication between web servers and clients. This allows building features like chat apps, multiplayer games and notifications in web applications. ASP.NET Core provides support for using WebSockets to add real-time capabilities to web apps. In this blog post, we will learn how to setup and use WebSockets in ASP.NET Core applications.

Setting Up a WebSocket Endpoint The first step is to define a WebSocket endpoint in our ASP.NET Core application. This involves creating a class that derives from WebSocketEndpoint and overrides the OnConnectedAsync method.

We can create a ChatEndpoint class like below:

public class ChatEndpoint : WebSocketEndpoint { public override async Task OnConnectedAsync(WebSocketConnection connection) { // code to handle new connections } }

To register this endpoint, we need to add it to the endpoint mappings in Startup.cs:

app.UseEndpoints(endpoints => { endpoints.MapWebSocketEndpoint("/chat", typeof(ChatEndpoint)); });

This maps the /chat URL to our ChatEndpoint class. When a WebSocket connection is established at this URL, the OnConnectedAsync method will be called to handle the connection.

Handling Connections and Messages In the OnConnectedAsync method, we can add the connection to a static list to keep track of all active connections:

private static List<WebSocketConnection> Connections = new List<WebSocketConnection>();

public override async Task OnConnectedAsync(WebSocketConnection connection) { Connections.Add(connection);

// logic to handle connection }

**We also need to override OnDisconnectedAsync to remove connections**:

public override async Task OnDisconnectedAsync(WebSocketConnection connection, WebSocketCloseStatus closeStatus) 

{ Connections.Remove(connection); }

**To receive and send messages, we can use the ReceiveAsync and SendAsync methods on the connection:**

public override async Task OnMessageAsync(WebSocketMessage message, WebSocketConnection connection)

{ var receiveContent = JsonSerializer.Deserialize<Message>(message.Text);

// broadcast message to all other connections await BroadcastMessage(receiveContent); }

private async Task BroadcastMessage(Message message) { foreach (var conn in Connections) { if (conn != connection) await conn.SendAsync(message.ToString(), WebSocketMessageType.Text); } }

This broadcasts any received message to all other active connections after deserializing it.

Sending Periodic Messages

We can also periodically send messages to clients without explicitly receiving one. For example, to broadcast a heartbeat message every 30 seconds:

public async Task Heartbeat() 

{ while(true) { await BroadcastMessage(new Message() { Type = "heartbeat" }); await Task.Delay(30000); } }

public override async Task OnConnectedAsync(WebSocketConnection connection) { // existing code

await Heartbeat(); }

This ensures clients stay connected by sending periodic messages from the server.

Integrating with ASP.NET Core

To integrate WebSockets with ASP.NET Core, we need to register our endpoint class with dependency injection:

services.AddSingleton<IHttpConnectionDispatcher,ConnectionDispatcher>();

services.AddSingleton<ChatEndpoint>();

We also need to enable WebSocket support in the middleware pipeline:

app.UseWebSockets();

app.UseEndpoints(endpoints => { endpoints.MapWebSocketEndpoint("/chat", typeof(ChatEndpoint)); });

Now our WebSocket endpoint will be resolved using dependency injection and plugged into the WebSocket handling pipeline.

Handling Errors Any errors occurred during WebSocket operations like sending/receiving messages need to be gracefully handled to maintain connectivity.

We can catch errors inside OnConnectedAsync, OnMessageAsync etc and log them:

public override async Task OnMessageAsync(WebSocketMessage message, WebSocketConnection connection)

{ try { // existing message handling logic } catch (Exception ex) { logger.LogError(ex, "Error processing WebSocket message"); } }

It's also important to catch errors in background tasks like heartbeats to prevent the app from crashing:

public async Task Heartbeat()

{ try { // existing heartbeat logic } catch (Exception ex) { logger.LogError(ex, "Error sending heartbeat");
} }

Handled errors will ensure the WebSocket connection stays open even if message processing fails temporarily.

Conclusion

In this blog post, we covered how to setup and use WebSockets in ASP.NET Core applications. We looked at defining endpoints, handling connections, sending/receiving messages, periodic messages and error handling. WebSockets enable building real-time features in web apps. Combined with endpoint routing and dependency injection, ASP.NET Core makes WebSockets very easy to integrate into existing projects.

For Fore Information Visit : https://positiwise.com/blog/websockets-in-asp-net-core


All rights reserved

Viblo
Hãy đăng ký một tài khoản Viblo để nhận được nhiều bài viết thú vị hơn.
Đăng kí