spring-framework / org.springframework.web.socket.sockjs.client / SockJsClient

SockJsClient

open class SockJsClient : WebSocketClient, Lifecycle

A SockJS implementation of org.springframework.web.socket.client.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.

Author
Rossen Stoyanchev

Since
4.1

See Also
<a href="http://sockjs.org">http://sockjs.org</a>org.springframework.web.socket.sockjs.client.Transport

Constructors

<init>

SockJsClient(transports: MutableList<Transport>)

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.

Functions

clearServerInfoCache

open fun clearServerInfoCache(): Unit

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.

doHandshake

open fun doHandshake(handler: WebSocketHandler, uriTemplate: String, vararg uriVars: Any): ListenableFuture<WebSocketSession>
fun doHandshake(handler: WebSocketHandler, headers: WebSocketHttpHeaders, url: URI): ListenableFuture<WebSocketSession>

getHttpHeaderNames

open fun getHttpHeaderNames(): Array<String>

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

getInfoReceiver

open fun getInfoReceiver(): InfoReceiver

Return the configured InfoReceiver (never null).

getMessageCodec

open fun getMessageCodec(): SockJsMessageCodec

Return the SockJsMessageCodec to use.

isRunning

open fun isRunning(): Boolean

setConnectTimeoutScheduler

open fun setConnectTimeoutScheduler(connectTimeoutScheduler: TaskScheduler): Unit

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.

setHttpHeaderNames

open fun setHttpHeaderNames(vararg httpHeaderNames: String): Unit

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.

setInfoReceiver

open fun setInfoReceiver(infoReceiver: InfoReceiver): Unit

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.

setMessageCodec

open fun setMessageCodec(messageCodec: SockJsMessageCodec): Unit

Set the SockJsMessageCodec to use.

By default org.springframework.web.socket.sockjs.frame.Jackson2SockJsMessageCodec is used if Jackson is on the classpath.

start

open fun start(): Unit

stop

open fun stop(): Unit