Class WebSocketTransportHandler
- All Implemented Interfaces:
Aware,Lifecycle,ServletContextAware,HandshakeHandler,SockJsSessionFactory,TransportHandler
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 org.springframework.web.socket.sockjs.transport.handler.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(ServerHttpRequest request, ServerHttpResponse response, WebSocketHandler handler, Map<String, Object> attributes) Initiate the handshake.Return the transport type supported by this handler.voidhandleRequest(ServerHttpRequest request, ServerHttpResponse response, WebSocketHandler wsHandler, SockJsSession wsSession) Handle the given request and delegate messages to the providedWebSocketHandler.booleanCheck whether this component is currently running.voidsetServletContext(ServletContext servletContext) Set theServletContextthat this object runs in.voidstart()Start this component.voidstop()Stop this component, typically in a synchronous fashion, such that the component is fully stopped upon return of this method.Methods inherited from class org.springframework.web.socket.sockjs.transport.handler.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
Description copied from interface:ServletContextAwareSet theServletContextthat this object runs in.Invoked after population of normal bean properties but before an init callback like InitializingBean's
afterPropertiesSetor a custom init-method. Invoked after ApplicationContextAware'ssetApplicationContext.- Specified by:
setServletContextin interfaceServletContextAware- Parameters:
servletContext- the ServletContext object to be used by this object- See Also:
-
start
public void start()Description copied from interface:LifecycleStart 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.
-
stop
public void stop()Description copied from interface:LifecycleStop this component, typically in a synchronous fashion, such that the component is fully stopped upon return of this method. Consider implementingSmartLifecycleand itsstop(Runnable)variant when asynchronous stop behavior is necessary.Note that this stop notification is not guaranteed to come before destruction: On regular shutdown,
Lifecyclebeans 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.
-
isRunning
public boolean isRunning()Description copied from interface:LifecycleCheck whether this component is currently running.In the case of a container, this will return
trueonly if all components that apply are currently running. -
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(org.springframework.http.server.ServerHttpRequest, org.springframework.http.server.ServerHttpResponse, org.springframework.web.socket.WebSocketHandler, org.springframework.web.socket.sockjs.transport.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(ServerHttpRequest request, 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(ServerHttpRequest request, 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.
-