Skip to content

Commit 88be785

Browse files
authored
What's New: Keep-Alive Timeout for WebWSockets:Draft include (#33445)
Moving as is to include as first pre-work step.
1 parent c7b835d commit 88be785

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
### Keep-Alive Timeout for WebSockets
2+
3+
The [WebSockets middleware](https://learn.microsoft.com/aspnet/core/fundamentals/websockets#configure-the-middleware) can now be configured for keep alive timeouts.
4+
5+
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.
6+
7+
The keep alive timeout can be configured globally for the WebSocket middleware:
8+
```csharp
9+
app.UseWebSockets(new WebSocketOptions { KeepAliveInterval = TimeSpan.FromSeconds(15) });
10+
```
11+
12+
Or configured per accepted WebSocket:
13+
```csharp
14+
app.Run(async (context) =>
15+
{
16+
using var webSocket = await context.WebSockets.AcceptWebSocketAsync(
17+
new WebSocketAcceptContext { KeepAliveTimeout = TimeSpan.FromSeconds(15) });
18+
19+
// ...
20+
}
21+
```

0 commit comments

Comments
 (0)