public class HandshakeWebSocketService extends java.lang.Object implements WebSocketService, Lifecycle
WebSocketService
implementation that handles a WebSocket HTTP
handshake request by delegating to a RequestUpgradeStrategy
which
is either auto-detected (no-arg constructor) from the classpath but can
also be explicitly configured.Modifier and Type | Field and Description |
---|---|
private static <any> |
EMPTY_ATTRIBUTES |
private static boolean |
jettyPresent |
protected static Log |
logger |
private static boolean |
reactorNettyPresent |
private boolean |
running |
private static java.lang.String |
SEC_WEBSOCKET_KEY |
private static java.lang.String |
SEC_WEBSOCKET_PROTOCOL |
private java.util.function.Predicate<java.lang.String> |
sessionAttributePredicate |
private static boolean |
tomcatPresent |
private static boolean |
undertowPresent |
private RequestUpgradeStrategy |
upgradeStrategy |
Constructor and Description |
---|
HandshakeWebSocketService()
Default constructor automatic, classpath detection based discovery of the
RequestUpgradeStrategy to use. |
HandshakeWebSocketService(RequestUpgradeStrategy upgradeStrategy)
Alternative constructor with the
RequestUpgradeStrategy to use. |
Modifier and Type | Method and Description |
---|---|
private HandshakeInfo |
createHandshakeInfo(ServerWebExchange exchange,
ServerHttpRequest request,
java.lang.String protocol,
java.util.Map<java.lang.String,java.lang.Object> attributes) |
protected void |
doStart() |
protected void |
doStop() |
java.util.function.Predicate<java.lang.String> |
getSessionAttributePredicate()
Return the configured predicate for initialization WebSocket session
attributes from
WebSession attributes. |
RequestUpgradeStrategy |
getUpgradeStrategy()
Return the
RequestUpgradeStrategy for WebSocket requests. |
private <any> |
handleBadRequest(ServerWebExchange exchange,
java.lang.String reason) |
<any> |
handleRequest(ServerWebExchange exchange,
WebSocketHandler handler)
Handle the request with the given
WebSocketHandler . |
private <any> |
initAttributes(ServerWebExchange exchange) |
private static RequestUpgradeStrategy |
initUpgradeStrategy() |
boolean |
isRunning()
Check whether this component is currently running.
|
private java.lang.String |
selectProtocol(HttpHeaders headers,
WebSocketHandler handler) |
void |
setSessionAttributePredicate(java.util.function.Predicate<java.lang.String> predicate)
Configure a predicate to use to extract
WebSession attributes
and use them to initialize the WebSocket session with. |
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.
|
private static final java.lang.String SEC_WEBSOCKET_KEY
private static final java.lang.String SEC_WEBSOCKET_PROTOCOL
private static final <any> EMPTY_ATTRIBUTES
private static final boolean tomcatPresent
private static final boolean jettyPresent
private static final boolean undertowPresent
private static final boolean reactorNettyPresent
protected static final Log logger
private final RequestUpgradeStrategy upgradeStrategy
@Nullable private java.util.function.Predicate<java.lang.String> sessionAttributePredicate
private volatile boolean running
public HandshakeWebSocketService()
RequestUpgradeStrategy
to use.public HandshakeWebSocketService(RequestUpgradeStrategy upgradeStrategy)
RequestUpgradeStrategy
to use.upgradeStrategy
- the strategy to useprivate static RequestUpgradeStrategy initUpgradeStrategy()
public RequestUpgradeStrategy getUpgradeStrategy()
RequestUpgradeStrategy
for WebSocket requests.public void setSessionAttributePredicate(@Nullable java.util.function.Predicate<java.lang.String> predicate)
WebSession
attributes
and use them to initialize the WebSocket session with.
By default this is not set in which case no attributes are passed.
predicate
- the predicate@Nullable public java.util.function.Predicate<java.lang.String> getSessionAttributePredicate()
WebSession
attributes.public void start()
Lifecycle
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
in interface Lifecycle
SmartLifecycle.isAutoStartup()
protected void doStart()
public void stop()
Lifecycle
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.
stop
in interface Lifecycle
SmartLifecycle.stop(Runnable)
,
DisposableBean.destroy()
protected void doStop()
public boolean isRunning()
Lifecycle
In the case of a container, this will return true
only if all
components that apply are currently running.
public <any> handleRequest(ServerWebExchange exchange, WebSocketHandler handler)
WebSocketService
WebSocketHandler
.handleRequest
in interface WebSocketService
exchange
- the current exchangehandler
- handler for WebSocket sessionMono<Void>
that completes when application handling of
the WebSocket session completes.private <any> handleBadRequest(ServerWebExchange exchange, java.lang.String reason)
@Nullable private java.lang.String selectProtocol(HttpHeaders headers, WebSocketHandler handler)
private <any> initAttributes(ServerWebExchange exchange)
private HandshakeInfo createHandshakeInfo(ServerWebExchange exchange, ServerHttpRequest request, @Nullable java.lang.String protocol, java.util.Map<java.lang.String,java.lang.Object> attributes)