org.springframework.web.socket.messaging

## Class SubProtocolWebSocketHandler

- java.lang.Object
- - org.springframework.web.socket.messaging.SubProtocolWebSocketHandler

- All Implemented Interfaces:
    Aware, ApplicationEventPublisherAware, Lifecycle, Phased, SmartLifecycle, MessageHandler, SubProtocolCapable, WebSocketHandler

      public class SubProtocolWebSocketHandler
      extends Object
      implements WebSocketHandler, SubProtocolCapable, MessageHandler, SmartLifecycle, ApplicationEventPublisherAware

  An implementation of `WebSocketHandler` that delegates incoming WebSocket messages to a `SubProtocolHandler` along with a `MessageChannel` to which the sub-protocol handler can send messages from WebSocket clients to the application.

  Also an implementation of `MessageHandler` that finds the WebSocket session associated with the `Message` and passes it, along with the message, to the sub-protocol handler to send messages from the application back to the client.

  Since:
    4.0
  Author:
    Rossen Stoyanchev, Andy Wilkinson

- - ### Constructor Summary

    Constructors
    Constructor and Description
    `SubProtocolWebSocketHandler(MessageChannel clientInboundChannel, SubscribableChannel clientOutboundChannel)`

  - ### Method Summary

    All Methods Instance Methods Concrete Methods
    Modifier and Type | Method and Description
    --- | ---
    `void` | `addProtocolHandler(SubProtocolHandler handler)` Register a sub-protocol handler.
    `void` | `afterConnectionClosed(WebSocketSession session, CloseStatus closeStatus)` Invoked after the WebSocket connection has been closed by either side, or after a transport error has occurred.
    `void` | `afterConnectionEstablished(WebSocketSession session)` Invoked after WebSocket negotiation has succeeded and the WebSocket connection is opened and ready for use.
    `protected SubProtocolHandler` | `findProtocolHandler(WebSocketSession session)`
    `SubProtocolHandler` | `getDefaultProtocolHandler()`
    `int` | `getPhase()` Return the phase value of this object.
    `Map<String,SubProtocolHandler>` | `getProtocolHandlerMap()` Return the sub-protocols keyed by protocol name.
    `List<SubProtocolHandler>` | `getProtocolHandlers()`
    `int` | `getSendBufferSizeLimit()`
    `int` | `getSendTimeLimit()`
    `List<String>` | `getSubProtocols()` Return all supported protocols.
    `void` | `handleMessage(Message<?> message)` Handle the given message.
    `void` | `handleMessage(WebSocketSession session, WebSocketMessage<?> message)` Invoked when a new WebSocket message arrives.
    `void` | `handleTransportError(WebSocketSession session, Throwable exception)` Handle an error from the underlying WebSocket message transport.
    `boolean` | `isAutoStartup()` Return whether this Lifecycle component should be started automatically by the container when the ApplicationContext is refreshed.
    `boolean` | `isRunning()` Check whether this component is currently running.
    `void` | `setApplicationEventPublisher(ApplicationEventPublisher eventPublisher)` Set the ApplicationEventPublisher that this object runs in.
    `void` | `setDefaultProtocolHandler(SubProtocolHandler defaultProtocolHandler)` Set the `SubProtocolHandler` to use when the client did not request a sub-protocol.
    `void` | `setProtocolHandlers(List<SubProtocolHandler> protocolHandlers)` Configure one or more handlers to use depending on the sub-protocol requested by the client in the WebSocket handshake request.
    `void` | `setSendBufferSizeLimit(int sendBufferSizeLimit)`
    `void` | `setSendTimeLimit(int sendTimeLimit)`
    `void` | `start()` Start this component.
    `void` | `stop()` Stop this component, typically in a synchronous fashion, such that the component is fully stopped upon return of this method.
    `void` | `stop(Runnable callback)` Indicates that a Lifecycle component must stop if it is currently running.
    `boolean` | `supportsPartialMessages()` Whether the WebSocketHandler handles partial messages.

    - ### Methods inherited from class java.lang.Object

      `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- - ### Constructor Detail

    - #### SubProtocolWebSocketHandler

          public SubProtocolWebSocketHandler(MessageChannel clientInboundChannel,
                                             SubscribableChannel clientOutboundChannel)

  - ### Method Detail

    - #### setProtocolHandlers

          public void setProtocolHandlers(List<SubProtocolHandler> protocolHandlers)

      Configure one or more handlers to use depending on the sub-protocol requested by the client in the WebSocket handshake request.
      Parameters:
        `protocolHandlers` - the sub-protocol handlers to use

    - #### getProtocolHandlers

          public List<SubProtocolHandler> getProtocolHandlers()

    - #### addProtocolHandler

          public void addProtocolHandler(SubProtocolHandler handler)

      Register a sub-protocol handler.

    - #### getProtocolHandlerMap

          public Map<String,SubProtocolHandler> getProtocolHandlerMap()

      Return the sub-protocols keyed by protocol name.

    - #### setDefaultProtocolHandler

          public void setDefaultProtocolHandler(SubProtocolHandler defaultProtocolHandler)

      Set the `SubProtocolHandler` to use when the client did not request a sub-protocol.
      Parameters:
        `defaultProtocolHandler` - the default handler

    - #### getDefaultProtocolHandler

          public SubProtocolHandler getDefaultProtocolHandler()

      Returns:
        the default sub-protocol handler to use

    - #### getSubProtocols

          public List<String> getSubProtocols()

      Return all supported protocols.
      Specified by:
        `getSubProtocols` in interface `SubProtocolCapable`

    - #### setSendTimeLimit

          public void setSendTimeLimit(int sendTimeLimit)

    - #### getSendTimeLimit

          public int getSendTimeLimit()

    - #### setSendBufferSizeLimit

          public void setSendBufferSizeLimit(int sendBufferSizeLimit)

    - #### getSendBufferSizeLimit

          public int getSendBufferSizeLimit()

    - #### setApplicationEventPublisher

          public void setApplicationEventPublisher(ApplicationEventPublisher eventPublisher)

      Description copied from interface: `ApplicationEventPublisherAware`
      Set the ApplicationEventPublisher that this object runs in.

      Invoked after population of normal bean properties but before an init callback like InitializingBean's afterPropertiesSet or a custom init-method. Invoked before ApplicationContextAware's setApplicationContext.

      Specified by:
        `setApplicationEventPublisher` in interface `ApplicationEventPublisherAware`
      Parameters:
        `eventPublisher` - event publisher to be used by this object

    - #### isAutoStartup

          public boolean isAutoStartup()

      Description copied from interface: `SmartLifecycle`
      Return whether this Lifecycle component should be started automatically by the container when the ApplicationContext is refreshed. A value of "false" indicates that the component is intended to be started manually.
      Specified by:
        `isAutoStartup` in interface `SmartLifecycle`

    - #### getPhase

          public int getPhase()

      Description copied from interface: `Phased`
      Return the phase value of this object.
      Specified by:
        `getPhase` in interface `Phased`

    - #### isRunning

          public final boolean isRunning()

      Description copied from interface: `Lifecycle`
      Check whether this component is currently running.

      In the case of a container, this will return `true` only if all components that apply are currently running.

      Specified by:
        `isRunning` in interface `Lifecycle`
      Returns:
        whether the component is currently running

    - #### start

          public final void start()

      Description copied from interface: `Lifecycle`
      Start this component. Should not throw an exception if the component is already running.

      In the case of a container, this will propagate the start signal to all components that apply.

      Specified by:
        `start` in interface `Lifecycle`

    - #### stop

          public final void stop()

      Description copied from interface: `Lifecycle`
      Stop this component, typically in a synchronous fashion, such that the component is fully stopped upon return of this method. Consider implementing `SmartLifecycle` and its `stop(Runnable)` variant in cases where asynchronous stop behavior is necessary.

      Should not throw an exception if the component isn't started yet.

      In the case of a container, this will propagate the stop signal to all components that apply.

      Specified by:
        `stop` in interface `Lifecycle`
      See Also:
        `SmartLifecycle.stop(Runnable)`

    - #### stop

          public final void stop(Runnable callback)

      Description copied from interface: `SmartLifecycle`
      Indicates that a Lifecycle component must stop if it is currently running.

      The provided callback is used by the `LifecycleProcessor` to support an ordered, and potentially concurrent, shutdown of all components having a common shutdown order value. The callback must be executed after the SmartLifecycle component does indeed stop.

      The `LifecycleProcessor` will call only this variant of the `stop` method; i.e. `Lifecycle.stop()` will not be called for `SmartLifecycle` implementations unless explicitly delegated to within this method.

      Specified by:
        `stop` in interface `SmartLifecycle`

    - #### afterConnectionEstablished

          public void afterConnectionEstablished(WebSocketSession session)
                                          throws Exception

      Description copied from interface: `WebSocketHandler`
      Invoked after WebSocket negotiation has succeeded and the WebSocket connection is opened and ready for use.
      Specified by:
        `afterConnectionEstablished` in interface `WebSocketHandler`
      Throws:
        `Exception` - this method can handle or propagate exceptions; see class-level Javadoc for details.

    - #### findProtocolHandler

          protected final SubProtocolHandler findProtocolHandler(WebSocketSession session)

    - #### handleMessage

          public void handleMessage(WebSocketSession session,
                                    WebSocketMessage<?> message)
                             throws Exception

      Description copied from interface: `WebSocketHandler`
      Invoked when a new WebSocket message arrives.
      Specified by:
        `handleMessage` in interface `WebSocketHandler`
      Throws:
        `Exception` - this method can handle or propagate exceptions; see class-level Javadoc for details.

    - #### handleMessage

          public void handleMessage(Message<?> message)
                             throws MessagingException

      Description copied from interface: `MessageHandler`
      Handle the given message.
      Specified by:
        `handleMessage` in interface `MessageHandler`
      Parameters:
        `message` - the message to be handled
      Throws:
        `MessagingException`

    - #### handleTransportError

          public void handleTransportError(WebSocketSession session,
                                           Throwable exception)
                                    throws Exception

      Description copied from interface: `WebSocketHandler`
      Handle an error from the underlying WebSocket message transport.
      Specified by:
        `handleTransportError` in interface `WebSocketHandler`
      Throws:
        `Exception` - this method can handle or propagate exceptions; see class-level Javadoc for details.

    - #### afterConnectionClosed

          public void afterConnectionClosed(WebSocketSession session,
                                            CloseStatus closeStatus)
                                     throws Exception

      Description copied from interface: `WebSocketHandler`
      Invoked after the WebSocket connection has been closed by either side, or after a transport error has occurred. Although the session may technically still be open, depending on the underlying implementation, sending messages at this point is discouraged and most likely will not succeed.
      Specified by:
        `afterConnectionClosed` in interface `WebSocketHandler`
      Throws:
        `Exception` - this method can handle or propagate exceptions; see class-level Javadoc for details.

    - #### supportsPartialMessages

          public boolean supportsPartialMessages()

      Description copied from interface: `WebSocketHandler`
      Whether the WebSocketHandler handles partial messages. If this flag is set to `true` and the underlying WebSocket server supports partial messages, then a large WebSocket message, or one of an unknown size may be split and maybe received over multiple calls to `WebSocketHandler.handleMessage(WebSocketSession, WebSocketMessage)`. The flag `WebSocketMessage.isLast()` indicates if the message is partial and whether it is the last part.
      Specified by:
        `supportsPartialMessages` in interface `WebSocketHandler`