Class SockJsClient
- All Implemented Interfaces:
Lifecycle
,WebSocketClient
WebSocketClient
with fallback alternatives that simulate a WebSocket interaction through plain
HTTP streaming and long polling techniques.
Implements Lifecycle
in order to propagate lifecycle events to
the transports it is configured with.
- Since:
- 4.1
- Author:
- Rossen Stoyanchev, Sam Brannen, Juergen Hoeller
- See Also:
-
Constructor Summary
ConstructorDescriptionSockJsClient
(List<Transport> transports) Create aSockJsClient
with the given transports. -
Method Summary
Modifier and TypeMethodDescriptionprotected SockJsUrlInfo
buildSockJsUrlInfo
(URI url) Create a newSockJsUrlInfo
for the current client execution.void
By default, the result of a SockJS "Info" request, including whether the server has WebSocket disabled and how long the request took (used for calculating transport timeout time) is cached.execute
(WebSocketHandler handler, String uriTemplate, Object... uriVars) Execute a handshake request to the given url and handle the resulting WebSocket session with the given handler.execute
(WebSocketHandler handler, WebSocketHttpHeaders headers, URI url) Execute a handshake request to the given url and handle the resulting WebSocket session with the given handler.String[]
The configured HTTP header names to be copied from the handshake headers and also included in other HTTP requests.Return the configuredInfoReceiver
(nevernull
).Return the SockJsMessageCodec to use.protected Principal
getUser()
Return the user to associate with the SockJS session and make available viaWebSocketSession.getPrincipal()
.boolean
Check whether this component is currently running.void
setConnectTimeoutScheduler
(TaskScheduler connectTimeoutScheduler) Configure aTaskScheduler
for scheduling a connect timeout task where the timeout value is calculated based on the duration of the initial SockJS "Info" request.void
setHttpHeaderNames
(String... httpHeaderNames) The names of HTTP headers that should be copied from the handshake headers of each call toexecute(WebSocketHandler, WebSocketHttpHeaders, URI)
and also used with other HTTP requests issued as part of that SockJS connection, for example, the initial info request, XHR send or receive requests.void
setInfoReceiver
(InfoReceiver infoReceiver) Configure theInfoReceiver
to use to perform the SockJS "Info" request before the SockJS session starts.void
setMessageCodec
(SockJsMessageCodec messageCodec) Set the SockJsMessageCodec to use.void
start()
Start this component.void
stop()
Stop this component, typically in a synchronous fashion, such that the component is fully stopped upon return of this method.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface org.springframework.web.socket.client.WebSocketClient
doHandshake, doHandshake
-
Constructor Details
-
SockJsClient
Create aSockJsClient
with the given transports.If the list includes an
XhrTransport
(or more specifically an implementation ofInfoReceiver
) the instance is used to initialize theinfoReceiver
property, or otherwise is defaulted toRestTemplateXhrTransport
.- Parameters:
transports
- the (non-empty) list of transports to use
-
-
Method Details
-
setHttpHeaderNames
The names of HTTP headers that should be copied from the handshake headers of each call toexecute(WebSocketHandler, WebSocketHttpHeaders, URI)
and also used with other HTTP requests issued as part of that SockJS connection, for example, the initial info request, XHR send or receive requests.By default if this property is not set, all handshake headers are also used for other HTTP requests. Set it if you want only a subset of handshake headers (for example, auth headers) to be used for other HTTP requests.
- Parameters:
httpHeaderNames
- the HTTP header names
-
getHttpHeaderNames
The configured HTTP header names to be copied from the handshake headers and also included in other HTTP requests. -
setInfoReceiver
Configure theInfoReceiver
to use to perform the SockJS "Info" request before the SockJS session starts.If the list of transports provided to the constructor contained an
XhrTransport
or an implementation ofInfoReceiver
that instance would have been used to initialize this property, or otherwise it defaults toRestTemplateXhrTransport
.- Parameters:
infoReceiver
- the transport to use for the SockJS "Info" request
-
getInfoReceiver
Return the configuredInfoReceiver
(nevernull
). -
setMessageCodec
Set the SockJsMessageCodec to use.By default
Jackson2SockJsMessageCodec
is used if Jackson is on the classpath. -
getMessageCodec
Return the SockJsMessageCodec to use. -
setConnectTimeoutScheduler
Configure aTaskScheduler
for scheduling a connect timeout task where the timeout value is calculated based on the duration of the initial SockJS "Info" request. The connect timeout task ensures a more timely fallback but is otherwise entirely optional.By default this is not configured in which case a fallback may take longer.
- Parameters:
connectTimeoutScheduler
- the task scheduler to use
-
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 the start signal to all components that apply.
-
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 implementingSmartLifecycle
and itsstop(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.
-
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. -
execute
public CompletableFuture<WebSocketSession> execute(WebSocketHandler handler, String uriTemplate, Object... uriVars) Description copied from interface:WebSocketClient
Execute a handshake request to the given url and handle the resulting WebSocket session with the given handler.- Specified by:
execute
in interfaceWebSocketClient
- Parameters:
handler
- the session handleruriTemplate
- the url templateuriVars
- the variables to expand the template- Returns:
- a future that completes when the session is available
-
execute
public final CompletableFuture<WebSocketSession> execute(WebSocketHandler handler, @Nullable WebSocketHttpHeaders headers, URI url) Description copied from interface:WebSocketClient
Execute a handshake request to the given url and handle the resulting WebSocket session with the given handler.- Specified by:
execute
in interfaceWebSocketClient
- Parameters:
handler
- the session handlerurl
- the url- Returns:
- a future that completes when the session is available
-
buildSockJsUrlInfo
Create a newSockJsUrlInfo
for the current client execution.The default implementation builds a
SockJsUrlInfo
which calculates a random server id and session id if necessary.- Parameters:
url
- the target URL- Since:
- 6.1.3
- See Also:
-
getUser
Return the user to associate with the SockJS session and make available viaWebSocketSession.getPrincipal()
.By default this method returns
null
.- Returns:
- the user to associate with the session (possibly
null
)
-
clearServerInfoCache
public void clearServerInfoCache()By default, the result of a SockJS "Info" request, including whether the server has WebSocket disabled and how long the request took (used for calculating transport timeout time) is cached. This method can be used to clear that cache hence causing it to re-populate.
-