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 ExpectedWebSocketRequestRejection.

If the subprotocol parameter is None any WebSocket request is accepted. If the subprotocol parameter is Some(protocol) a WebSocket request is only accepted if the list of subprotocols supported by the client (as announced in the WebSocket request) contains protocol. 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.