From 88be785c7c9cddbff88a739a935c68a8c33cf39f Mon Sep 17 00:00:00 2001 From: Wade Pickett Date: Mon, 26 Aug 2024 15:35:14 -0700 Subject: [PATCH] What's New: Keep-Alive Timeout for WebWSockets:Draft include (#33445) Moving as is to include as first pre-work step. --- .../includes/websockets-keep-alive-timeout | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 aspnetcore/release-notes/aspnetcore-9/includes/websockets-keep-alive-timeout diff --git a/aspnetcore/release-notes/aspnetcore-9/includes/websockets-keep-alive-timeout b/aspnetcore/release-notes/aspnetcore-9/includes/websockets-keep-alive-timeout new file mode 100644 index 0000000000..1bd5564cf2 --- /dev/null +++ b/aspnetcore/release-notes/aspnetcore-9/includes/websockets-keep-alive-timeout @@ -0,0 +1,21 @@ +### Keep-Alive Timeout for WebSockets + +The [WebSockets middleware](https://learn.microsoft.com/aspnet/core/fundamentals/websockets#configure-the-middleware) can now be configured for keep alive timeouts. + +The keep alive timeout will abort the WebSocket and throw from `WebSocket.ReceiveAsync` if a ping frame from the websocket protocol is sent by the server and the client doesn't reply with a pong frame within the specified timeout. The ping frame is automatically sent by the server and configured with `KeepAliveInterval`. This option is useful when wanting to detect connections that might be slow or ungracefully disconnected. + +The keep alive timeout can be configured globally for the WebSocket middleware: +```csharp +app.UseWebSockets(new WebSocketOptions { KeepAliveInterval = TimeSpan.FromSeconds(15) }); +``` + +Or configured per accepted WebSocket: +```csharp +app.Run(async (context) => +{ + using var webSocket = await context.WebSockets.AcceptWebSocketAsync( + new WebSocketAcceptContext { KeepAliveTimeout = TimeSpan.FromSeconds(15) }); + + // ... +} +```