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
-
Field Summary
Fields inherited from class AbstractTransportHandler
logger -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionbooleancheckSessionType(SockJsSession session) Check whether the type of the given session matches the transport type of thisTransportHandlerwhere session id and the transport type are extracted from the SockJS URL.createSession(String id, WebSocketHandler handler, Map<String, Object> attrs) Create a new SockJS session.booleandoHandshake(org.springframework.http.server.ServerHttpRequest request, org.springframework.http.server.ServerHttpResponse response, WebSocketHandler handler, Map<String, Object> attributes) Initiate the handshake.Return the transport type supported by this handler.voidhandleRequest(org.springframework.http.server.ServerHttpRequest request, org.springframework.http.server.ServerHttpResponse response, WebSocketHandler wsHandler, SockJsSession wsSession) Handle the given request and delegate messages to the providedWebSocketHandler.booleanvoidsetServletContext(jakarta.servlet.ServletContext servletContext) voidstart()voidstop()Methods inherited from class AbstractTransportHandler
getServiceConfig, initialize
-
Constructor Details
-
WebSocketTransportHandler
-
-
Method Details
-
getTransportType
Description copied from interface:TransportHandlerReturn the transport type supported by this handler.- Specified by:
getTransportTypein interfaceTransportHandler
-
getHandshakeHandler
-
setServletContext
public void setServletContext(jakarta.servlet.ServletContext servletContext) - Specified by:
setServletContextin interfaceorg.springframework.web.context.ServletContextAware
-
start
public void start()- Specified by:
startin interfaceorg.springframework.context.Lifecycle
-
stop
public void stop()- Specified by:
stopin interfaceorg.springframework.context.Lifecycle
-
isRunning
public boolean isRunning()- Specified by:
isRunningin interfaceorg.springframework.context.Lifecycle
-
checkSessionType
Description copied from interface:TransportHandlerCheck whether the type of the given session matches the transport type of thisTransportHandlerwhere session id and the transport type are extracted from the SockJS URL.- Specified by:
checkSessionTypein interfaceTransportHandler- Returns:
trueif the session matches (and would therefore get accepted byTransportHandler.handleRequest(ServerHttpRequest, ServerHttpResponse, WebSocketHandler, SockJsSession)), orfalseotherwise
-
createSession
public AbstractSockJsSession createSession(String id, WebSocketHandler handler, Map<String, Object> attrs) Description copied from interface:SockJsSessionFactoryCreate a new SockJS session.- Specified by:
createSessionin interfaceSockJsSessionFactory- Parameters:
id- the ID of the sessionhandler- the underlyingWebSocketHandlerattrs- 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:TransportHandlerHandle the given request and delegate messages to the providedWebSocketHandler.- Specified by:
handleRequestin interfaceTransportHandler- Parameters:
request- the current requestresponse- the current responsewsHandler- the target WebSocketHandler (nevernull)wsSession- the SockJS session (nevernull)- Throws:
SockJsException- raised when request processing fails as explained inSockJsService
-
doHandshake
public boolean doHandshake(org.springframework.http.server.ServerHttpRequest request, org.springframework.http.server.ServerHttpResponse response, WebSocketHandler handler, Map<String, Object> attributes) throws HandshakeFailureExceptionDescription copied from interface:HandshakeHandlerInitiate the handshake.- Specified by:
doHandshakein interfaceHandshakeHandler- Parameters:
request- the current requestresponse- the current responsehandler- the handler to process WebSocket messages; seePerConnectionWebSocketHandlerfor 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.
-