def handleWebSocketMessagesForOptionalProtocol(handler: Flow[Message, Message, Any], subprotocol: Option[String]): Route


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

If the subprotocol parameter is Noneempty any WebSocket request is accepted. If the subprotocol parameter is Some(protocol)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) contains protocolmatches the contained subprotocol. If the client did not offer the protocol in question the request is rejected with an UnsupportedWebSocketSubprotocolRejectionUnsupportedWebSocketSubprotocolRejection.

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.

Found an error in this documentation? The source code for this page can be found here. Please feel free to edit and contribute a pull request.