handleWebSocketMessagesForOptionalProtocol

Signature

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

Description

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.