org.springframework.web.socket.sockjs.client

## Class SockJsClient

- java.lang.Object
- - org.springframework.web.socket.sockjs.client.SockJsClient

All Implemented Interfaces:
  org.springframework.context.Lifecycle, WebSocketClient

    public class SockJsClient
    extends Object
    implements WebSocketClient, org.springframework.context.Lifecycle

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.

Since:
  4.1
Author:
  Rossen Stoyanchev
See Also:
  https://github.com/sockjs/sockjs-client, `Transport`

### Constructor Summary

Constructors
Constructor and Description
`SockJsClient(List<Transport> transports)` Create a `SockJsClient` with the given transports.

### Method Summary

All Methods Instance Methods Concrete Methods
`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)`
`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 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` `setHttpHeaderNames(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()`
`void` `stop()`

### Methods inherited from class java.lang.Object

`clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

### Constructor Detail

#### SockJsClient

    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` - the (non-empty) list of transports to use

### Method Detail

#### setHttpHeaderNames

    public void setHttpHeaderNames(@Nullable
                                   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. 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` - the HTTP header names

#### getHttpHeaderNames

    @Nullable
    public String[] getHttpHeaderNames()

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

#### setInfoReceiver

    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` - the transport to use for the SockJS "Info" request

#### getInfoReceiver

    public InfoReceiver getInfoReceiver()

Return the configured `InfoReceiver` (never `null`).

#### setMessageCodec

    public void setMessageCodec(SockJsMessageCodec messageCodec)

Set the SockJsMessageCodec to use.

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

#### getMessageCodec

    public SockJsMessageCodec getMessageCodec()

Return the SockJsMessageCodec to use.

#### setConnectTimeoutScheduler

    public 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. 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()

Specified by:
  `start` in interface `org.springframework.context.Lifecycle`

#### stop

    public void stop()

Specified by:
  `stop` in interface `org.springframework.context.Lifecycle`

#### isRunning

    public boolean isRunning()

Specified by:
  `isRunning` in interface `org.springframework.context.Lifecycle`

#### doHandshake

    public org.springframework.util.concurrent.ListenableFuture<WebSocketSession> doHandshake(WebSocketHandler handler,
                                                                                              String uriTemplate,
                                                                                              Object... uriVars)

Specified by:
  `doHandshake` in interface `WebSocketClient`

#### doHandshake

    public final org.springframework.util.concurrent.ListenableFuture<WebSocketSession> doHandshake(WebSocketHandler handler,
                                                                                                    @Nullable
                                                                                                    WebSocketHttpHeaders headers,
                                                                                                    URI url)

Specified by:
  `doHandshake` in interface `WebSocketClient`

#### getUser

    @Nullable
    protected Principal getUser()

Return the user to associate with the SockJS session and make available via `WebSocketSession.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.