handleWebSocketMessagesForOptionalProtocol

Description

Handles WebSocket requests with the given handler and rejects other requests with an ExpectedWebSocketRequestRejection.

If the subprotocol parameter is empty any WebSocket request is accepted. If the subprotocol parameter is a non-empty Optional a WebSocket request is only accepted if the list of subprotocols supported by the client (as announced in the WebSocket request) matches the contained subprotocol. If the client did not offer the protocol in question the request is rejected with an UnsupportedWebSocketSubprotocolRejection.

To support several subprotocols you may chain several handleWebSocketMessagesForOptionalProtocol routes.

The handleWebSocketMessagesForOptionalProtocol directive is used as a building block for WebSocket Directives to handle websocket messages.

For more information about the WebSocket support, see Server-Side WebSocket Support.

The source code for this page can be found here.