类 SockJsClient
- java.lang.Object
- org.springframework.web.socket.sockjs.client.SockJsClient
- 所有已实现的接口:
Lifecycle
,WebSocketClient
public class SockJsClient extends Object implements WebSocketClient, Lifecycle
A SockJS implementation ofWebSocketClient
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.- 从以下版本开始:
- 4.1
- 作者:
- Rossen Stoyanchev
- 另请参阅:
- https://github.com/sockjs/sockjs-client,
Transport
构造器概要
构造器 构造器 说明 SockJsClient(List<Transport> transports)
Create aSockJsClient
with the given transports.
方法概要
所有方法 实例方法 具体方法 修饰符和类型 方法 说明 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, String uriTemplate, Object... uriVars)
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 configuredInfoReceiver
(nevernull
).SockJsMessageCodec
getMessageCodec()
Return the SockJsMessageCodec to use.protected Principal
getUser()
Return the user to associate with the SockJS session and make available viaWebSocketSession.getPrincipal()
.boolean
isRunning()
Check whether this component is currently running.void
setConnectTimeoutScheduler(TaskScheduler connectTimeoutScheduler)
Configure aTaskScheduler
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 todoHandshake(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.void
setInfoReceiver(InfoReceiver infoReceiver)
Configure theInfoReceiver
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.
构造器详细资料
SockJsClient
public SockJsClient(List<Transport> transports)
Create aSockJsClient
with the given transports.If the list includes an
XhrTransport
(or more specifically an implementation ofInfoReceiver
) the instance is used to initialize theinfoReceiver
property, or otherwise is defaulted toRestTemplateXhrTransport
.- 参数:
transports
- the (non-empty) list of transports to use
方法详细资料
setHttpHeaderNames
public void setHttpHeaderNames(String... httpHeaderNames)
The names of HTTP headers that should be copied from the handshake headers of each call todoHandshake(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 names
getHttpHeaderNames
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 theInfoReceiver
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 ofInfoReceiver
that instance would have been used to initialize this property, or otherwise it defaults toRestTemplateXhrTransport
.- 参数:
infoReceiver
- the transport to use for the SockJS "Info" request
getInfoReceiver
public InfoReceiver getInfoReceiver()
Return the configuredInfoReceiver
(nevernull
).
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(TaskScheduler connectTimeoutScheduler)
Configure aTaskScheduler
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 use
start
public void start()
从接口复制的说明:Lifecycle
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.
- 指定者:
start
在接口中Lifecycle
- 另请参阅:
SmartLifecycle.isAutoStartup()
stop
public void stop()
从接口复制的说明:Lifecycle
Stop this component, typically in a synchronous fashion, such that the component is fully stopped upon return of this method. Consider implementingSmartLifecycle
and itsstop(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
在接口中Lifecycle
- 另请参阅:
SmartLifecycle.stop(Runnable)
,DisposableBean.destroy()
isRunning
public boolean isRunning()
从接口复制的说明:Lifecycle
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.
doHandshake
public ListenableFuture<WebSocketSession> doHandshake(WebSocketHandler handler, String uriTemplate, Object... uriVars)
- 指定者:
doHandshake
在接口中WebSocketClient
doHandshake
public final ListenableFuture<WebSocketSession> doHandshake(WebSocketHandler handler, WebSocketHttpHeaders headers, URI url)
- 指定者:
doHandshake
在接口中WebSocketClient
getUser
protected Principal getUser()
Return the user to associate with the SockJS session and make available viaWebSocketSession.getPrincipal()
.By default this method returns
null
.- 返回:
- 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.