public class SockJsClient extends java.lang.Object implements WebSocketClient, Lifecycle
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.
Transport
Constructor and Description |
---|
SockJsClient(java.util.List<Transport> transports)
Create a
SockJsClient with the given transports. |
Modifier and Type | Method and Description |
---|---|
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.
|
ListenableFuture<WebSocketSession> |
doHandshake(WebSocketHandler handler,
java.lang.String uriTemplate,
java.lang.Object... uriVars) |
ListenableFuture<WebSocketSession> |
doHandshake(WebSocketHandler handler,
WebSocketHttpHeaders headers,
java.net.URI url) |
java.lang.String[] |
getHttpHeaderNames()
The configured HTTP header names to be copied from the handshake
headers and also included in other HTTP requests.
|
InfoReceiver |
getInfoReceiver()
Return the configured
InfoReceiver (never null ). |
SockJsMessageCodec |
getMessageCodec()
Return the SockJsMessageCodec to use.
|
protected java.security.Principal |
getUser()
Return the user to associate with the SockJS session and make available via
WebSocketSession.getPrincipal() . |
boolean |
isRunning()
Check whether this component is currently running.
|
void |
setConnectTimeoutScheduler(TaskScheduler connectTimeoutScheduler)
Configure a
TaskScheduler for scheduling a connect timeout task
where the timeout value is calculated based on the duration of the initial
SockJS "Info" request. |
void |
setHttpHeaderNames(java.lang.String... httpHeaderNames)
The names of HTTP headers that should be copied from the handshake headers
of each call to
doHandshake(WebSocketHandler, WebSocketHttpHeaders, URI)
and also used with other HTTP requests issued as part of that SockJS
connection, e.g. |
void |
setInfoReceiver(InfoReceiver infoReceiver)
Configure the
InfoReceiver 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.
|
public SockJsClient(java.util.List<Transport> transports)
SockJsClient
with the given transports.
If the list includes an XhrTransport
(or more specifically an
implementation of InfoReceiver
) the instance is used to initialize
the infoReceiver
property, or
otherwise is defaulted to RestTemplateXhrTransport
.
transports
- the (non-empty) list of transports to usepublic void setHttpHeaderNames(java.lang.String... httpHeaderNames)
doHandshake(WebSocketHandler, WebSocketHttpHeaders, URI)
and also used with other HTTP requests issued as part of that SockJS
connection, e.g. 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 (e.g. auth headers) to be used for other HTTP requests.
httpHeaderNames
- HTTP header namespublic java.lang.String[] getHttpHeaderNames()
public void setInfoReceiver(InfoReceiver infoReceiver)
InfoReceiver
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 of InfoReceiver
that
instance would have been used to initialize this property, or otherwise
it defaults to RestTemplateXhrTransport
.
infoReceiver
- the transport to use for the SockJS "Info" requestpublic InfoReceiver getInfoReceiver()
InfoReceiver
(never null
).public void setMessageCodec(SockJsMessageCodec messageCodec)
By default Jackson2SockJsMessageCodec
is used if Jackson is on the classpath.
public SockJsMessageCodec getMessageCodec()
public void setConnectTimeoutScheduler(TaskScheduler connectTimeoutScheduler)
TaskScheduler
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.
connectTimeoutScheduler
- the task scheduler to usepublic void start()
Lifecycle
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.
start
in interface Lifecycle
SmartLifecycle.isAutoStartup()
public void stop()
Lifecycle
SmartLifecycle
and its stop(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.
stop
in interface Lifecycle
SmartLifecycle.stop(Runnable)
,
DisposableBean.destroy()
public boolean isRunning()
Lifecycle
In the case of a container, this will return true
only if all
components that apply are currently running.
public ListenableFuture<WebSocketSession> doHandshake(WebSocketHandler handler, java.lang.String uriTemplate, java.lang.Object... uriVars)
doHandshake
in interface WebSocketClient
public final ListenableFuture<WebSocketSession> doHandshake(WebSocketHandler handler, WebSocketHttpHeaders headers, java.net.URI url)
doHandshake
in interface WebSocketClient
protected java.security.Principal getUser()
WebSocketSession.getPrincipal()
.
By default this method returns null
.
null
)public void clearServerInfoCache()