Top Description Inners Fields Constructors Methods
org.springframework.web.socket.sockjs.client

public Class SockJsClient

extends Object
implements WebSocketClient, Lifecycle
Class Inheritance
All Implemented Interfaces
org.springframework.context.Lifecycle, org.springframework.web.socket.client.WebSocketClient
Imports
java.net.URI, java.security.Principal, java.util.ArrayList, .List, .Map, .Set, java.util.concurrent.CompletableFuture, .ConcurrentHashMap, org.apache.commons.logging.Log, .LogFactory, org.springframework.context.Lifecycle, org.springframework.http.HttpHeaders, org.springframework.lang.Nullable, org.springframework.scheduling.TaskScheduler, org.springframework.util.Assert, .ClassUtils, .CollectionUtils, org.springframework.web.socket.WebSocketHandler, .WebSocketHttpHeaders, .WebSocketSession, org.springframework.web.socket.client.WebSocketClient, org.springframework.web.socket.sockjs.frame.Jackson2SockJsMessageCodec, .SockJsMessageCodec, org.springframework.web.socket.sockjs.transport.TransportType, org.springframework.web.util.UriComponentsBuilder

A SockJS implementation of 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.

Authors
Rossen Stoyanchev, Sam Brannen, Juergen Hoeller
Since
4.1
See Also
https://github.com/sockjs/sockjs-client, org.springframework.web.socket.sockjs.client.Transport

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static class
SockJsClient.ServerInfo

A simple value object holding the result from a SockJS "Info" request.

Field Summary

Modifier and TypeField and Description
private TaskScheduler
private String[]
private InfoReceiver
private static final boolean
private static final Log
private SockJsMessageCodec
private volatile boolean
private final Map<URI, SockJsClient.ServerInfo>
private static final Set<String>
private final List<Transport>

Constructor Summary

AccessConstructor and Description
public
SockJsClient(List<Transport>
the (non-empty) list of transports to use
transports
)

Create a SockJsClient with the given transports.

Method Summary

Modifier and TypeMethod and Description
protected SockJsUrlInfo
buildSockJsUrlInfo(URI
the target URL
url
)

Create a new SockJsUrlInfo for the current client execution.

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.

private DefaultTransportRequest
public CompletableFuture<WebSocketSession>
execute(WebSocketHandler
the session handler
handler
,
String
the url template
uriTemplate
,
Object...
the variables to expand the template
uriVars
)

Implements org.springframework.web.socket.client.WebSocketClient.execute.

Execute a handshake request to the given url and handle the resulting WebSocket session with the given handler.

public final CompletableFuture<WebSocketSession>
execute(WebSocketHandler
the session handler
handler
,
WebSocketHttpHeaders headers, URI
the url
url
)

Implements org.springframework.web.socket.client.WebSocketClient.execute.

Execute a handshake request to the given url and handle the resulting WebSocket session with the given handler.

public String[]
getHttpHeaderNames()

The configured HTTP header names to be copied from the handshake headers and also included in other HTTP requests.

private HttpHeaders
getHttpRequestHeaders(HttpHeaders webSocketHttpHeaders)

public InfoReceiver
getInfoReceiver()

Return the configured InfoReceiver (never null).

public SockJsMessageCodec
getMessageCodec()

Return the SockJsMessageCodec to use.

private SockJsClient.ServerInfo
getServerInfo(SockJsUrlInfo sockJsUrlInfo, HttpHeaders headers)

protected Principal

Returns:

the user to associate with the session (possibly null)
getUser
()

Return the user to associate with the SockJS session and make available via org.springframework.web.socket.WebSocketSession#getPrincipal().

private static InfoReceiver
public boolean
isRunning()

Implements org.springframework.context.Lifecycle.isRunning.

Check whether this component is currently running.

