public class SockJsClient extends Object implements WebSocketClient, org.springframework.context.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(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.
|
org.springframework.util.concurrent.ListenableFuture<WebSocketSession> |
doHandshake(WebSocketHandler handler,
String uriTemplate,
Object... uriVars) |
org.springframework.util.concurrent.ListenableFuture<WebSocketSession> |
doHandshake(WebSocketHandler handler,
WebSocketHttpHeaders headers,
URI url) |
InfoReceiver |
getInfoReceiver()
Return the configured
InfoReceiver (never null ). |
SockJsMessageCodec |
getMessageCodec()
Return the SockJsMessageCodec to use.
|
protected Principal |
getUser()
Return the user to associate with the SockJS session and make available via
WebSocketSession.getPrincipal() . |
boolean |
isRunning() |
void |
setConnectTimeoutScheduler(org.springframework.scheduling.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 |
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() |
void |
stop() |
public SockJsClient(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 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(org.springframework.scheduling.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()
start
in interface org.springframework.context.Lifecycle
public void stop()
stop
in interface org.springframework.context.Lifecycle
public boolean isRunning()
isRunning
in interface org.springframework.context.Lifecycle
public org.springframework.util.concurrent.ListenableFuture<WebSocketSession> doHandshake(WebSocketHandler handler, String uriTemplate, Object... uriVars)
doHandshake
in interface WebSocketClient
public final org.springframework.util.concurrent.ListenableFuture<WebSocketSession> doHandshake(WebSocketHandler handler, WebSocketHttpHeaders headers, URI url)
doHandshake
in interface WebSocketClient
protected Principal getUser()
WebSocketSession.getPrincipal()
.
By default this method returns null
.
null
)public void clearServerInfoCache()