Class WebSocketTransportHandler

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

public class WebSocketTransportHandler extends AbstractTransportHandler implements SockJsSessionFactory, HandshakeHandler, org.springframework.context.Lifecycle, org.springframework.web.context.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)
      Specified by:
      setServletContext in interface org.springframework.web.context.ServletContextAware
    • start

      public void start()
      Specified by:
      start in interface org.springframework.context.Lifecycle
    • stop

      public void stop()
      Specified by:
      stop in interface org.springframework.context.Lifecycle
    • isRunning

      public boolean isRunning()
      Specified by:
      isRunning in interface org.springframework.context.Lifecycle
    • 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(org.springframework.http.server.ServerHttpRequest request, org.springframework.http.server.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(org.springframework.http.server.ServerHttpRequest request, org.springframework.http.server.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.