public void
setConnectTimeoutScheduler(TaskScheduler
the task scheduler to use
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.

public void
setHttpHeaderNames(String...
the HTTP header names
httpHeaderNames
)

The names of HTTP headers that should be copied from the handshake headers of each call to SockJsClient#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.

public void
setInfoReceiver(InfoReceiver
the transport to use for the SockJS "Info" request
infoReceiver
)

Configure the InfoReceiver to use to perform the SockJS "Info" request before the SockJS session starts.

public void
setMessageCodec(SockJsMessageCodec messageCodec)

Set the SockJsMessageCodec to use.

public void
start()

Implements org.springframework.context.Lifecycle.start.

Start this component.

public void
stop()

Implements org.springframework.context.Lifecycle.stop.

Stop this component, typically in a synchronous fashion, such that the component is fully stopped upon return of this method.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

connectTimeoutSchedulerback to summary
private TaskScheduler connectTimeoutScheduler
Annotations
@Nullable
httpHeaderNamesback to summary
private String[] httpHeaderNames
Annotations
@Nullable
infoReceiverback to summary
private InfoReceiver infoReceiver
jackson2Presentback to summary
private static final boolean jackson2Present
loggerback to summary
private static final Log logger
messageCodecback to summary
private SockJsMessageCodec messageCodec
Annotations
@Nullable
runningback to summary
private volatile boolean running
serverInfoCacheback to summary
private final Map<URI, SockJsClient.ServerInfo> serverInfoCache
supportedProtocolsback to summary
private static final Set<String> supportedProtocols
transportsback to summary
private final List<Transport> transports

Constructor Detail

SockJsClientback to summary
public SockJsClient(List<Transport> transports)

Create a 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.

Parameters
transports:List<Transport>

the (non-empty) list of transports to use

Method Detail

buildSockJsUrlInfoback to summary
protected SockJsUrlInfo buildSockJsUrlInfo(URI url)

Create a new SockJsUrlInfo for the current client execution.

The default implementation builds a SockJsUrlInfo which calculates a random server id and session id if necessary.

Parameters
url:URI

the target URL

Since
6.1.3
See Also
SockJsUrlInfo#SockJsUrlInfo(URI)
clearServerInfoCacheback to summary
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.

createRequestback to summary
private DefaultTransportRequest createRequest(SockJsUrlInfo urlInfo, HttpHeaders headers, SockJsClient.ServerInfo serverInfo)
executeback to summary
public CompletableFuture<WebSocketSession> execute(WebSocketHandler handler, String uriTemplate, Object... uriVars)

Implements org.springframework.web.socket.client.WebSocketClient.execute.

Doc from org.springframework.web.socket.client.WebSocketClient.execute.

Execute a handshake request to the given url and handle the resulting WebSocket session with the given handler.

Parameters
handler:WebSocketHandler

the session handler

uriTemplate:String

the url template

uriVars:Object[]

the variables to expand the template

Returns:CompletableFuture<WebSocketSession>

a future that completes when the session is available

Annotations
@Override
executeback to summary
public final CompletableFuture<WebSocketSession> execute(WebSocketHandler handler, WebSocketHttpHeaders headers, URI url)

Implements org.springframework.web.socket.client.WebSocketClient.execute.

Doc from org.springframework.web.socket.client.WebSocketClient.execute.

Execute a handshake request to the given url and handle the resulting WebSocket session with the given handler.

Parameters
handler:WebSocketHandler

the session handler

url:URI

the url

Returns:CompletableFuture<WebSocketSession>

a future that completes when the session is available

Annotations
@Override
getHttpHeaderNamesback to summary
public String[] getHttpHeaderNames()

The configured HTTP header names to be copied from the handshake headers and also included in other HTTP requests.

Annotations
@Nullable
getHttpRequestHeadersback to summary
private HttpHeaders getHttpRequestHeaders(HttpHeaders webSocketHttpHeaders)
Annotations
@Nullable
getInfoReceiverback to summary
public InfoReceiver getInfoReceiver()

Return the configured InfoReceiver (never null).

getMessageCodecback to summary
public SockJsMessageCodec getMessageCodec()

Return the SockJsMessageCodec to use.

getServerInfoback to summary
private SockJsClient.ServerInfo getServerInfo(SockJsUrlInfo sockJsUrlInfo, HttpHeaders headers)
getUserback to summary
protected Principal getUser()

Return the user to associate with the SockJS session and make available via org.springframework.web.socket.WebSocketSession#getPrincipal().

By default this method returns null.

Returns:Principal

the user to associate with the session (possibly null)

Annotations
@Nullable
initInfoReceiverback to summary
private static InfoReceiver initInfoReceiver(List<Transport> transports)
isRunningback to summary
public boolean isRunning()

Implements org.springframework.context.Lifecycle.isRunning.

Doc from org.springframework.context.Lifecycle.isRunning.

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.

Returns:boolean

whether the component is currently running

Annotations
@Override
setConnectTimeoutSchedulerback to summary
public 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. 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:TaskScheduler

the task scheduler to use

setHttpHeaderNamesback to summary
public void setHttpHeaderNames(String... httpHeaderNames)

The names of HTTP headers that should be copied from the handshake headers of each call to SockJsClient#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.

Parameters
httpHeaderNames:String[]

the HTTP header names

setInfoReceiverback to summary
public void setInfoReceiver(InfoReceiver infoReceiver)

Configure the 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.

Parameters
infoReceiver:InfoReceiver

the transport to use for the SockJS "Info" request

setMessageCodecback to summary
public void setMessageCodec(SockJsMessageCodec messageCodec)

Set the SockJsMessageCodec to use.

By default Jackson2SockJsMessageCodec is used if Jackson is on the classpath.

startback to summary
public void start()

Implements org.springframework.context.Lifecycle.start.

Doc from org.springframework.context.Lifecycle.start.

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.

Annotations
@Override
stopback to summary
public void stop()

Implements org.springframework.context.Lifecycle.stop.

Doc from org.springframework.context.Lifecycle.stop.

Stop this component, typically in a synchronous fashion, such that the component is fully stopped upon return of this method. Consider implementing 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.

Annotations
@Override
org.springframework.web.socket.sockjs.client back to summary

private Class SockJsClient.ServerInfo

extends Object
Class Inheritance
  • java.lang.Object
  • org.springframework.web.socket.sockjs.client.SockJsClient.ServerInfo

A simple value object holding the result from a SockJS "Info" request.

Field Summary

Modifier and TypeField and Description
private final long
private final boolean

Constructor Summary

AccessConstructor and Description
public
ServerInfo(String response, long responseTime)

Method Summary

Modifier and TypeMethod and Description
public long
public boolean
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

responseTimeback to summary
private final long responseTime
webSocketEnabledback to summary
private final boolean webSocketEnabled

Constructor Detail

ServerInfoback to summary
public ServerInfo(String response, long responseTime)

Method Detail

getRetransmissionTimeoutback to summary
public long getRetransmissionTimeout()
isWebSocketEnabledback to summary
public boolean isWebSocketEnabled()