Class WebSocketTransportHandler

java.lang.Object
org.springframework.web.socket.sockjs.transport.handler.AbstractTransportHandler
org.springframework.web.socket.sockjs.transport.handler.WebSocketTransportHandler
All Implemented Interfaces:
Aware, Lifecycle, ServletContextAware, HandshakeHandler, SockJsSessionFactory, TransportHandler

public class WebSocketTransportHandler extends AbstractTransportHandler implements SockJsSessionFactory, HandshakeHandler, Lifecycle, ServletContextAware
WebSocket-based TransportHandler. Uses SockJsWebSocketHandler and WebSocketServerSockJsSession to add SockJS processing.

Also implements HandshakeHandler to support raw WebSocket communication at SockJS URL "/websocket".

Since:
4.0
Author:
Rossen Stoyanchev
  • Constructor Details

    • WebSocketTransportHandler

      public WebSocketTransportHandler(HandshakeHandler handshakeHandler)
  • Method Details

    • getTransportType

      public TransportType getTransportType()
      Description copied from interface: TransportHandler
      Return the transport type supported by this handler.
      Specified by:
      getTransportType in interface TransportHandler
    • getHandshakeHandler

      public HandshakeHandler getHandshakeHandler()
    • setServletContext

      public void setServletContext(jakarta.servlet.ServletContext servletContext)
      Description copied from interface: ServletContextAware
      Set the ServletContext 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 after ApplicationContextAware's setApplicationContext.

      Specified by:
      setServletContext in interface ServletContextAware
      Parameters:
      servletContext - the ServletContext object to be used by this object
      See Also:
    • start

      public 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 a hard start signal to all components that apply, even to non-auto-startup components.

      Specified by:
      start in interface Lifecycle
      See Also:
    • stop

      public 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 when asynchronous stop behavior is necessary.

      Note that this stop notification is not guaranteed to come before destruction: On regular shutdown, Lifecycle beans will first receive a stop notification before the general destruction callbacks are being propagated; however, on hot refresh during a context's lifetime or on aborted refresh attempts, a given bean's destroy method will be called without any consideration of stop signals upfront.

      Should not throw an exception if the component is not running (not 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:
    • isRunning

      public 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
    • checkSessionType

      public boolean checkSessionType(SockJsSession session)
      Description copied from interface: TransportHandler
      Check whether the type of the given session matches the transport type of this TransportHandler where session id and the transport type are extracted from the SockJS URL.
      Specified by:
      checkSessionType in interface TransportHandler
      Returns:
      true if the session matches (and would therefore get accepted by TransportHandler.handleRequest(ServerHttpRequest, ServerHttpResponse, WebSocketHandler, SockJsSession)), or false otherwise
    • createSession

      public AbstractSockJsSession createSession(String id, WebSocketHandler handler, Map<String,Object> attrs)
      Description copied from interface: SockJsSessionFactory
      Create a new SockJS session.
      Specified by:
      createSession in interface SockJsSessionFactory
      Parameters:
      id - the ID of the session
      handler - the underlying WebSocketHandler
      attrs - handshake request specific attributes
      Returns:
      a new session, never null
    • handleRequest

      public void handleRequest(ServerHttpRequest request, ServerHttpResponse response, WebSocketHandler wsHandler, SockJsSession wsSession) throws SockJsException
      Description copied from interface: TransportHandler
      Handle the given request and delegate messages to the provided WebSocketHandler.
      Specified by:
      handleRequest in interface TransportHandler
      Parameters:
      request - the current request
      response - the current response
      wsHandler - the target WebSocketHandler (never null)
      wsSession - the SockJS session (never null)
      Throws:
      SockJsException - raised when request processing fails as explained in SockJsService
    • doHandshake

      public boolean doHandshake(ServerHttpRequest request, ServerHttpResponse response, WebSocketHandler handler, Map<String,Object> attributes) throws HandshakeFailureException
      Description copied from interface: HandshakeHandler
      Initiate the handshake.
      Specified by:
      doHandshake in interface HandshakeHandler
      Parameters:
      request - the current request
      response - the current response
      handler - the handler to process WebSocket messages; see PerConnectionWebSocketHandler for providing a handler with per-connection lifecycle.
      attributes - the attributes from the HTTP handshake to associate with the WebSocket session; the provided attributes are copied, the original map is not used.
      Returns:
      whether the handshake negotiation was successful or not. In either case the response status, headers, and body will have been updated to reflect the result of the negotiation
      Throws:
      HandshakeFailureException - thrown when handshake processing failed to complete due to an internal, unrecoverable error, i.e. a server error as opposed to a failure to successfully negotiate the handshake.