Back to Aspnetcore

Websockets Keep Alive Timeout

aspnetcore/release-notes/aspnetcore-9/includes/websockets-keep-alive-timeout.md

latest1.1 KB
Original Source

Keep-Alive Timeout for WebSockets

The WebSockets middleware can now be configured for keep-alive timeouts.

The keep-alive timeout aborts the WebSocket connection and throws an exception from WebSocket.ReceiveAsync if both of the following conditions are met:

  • The server sends a ping frame using the websocket protocol.
  • The client doesn't reply with a pong frame within the specified timeout.

The server automatically sends the ping frame and configures it with KeepAliveInterval.

The keep-alive timeout setting is useful for detecting connections that might be slow or ungracefully disconnected.

The keep-alive timeout can be configured globally for the WebSocket middleware:

[!code-csharp]

Or configured per accepted WebSocket:

[!code-csharp]