Class ServerWebSocketContainer
- All Implemented Interfaces:
Request.Handler
,Container
,Destroyable
,Dumpable
,Dumpable.DumpableContainer
,LifeCycle
,Invocable
,Configurable
,WebSocketContainer
A server-side WebSocket container that allows to map
URI paths to WebSocket endpoints and configure WebSocket parameters such as idle timeouts,
max WebSocket message sizes, etc.
Direct WebSocket upgrades not mapped to URI paths are possible via
upgrade(WebSocketCreator, Request, Response, Callback)
.
-
Nested Class Summary
Nested classes/interfaces inherited from class org.eclipse.jetty.util.component.AbstractLifeCycle
AbstractLifeCycle.AbstractLifeCycleListener, AbstractLifeCycle.StopException
Nested classes/interfaces inherited from interface org.eclipse.jetty.util.component.Container
Container.InheritedListener, Container.Listener
Nested classes/interfaces inherited from interface org.eclipse.jetty.util.component.Dumpable
Dumpable.DumpableContainer
Nested classes/interfaces inherited from interface org.eclipse.jetty.util.thread.Invocable
Invocable.Callable, Invocable.InvocationType, Invocable.ReadyTask, Invocable.Task
Nested classes/interfaces inherited from interface org.eclipse.jetty.util.component.LifeCycle
LifeCycle.Listener
Nested classes/interfaces inherited from interface org.eclipse.jetty.server.Request.Handler
Request.Handler.AbortException
-
Field Summary
Fields inherited from class org.eclipse.jetty.util.component.AbstractLifeCycle
FAILED, STARTED, STARTING, STOPPED, STOPPING
Fields inherited from interface org.eclipse.jetty.util.thread.Invocable
__nonBlocking, NOOP
-
Method Summary
Modifier and TypeMethodDescriptionvoid
addMapping
(String pathSpec, WebSocketCreator creator) Maps the givenpathSpec
to the creator of WebSocket endpoints.void
addMapping
(PathSpec pathSpec, WebSocketCreator creator) Maps the givenpathSpec
to the creator of WebSocket endpoints.void
addSessionListener
(WebSocketSessionListener listener) Register a WebSocketSessionListener with the containerstatic ServerWebSocketContainer
Returns theServerWebSocketContainer
, ensuring that it is available viaget(Context)
.static ServerWebSocketContainer
ensure
(Server server, ContextHandler contextHandler) Returns theServerWebSocketContainer
, ensuring that it is available viaget(Context)
.static ServerWebSocketContainer
Returns theServerWebSocketContainer
present as the context attribute under the name corresponding to the full qualified name of classWebSocketContainer
.The Container provided Executor.The duration that a websocket may be idle before being closed by the implementationint
The input (read from network layer) buffer size.long
Get the maximum size of a binary message during parsing.long
The maximum payload size of any WebSocket Frame which can be received.int
Get the maximum number of data frames allowed to be waiting to be sent at any one time.long
Get the maximum size of a text message during parsing.Get the collection of open Sessions being tracked by this containerint
The output (write to network layer) buffer size.boolean
Matches the givenrequest
against existing WebSocket mappings, upgrading to WebSocket if there is a match.boolean
If true, frames are automatically fragmented to respect the maximum frame size.void
Notify the Session Listeners of an event.boolean
Remove a WebSocketSessionListener from the containervoid
setAutoFragment
(boolean autoFragment) If set to true, frames are automatically fragmented to respect the maximum frame size.void
setIdleTimeout
(Duration duration) The duration that a websocket may be idle before being closed by the implementationvoid
setInputBufferSize
(int size) The input (read from network layer) buffer size.void
setInvocationType
(Invocable.InvocationType invocationType) Sets the invocation type of this container.void
setMaxBinaryMessageSize
(long size) The maximum size of a binary message during parsing/generating.void
setMaxFrameSize
(long maxFrameSize) The maximum payload size of any WebSocket Frame which can be received.void
setMaxOutgoingFrames
(int maxOutgoingFrames) Set the maximum number of data frames allowed to be waiting to be sent at any one time.void
setMaxTextMessageSize
(long size) The maximum size of a text message during parsing/generating.void
setOutputBufferSize
(int size) The output (write to network layer) buffer size.boolean
upgrade
(WebSocketCreator creator, Request request, Response response, Callback callback) Upgrades the givenrequest
without matching against the WebSocket mappings.Methods inherited from class org.eclipse.jetty.util.component.ContainerLifeCycle
addBean, addBean, addEventListener, addManaged, contains, destroy, doStart, doStop, dump, dump, dump, dumpObjects, dumpStdErr, getBean, getBeans, getBeans, getContainedBeans, getContainedBeans, installBean, installBean, isAuto, isManaged, isUnmanaged, manage, removeBean, removeBeans, removeEventListener, setBeans, start, stop, unmanage, updateBean, updateBean, updateBeans, updateBeans
Methods inherited from class org.eclipse.jetty.util.component.AbstractLifeCycle
getEventListeners, getState, getState, isFailed, isRunning, isStarted, isStarting, isStopped, isStopping, setEventListeners, start, stop, toString
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
Methods inherited from interface org.eclipse.jetty.util.component.Container
getCachedBeans, getEventListeners
Methods inherited from interface org.eclipse.jetty.util.component.Dumpable.DumpableContainer
isDumpable
-
Method Details
-
ensure
Returns the
ServerWebSocketContainer
, ensuring that it is available viaget(Context)
.If the
ServerWebSocketContainer
is not already available, an instance is created, stored to be available viaget(Context)
and returned.This method should be invoked during the setup of the
Handler
hierarchy.- Parameters:
server
- theServer
object used to lookup common WebSocket componentscontextHandler
- theContextHandler
used to store theServerWebSocketContainer
- Returns:
- a non-
null
ServerWebSocketContainer
-
ensure
Returns the
ServerWebSocketContainer
, ensuring that it is available viaget(Context)
.If the
ServerWebSocketContainer
is not already available, an instance is created, stored to be available viaget(Context)
and returned.This method should be invoked during the setup of the
Handler
hierarchy.- Parameters:
server
- theServer
object used to lookup common WebSocket components and store theServerWebSocketContainer
- Returns:
- a non-
null
ServerWebSocketContainer
-
get
Returns the
ServerWebSocketContainer
present as the context attribute under the name corresponding to the full qualified name of classWebSocketContainer
.- Parameters:
context
- theContext
to look for the attribute- Returns:
- the
ServerWebSocketContainer
stored as an attribute, ornull
if no such attribute is present
-
getExecutor
Description copied from interface:WebSocketContainer
The Container provided Executor.- Specified by:
getExecutor
in interfaceWebSocketContainer
-
getOpenSessions
Description copied from interface:WebSocketContainer
Get the collection of open Sessions being tracked by this container- Specified by:
getOpenSessions
in interfaceWebSocketContainer
- Returns:
- the collection of open sessions
-
addSessionListener
Description copied from interface:WebSocketContainer
Register a WebSocketSessionListener with the container- Specified by:
addSessionListener
in interfaceWebSocketContainer
- Parameters:
listener
- the listener
-
removeSessionListener
Description copied from interface:WebSocketContainer
Remove a WebSocketSessionListener from the container- Specified by:
removeSessionListener
in interfaceWebSocketContainer
- Parameters:
listener
- the listener- Returns:
- true if listener was present and removed
-
notifySessionListeners
Description copied from interface:WebSocketContainer
Notify the Session Listeners of an event.- Specified by:
notifySessionListeners
in interfaceWebSocketContainer
- Parameters:
consumer
- the consumer to call for each tracked listener
-
getIdleTimeout
Description copied from interface:Configurable
The duration that a websocket may be idle before being closed by the implementation- Specified by:
getIdleTimeout
in interfaceConfigurable
- Returns:
- the timeout duration
-
setIdleTimeout
Description copied from interface:Configurable
The duration that a websocket may be idle before being closed by the implementation- Specified by:
setIdleTimeout
in interfaceConfigurable
- Parameters:
duration
- the timeout duration (may not be null or negative)
-
getInputBufferSize
public int getInputBufferSize()Description copied from interface:Configurable
The input (read from network layer) buffer size.This is the raw read operation buffer size, before the parsing of the websocket frames.
- Specified by:
getInputBufferSize
in interfaceConfigurable
- Returns:
- the raw network buffer input size.
-
setInputBufferSize
public void setInputBufferSize(int size) Description copied from interface:Configurable
The input (read from network layer) buffer size.- Specified by:
setInputBufferSize
in interfaceConfigurable
- Parameters:
size
- the size in bytes
-
getOutputBufferSize
public int getOutputBufferSize()Description copied from interface:Configurable
The output (write to network layer) buffer size.This is the raw write operation buffer size and has no relationship to the websocket frame.
- Specified by:
getOutputBufferSize
in interfaceConfigurable
- Returns:
- the raw network buffer output size.
-
setOutputBufferSize
public void setOutputBufferSize(int size) Description copied from interface:Configurable
The output (write to network layer) buffer size.- Specified by:
setOutputBufferSize
in interfaceConfigurable
- Parameters:
size
- the size in bytes
-
getMaxBinaryMessageSize
public long getMaxBinaryMessageSize()Description copied from interface:Configurable
Get the maximum size of a binary message during parsing.This is a memory conservation option, memory over this limit will not be allocated by Jetty for handling binary messages. This applies to individual frames, whole message handling, and partial message handling.
Binary messages over this maximum will result in a close code 1009
StatusCode.MESSAGE_TOO_LARGE
- Specified by:
getMaxBinaryMessageSize
in interfaceConfigurable
- Returns:
- the maximum size of a binary message
-
setMaxBinaryMessageSize
public void setMaxBinaryMessageSize(long size) Description copied from interface:Configurable
The maximum size of a binary message during parsing/generating.Binary messages over this maximum will result in a close code 1009
StatusCode.MESSAGE_TOO_LARGE
- Specified by:
setMaxBinaryMessageSize
in interfaceConfigurable
- Parameters:
size
- the maximum allowed size of a binary message.
-
getMaxTextMessageSize
public long getMaxTextMessageSize()Description copied from interface:Configurable
Get the maximum size of a text message during parsing.This is a memory conservation option, memory over this limit will not be allocated by Jetty for handling text messages. This applies to individual frames, whole message handling, and partial message handling.
Text messages over this maximum will result in a close code 1009
StatusCode.MESSAGE_TOO_LARGE
- Specified by:
getMaxTextMessageSize
in interfaceConfigurable
- Returns:
- the maximum size of a text message.
-
setMaxTextMessageSize
public void setMaxTextMessageSize(long size) Description copied from interface:Configurable
The maximum size of a text message during parsing/generating.Text messages over this maximum will result in a close code 1009
StatusCode.MESSAGE_TOO_LARGE
- Specified by:
setMaxTextMessageSize
in interfaceConfigurable
- Parameters:
size
- the maximum allowed size of a text message.
-
getMaxFrameSize
public long getMaxFrameSize()Description copied from interface:Configurable
The maximum payload size of any WebSocket Frame which can be received.- Specified by:
getMaxFrameSize
in interfaceConfigurable
- Returns:
- the maximum size of a WebSocket Frame.
-
setMaxFrameSize
public void setMaxFrameSize(long maxFrameSize) Description copied from interface:Configurable
The maximum payload size of any WebSocket Frame which can be received.WebSocket Frames over this maximum will result in a close code 1009
StatusCode.MESSAGE_TOO_LARGE
- Specified by:
setMaxFrameSize
in interfaceConfigurable
- Parameters:
maxFrameSize
- the maximum allowed size of a WebSocket Frame.
-
isAutoFragment
public boolean isAutoFragment()Description copied from interface:Configurable
If true, frames are automatically fragmented to respect the maximum frame size.- Specified by:
isAutoFragment
in interfaceConfigurable
- Returns:
- whether to automatically fragment incoming WebSocket Frames.
-
setAutoFragment
public void setAutoFragment(boolean autoFragment) Description copied from interface:Configurable
If set to true, frames are automatically fragmented to respect the maximum frame size.- Specified by:
setAutoFragment
in interfaceConfigurable
- Parameters:
autoFragment
- whether to automatically fragment incoming WebSocket Frames.
-
getMaxOutgoingFrames
public int getMaxOutgoingFrames()Description copied from interface:Configurable
Get the maximum number of data frames allowed to be waiting to be sent at any one time. The default value is -1, this indicates there is no limit on how many frames can be queued to be sent by the implementation. If the limit is exceeded, subsequent frames sent are failed with aWritePendingException
but the connection is not failed and will remain open.- Specified by:
getMaxOutgoingFrames
in interfaceConfigurable
- Returns:
- the max number of frames.
-
setMaxOutgoingFrames
public void setMaxOutgoingFrames(int maxOutgoingFrames) Description copied from interface:Configurable
Set the maximum number of data frames allowed to be waiting to be sent at any one time. The default value is -1, this indicates there is no limit on how many frames can be queued to be sent by the implementation. If the limit is exceeded, subsequent frames sent are failed with aWritePendingException
but the connection is not failed and will remain open.- Specified by:
setMaxOutgoingFrames
in interfaceConfigurable
- Parameters:
maxOutgoingFrames
- the max number of frames.
-
addMapping
Maps the given
pathSpec
to the creator of WebSocket endpoints.The
pathSpec
format is that supported byWebSocketMappings.parsePathSpec(String)
.- Parameters:
pathSpec
- thepathSpec
to associate to the creatorcreator
- the creator of WebSocket endpoints
-
addMapping
Maps the given
pathSpec
to the creator of WebSocket endpoints.- Parameters:
pathSpec
- thepathSpec
to associate to the creatorcreator
- the creator of WebSocket endpoints
-
handle
Matches the given
request
against existing WebSocket mappings, upgrading to WebSocket if there is a match.Direct upgrades without using WebSocket mappings may be performed via
upgrade(WebSocketCreator, Request, Response, Callback)
.When
true
is returned, a response has been sent to the client and thecallback
has been completed; either because of a successful WebSocket upgrade, or because an error has occurred.When
false
is returned, a response has not been sent to the client, and thecallback
has not been completed; typically because the request path does not match any existing WebSocket mappings, so that the request can be handled by otherHandler
s.- Specified by:
handle
in interfaceRequest.Handler
- Parameters:
request
- the request to handle, possibly a WebSocket upgrade requestresponse
- the response to handlecallback
- the callback to complete when the handling is complete- Returns:
true
in case of WebSocket upgrades or failures,false
if the request was not handled- Throws:
WebSocketException
- there is an error during the upgrade- See Also:
-
upgrade
public boolean upgrade(WebSocketCreator creator, Request request, Response response, Callback callback) Upgrades the given
request
without matching against the WebSocket mappings.When
true
is returned, a response has been sent to the client and thecallback
has been completed; either because of a successful WebSocket upgrade, or because an error has occurred.When
false
is returned, a response has not been sent to the client, and thecallback
has not been completed; for example because the request is not a WebSocket upgrade; in this case the caller must arrange to send a response and complete the callback.- Parameters:
creator
- the creator of the WebSocket endpointrequest
- the request to upgrade, possibly a WebSocket upgrade requestresponse
- the responsecallback
- the callback to complete when the upgrade is complete- Returns:
true
in case of WebSocket upgrades or failures,false
if the request was not upgraded- Throws:
WebSocketException
- there is an error during the upgrade- See Also:
-
getInvocationType
- Specified by:
getInvocationType
in interfaceInvocable
- Specified by:
getInvocationType
in interfaceRequest.Handler
- Returns:
- the invocation type, typically blocking or non-blocking, of this container
- See Also:
-
setInvocationType
Sets the invocation type of this container.
The invocation type may be set to
Invocable.InvocationType.NON_BLOCKING
when it is known that application code in the listener methods or annotated methods of the WebSocket endpoint does not use blocking APIs.Setting the invocation type to
Invocable.InvocationType.NON_BLOCKING
, but then using blocking APIs in the WebSocket endpoint may result in a server lockup.By default
Invocable.InvocationType.BLOCKING
is returned, assuming that application code in the WebSocket endpoint uses blocking APIs.- Parameters:
invocationType
- the invocation type of this container
-