org.springframework.web.socket.handler

## Class PerConnectionWebSocketHandler

- java.lang.Object
- - org.springframework.web.socket.handler.PerConnectionWebSocketHandler

- All Implemented Interfaces:
    Aware, BeanFactoryAware, WebSocketHandler

      public class PerConnectionWebSocketHandler
      extends Object
      implements WebSocketHandler, BeanFactoryAware

  A `WebSocketHandler` that initializes and destroys a `WebSocketHandler` instance for each WebSocket connection and delegates all other methods to it.

  Essentially create an instance of this class once, providing the type of `WebSocketHandler` class to create for each connection, and then pass it to any API method that expects a `WebSocketHandler`.

  If initializing the target `WebSocketHandler` type requires a Spring BeanFctory, then the `setBeanFactory(BeanFactory)` property accordingly. Simply declaring this class as a Spring bean will do that. Otherwise, `WebSocketHandler` instances of the target type will be created using the default constructor.

  Since:
    4.0
  Author:
    Rossen Stoyanchev

- - ### Constructor Summary

    Constructors
    Constructor and Description
    `PerConnectionWebSocketHandler(Class<? extends WebSocketHandler> handlerType)`
    `PerConnectionWebSocketHandler(Class<? extends WebSocketHandler> handlerType, boolean supportsPartialMessages)`

  - ### Method Summary

    All Methods Instance Methods Concrete Methods
    Modifier and Type | Method and Description
    --- | ---
    `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.
    `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.
    `void` | `setBeanFactory(BeanFactory beanFactory)` Callback that supplies the owning factory to a bean instance.
    `boolean` | `supportsPartialMessages()` Whether the WebSocketHandler handles partial messages.
    `String` | `toString()`

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

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

- - ### Constructor Detail

    - #### PerConnectionWebSocketHandler

          public PerConnectionWebSocketHandler(Class<? extends WebSocketHandler> handlerType)

    - #### PerConnectionWebSocketHandler

          public PerConnectionWebSocketHandler(Class<? extends WebSocketHandler> handlerType,
                                               boolean supportsPartialMessages)

  - ### Method Detail

    - #### setBeanFactory

          public void setBeanFactory(BeanFactory beanFactory)
                              throws BeansException

      Description copied from interface: `BeanFactoryAware`
      Callback that supplies the owning factory to a bean instance.

      Invoked after the population of normal bean properties but before an initialization callback such as `InitializingBean.afterPropertiesSet()` or a custom init-method.

      Specified by:
        `setBeanFactory` in interface `BeanFactoryAware`
      Parameters:
        `beanFactory` - owning BeanFactory (never `null`). The bean can immediately call methods on the factory.
      Throws:
        `BeansException` - in case of initialization errors
      See Also:
        `BeanInitializationException`

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

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

    - #### 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`

    - #### toString

          public String toString()

      Overrides:
        `toString` in class `Object`