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 AbstractLifeCycle
AbstractLifeCycle.AbstractLifeCycleListener, AbstractLifeCycle.StopExceptionNested classes/interfaces inherited from interface Container
Container.InheritedListener, Container.ListenerNested classes/interfaces inherited from interface Dumpable
Dumpable.DumpableContainerNested classes/interfaces inherited from interface Invocable
Invocable.Callable, Invocable.InvocationType, Invocable.ReadyTask, Invocable.TaskNested classes/interfaces inherited from interface LifeCycle
LifeCycle.ListenerNested classes/interfaces inherited from interface Request.Handler
Request.Handler.AbortException -
Field Summary
Fields inherited from interface Invocable
__nonBlocking, NOOP -
Method Summary
Modifier and TypeMethodDescriptionvoidaddMapping(String pathSpec, WebSocketCreator creator) Maps the givenpathSpecto the creator of WebSocket endpoints.voidaddMapping(PathSpec pathSpec, WebSocketCreator creator) Maps the givenpathSpecto the creator of WebSocket endpoints.voidaddSessionListener(WebSocketSessionListener listener) Register a WebSocketSessionListener with the containerstatic ServerWebSocketContainerReturns theServerWebSocketContainer, ensuring that it is available viaget(Context).static ServerWebSocketContainerensure(Server server, ContextHandler contextHandler) Returns theServerWebSocketContainer, ensuring that it is available viaget(Context).static ServerWebSocketContainerReturns theServerWebSocketContainerpresent 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 implementationintThe input (read from network layer) buffer size.longGet the maximum size of a binary message during parsing.longThe maximum payload size of any WebSocket Frame which can be received.intGet the maximum number of data frames allowed to be waiting to be sent at any one time.longGet the maximum size of a text message during parsing.Get the collection of open Sessions being tracked by this containerintThe output (write to network layer) buffer size.booleanMatches the givenrequestagainst existing WebSocket mappings, upgrading to WebSocket if there is a match.booleanIf true, frames are automatically fragmented to respect the maximum frame size.voidNotify the Session Listeners of an event.booleanRemove a WebSocketSessionListener from the containervoidsetAutoFragment(boolean autoFragment) If set to true, frames are automatically fragmented to respect the maximum frame size.voidsetIdleTimeout(Duration duration) The duration that a websocket may be idle before being closed by the implementationvoidsetInputBufferSize(int size) The input (read from network layer) buffer size.voidsetInvocationType(Invocable.InvocationType invocationType) Sets the invocation type of this container.voidsetMaxBinaryMessageSize(long size) The maximum size of a binary message during parsing/generating.voidsetMaxFrameSize(long maxFrameSize) The maximum payload size of any WebSocket Frame which can be received.voidsetMaxOutgoingFrames(int maxOutgoingFrames) Set the maximum number of data frames allowed to be waiting to be sent at any one time.voidsetMaxTextMessageSize(long size) The maximum size of a text message during parsing/generating.voidsetOutputBufferSize(int size) The output (write to network layer) buffer size.booleanupgrade(WebSocketCreator creator, Request request, Response response, Callback callback) Upgrades the givenrequestwithout matching against the WebSocket mappings.Methods inherited from class 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, updateBeansMethods inherited from class AbstractLifeCycle
getEventListeners, getState, getState, isFailed, isRunning, isStarted, isStarting, isStopped, isStopping, setEventListeners, start, stop, toStringMethods inherited from class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waitMethods inherited from interface Container
getCachedBeans, getEventListenersMethods inherited from interface Dumpable.DumpableContainer
isDumpable
-
Method Details
-
ensure
Returns the
ServerWebSocketContainer, ensuring that it is available viaget(Context).If the
ServerWebSocketContaineris 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
Handlerhierarchy.- Parameters:
server- theServerobject used to lookup common WebSocket componentscontextHandler- theContextHandlerused to store theServerWebSocketContainer- Returns:
- a non-
nullServerWebSocketContainer
-
ensure
Returns the
ServerWebSocketContainer, ensuring that it is available viaget(Context).If the
ServerWebSocketContaineris 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
Handlerhierarchy.- Parameters:
server- theServerobject used to lookup common WebSocket components and store theServerWebSocketContainer- Returns:
- a non-
nullServerWebSocketContainer
-
get
Returns the
ServerWebSocketContainerpresent as the context attribute under the name corresponding to the full qualified name of classWebSocketContainer.- Parameters:
context- theContextto look for the attribute- Returns:
- the
ServerWebSocketContainerstored as an attribute, ornullif no such attribute is present
-
getExecutor
Description copied from interface:WebSocketContainerThe Container provided Executor.- Specified by:
getExecutorin interfaceWebSocketContainer
-
getOpenSessions
Description copied from interface:WebSocketContainerGet the collection of open Sessions being tracked by this container- Specified by:
getOpenSessionsin interfaceWebSocketContainer- Returns:
- the collection of open sessions
-
addSessionListener
Description copied from interface:WebSocketContainerRegister a WebSocketSessionListener with the container- Specified by:
addSessionListenerin interfaceWebSocketContainer- Parameters:
listener- the listener
-
removeSessionListener
Description copied from interface:WebSocketContainerRemove a WebSocketSessionListener from the container- Specified by:
removeSessionListenerin interfaceWebSocketContainer- Parameters:
listener- the listener- Returns:
- true if listener was present and removed
-
notifySessionListeners
Description copied from interface:WebSocketContainerNotify the Session Listeners of an event.- Specified by:
notifySessionListenersin interfaceWebSocketContainer- Parameters:
consumer- the consumer to call for each tracked listener
-
getIdleTimeout
Description copied from interface:ConfigurableThe duration that a websocket may be idle before being closed by the implementation- Specified by:
getIdleTimeoutin interfaceConfigurable- Returns:
- the timeout duration
-
setIdleTimeout
Description copied from interface:ConfigurableThe duration that a websocket may be idle before being closed by the implementation- Specified by:
setIdleTimeoutin interfaceConfigurable- Parameters:
duration- the timeout duration (may not be null or negative)
-
getInputBufferSize
public int getInputBufferSize()Description copied from interface:ConfigurableThe input (read from network layer) buffer size.This is the raw read operation buffer size, before the parsing of the websocket frames.
- Specified by:
getInputBufferSizein interfaceConfigurable- Returns:
- the raw network buffer input size.
-
setInputBufferSize
public void setInputBufferSize(int size) Description copied from interface:ConfigurableThe input (read from network layer) buffer size.- Specified by:
setInputBufferSizein interfaceConfigurable- Parameters:
size- the size in bytes
-
getOutputBufferSize
public int getOutputBufferSize()Description copied from interface:ConfigurableThe 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:
getOutputBufferSizein interfaceConfigurable- Returns:
- the raw network buffer output size.
-
setOutputBufferSize
public void setOutputBufferSize(int size) Description copied from interface:ConfigurableThe output (write to network layer) buffer size.- Specified by:
setOutputBufferSizein interfaceConfigurable- Parameters:
size- the size in bytes
-
getMaxBinaryMessageSize
public long getMaxBinaryMessageSize()Description copied from interface:ConfigurableGet 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:
getMaxBinaryMessageSizein interfaceConfigurable- Returns:
- the maximum size of a binary message
-
setMaxBinaryMessageSize
public void setMaxBinaryMessageSize(long size) Description copied from interface:ConfigurableThe 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:
setMaxBinaryMessageSizein interfaceConfigurable- Parameters:
size- the maximum allowed size of a binary message.
-
getMaxTextMessageSize
public long getMaxTextMessageSize()Description copied from interface:ConfigurableGet 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:
getMaxTextMessageSizein interfaceConfigurable- Returns:
- the maximum size of a text message.
-
setMaxTextMessageSize
public void setMaxTextMessageSize(long size) Description copied from interface:ConfigurableThe 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:
setMaxTextMessageSizein interfaceConfigurable- Parameters:
size- the maximum allowed size of a text message.
-
getMaxFrameSize
public long getMaxFrameSize()Description copied from interface:ConfigurableThe maximum payload size of any WebSocket Frame which can be received.- Specified by:
getMaxFrameSizein interfaceConfigurable- Returns:
- the maximum size of a WebSocket Frame.
-
setMaxFrameSize
public void setMaxFrameSize(long maxFrameSize) Description copied from interface:ConfigurableThe 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:
setMaxFrameSizein interfaceConfigurable- Parameters:
maxFrameSize- the maximum allowed size of a WebSocket Frame.
-
isAutoFragment
public boolean isAutoFragment()Description copied from interface:ConfigurableIf true, frames are automatically fragmented to respect the maximum frame size.- Specified by:
isAutoFragmentin interfaceConfigurable- Returns:
- whether to automatically fragment incoming WebSocket Frames.
-
setAutoFragment
public void setAutoFragment(boolean autoFragment) Description copied from interface:ConfigurableIf set to true, frames are automatically fragmented to respect the maximum frame size.- Specified by:
setAutoFragmentin interfaceConfigurable- Parameters:
autoFragment- whether to automatically fragment incoming WebSocket Frames.
-
getMaxOutgoingFrames
public int getMaxOutgoingFrames()Description copied from interface:ConfigurableGet 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 aWritePendingExceptionbut the connection is not failed and will remain open.- Specified by:
getMaxOutgoingFramesin interfaceConfigurable- Returns:
- the max number of frames.
-
setMaxOutgoingFrames
public void setMaxOutgoingFrames(int maxOutgoingFrames) Description copied from interface:ConfigurableSet 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 aWritePendingExceptionbut the connection is not failed and will remain open.- Specified by:
setMaxOutgoingFramesin interfaceConfigurable- Parameters:
maxOutgoingFrames- the max number of frames.
-
addMapping
Maps the given
pathSpecto the creator of WebSocket endpoints.The
pathSpecformat is that supported byWebSocketMappings.parsePathSpec(String).- Parameters:
pathSpec- thepathSpecto associate to the creatorcreator- the creator of WebSocket endpoints
-
addMapping
Maps the given
pathSpecto the creator of WebSocket endpoints.- Parameters:
pathSpec- thepathSpecto associate to the creatorcreator- the creator of WebSocket endpoints
-
handle
Matches the given
requestagainst 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
trueis returned, a response has been sent to the client and thecallbackhas been completed; either because of a successful WebSocket upgrade, or because an error has occurred.When
falseis returned, a response has not been sent to the client, and thecallbackhas not been completed; typically because the request path does not match any existing WebSocket mappings, so that the request can be handled by otherHandlers.- Specified by:
handlein 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:
truein case of WebSocket upgrades or failures,falseif 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
requestwithout matching against the WebSocket mappings.When
trueis returned, a response has been sent to the client and thecallbackhas been completed; either because of a successful WebSocket upgrade, or because an error has occurred.When
falseis returned, a response has not been sent to the client, and thecallbackhas 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:
truein case of WebSocket upgrades or failures,falseif the request was not upgraded- Throws:
WebSocketException- there is an error during the upgrade- See Also:
-
getInvocationType
- Specified by:
getInvocationTypein interfaceInvocable- Specified by:
getInvocationTypein 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_BLOCKINGwhen 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.BLOCKINGis returned, assuming that application code in the WebSocket endpoint uses blocking APIs.- Parameters:
invocationType- the invocation type of this container
-