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 org.springframework.web.socket.sockjs.transport.handler.AbstractTransportHandler
logger
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionboolean
checkSessionType
(SockJsSession session) Check whether the type of the given session matches the transport type of thisTransportHandler
where 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.boolean
doHandshake
(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.void
handleRequest
(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
.boolean
void
setServletContext
(ServletContext servletContext) void
start()
void
stop()
Methods inherited from class org.springframework.web.socket.sockjs.transport.handler.AbstractTransportHandler
getServiceConfig, initialize
-
Constructor Details
-
WebSocketTransportHandler
-
-
Method Details
-
getTransportType
Description copied from interface:TransportHandler
Return the transport type supported by this handler.- Specified by:
getTransportType
in interfaceTransportHandler
-
getHandshakeHandler
-
setServletContext
- Specified by:
setServletContext
in interfaceorg.springframework.web.context.ServletContextAware
-
start
public void start()- Specified by:
start
in interfaceorg.springframework.context.Lifecycle
-
stop
public void stop()- Specified by:
stop
in interfaceorg.springframework.context.Lifecycle
-
isRunning
public boolean isRunning()- Specified by:
isRunning
in interfaceorg.springframework.context.Lifecycle
-
checkSessionType
Description copied from interface:TransportHandler
Check whether the type of the given session matches the transport type of thisTransportHandler
where session id and the transport type are extracted from the SockJS URL.- Specified by:
checkSessionType
in interfaceTransportHandler
- Returns:
true
if 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)
), orfalse
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 interfaceSockJsSessionFactory
- Parameters:
id
- the ID of the sessionhandler
- the underlyingWebSocketHandler
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 providedWebSocketHandler
.- Specified by:
handleRequest
in 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:HandshakeHandler
Initiate the handshake.- Specified by:
doHandshake
in interfaceHandshakeHandler
- Parameters:
request
- the current requestresponse
- the current responsehandler
- the handler to process WebSocket messages; seePerConnectionWebSocketHandler
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.
-