Interface Session
- All Superinterfaces:
AutoCloseable, Closeable
Represents a WebSocket session, which is the WebSocket connection between a client and a server.
A session is created when a WebSocket connection is established and is used to send and receive
messages.
-
Method Summary
Modifier and TypeMethodDescriptionvoidaddMessageHandler(MessageHandler handler) Registers aMessageHandlerfor incoming messages.<T> voidaddMessageHandler(Class<T> clazz, MessageHandler.Partial<T> handler) Registers aMessageHandlerfor partial incoming messages.<T> voidaddMessageHandler(Class<T> clazz, MessageHandler.Whole<T> handler) Registers aMessageHandlerfor whole incoming messages.voidclose()Close the connection to the remote end point using the codeCloseReason.CloseCodes.NORMAL_CLOSUREand an empty reason phrase.voidclose(CloseReason closeReason) Close the connection to the remote end point using the specified code and reason phrase.Returns the asynchronous remote endpoint for this session, used for non-blocking message sending.Returns the synchronous remote endpoint for this session, used for blocking message sending.Get the container that created this session.getId()Provides a unique identifier for the session.intGet the current maximum buffer size for binary messages.longGet the idle timeout for this session.intGet the maximum buffer size for text messages.Returns the set of message handlers currently registered on this session.Returns the list of extensions negotiated for this session.Returns the sub-protocol negotiated for this session, ornullif no sub-protocol was negotiated.Obtain the set of open sessions associated with the same local endpoint as this session.Returns the path parameters extracted from the request URI using the endpoint's URI template.Returns the version of the WebSocket protocol in use for this session.Returns the query string from the request that initiated this WebSocket session.Returns the query parameters from the request that initiated this WebSocket session.Returns the URI of the request that initiated this WebSocket session.Returns the principal of the user associated with this session, ornullif the user is not authenticated.Returns a map of user properties associated with this session.booleanisOpen()Returns whether this session is currently open.booleanisSecure()Returns whether this session is using a secure connection (wss://).voidremoveMessageHandler(MessageHandler listener) Removes the specified message handler from this session.voidsetMaxBinaryMessageBufferSize(int max) Set the current maximum buffer size for binary messages.voidsetMaxIdleTimeout(long timeout) Set the idle timeout for this session.voidsetMaxTextMessageBufferSize(int max) Set the maximum buffer size for text messages.
-
Method Details
-
getContainer
WebSocketContainer getContainer()Get the container that created this session.- Returns:
- the container that created this session.
-
addMessageHandler
Registers aMessageHandlerfor incoming messages. Only oneMessageHandlermay be registered for each message type (text, binary, pong). The message type will be derived at runtime from the providedMessageHandlerinstance. It is not always possible to do this so it is better to useaddMessageHandler(Class, jakarta.websocket.MessageHandler.Partial)oraddMessageHandler(Class, jakarta.websocket.MessageHandler.Whole).- Parameters:
handler- The message handler for an incoming message- Throws:
IllegalStateException- If a message handler has already been registered for the associated message type
-
getMessageHandlers
Set<MessageHandler> getMessageHandlers()Returns the set of message handlers currently registered on this session.- Returns:
- The set of registered message handlers
-
removeMessageHandler
Removes the specified message handler from this session.- Parameters:
listener- The message handler to remove
-
getProtocolVersion
String getProtocolVersion()Returns the version of the WebSocket protocol in use for this session.- Returns:
- The protocol version
-
getNegotiatedSubprotocol
String getNegotiatedSubprotocol()Returns the sub-protocol negotiated for this session, ornullif no sub-protocol was negotiated.- Returns:
- The negotiated sub-protocol
-
getNegotiatedExtensions
-
isSecure
boolean isSecure()Returns whether this session is using a secure connection (wss://).- Returns:
trueif the connection is secure
-
isOpen
boolean isOpen()Returns whether this session is currently open.- Returns:
trueif the session is open
-
getMaxIdleTimeout
long getMaxIdleTimeout()Get the idle timeout for this session.- Returns:
- The current idle timeout for this session in milliseconds. Zero or negative values indicate an infinite timeout.
-
setMaxIdleTimeout
void setMaxIdleTimeout(long timeout) Set the idle timeout for this session.- Parameters:
timeout- The new idle timeout for this session in milliseconds. Zero or negative values indicate an infinite timeout.
-
setMaxBinaryMessageBufferSize
void setMaxBinaryMessageBufferSize(int max) Set the current maximum buffer size for binary messages.- Parameters:
max- The new maximum buffer size in bytes
-
getMaxBinaryMessageBufferSize
int getMaxBinaryMessageBufferSize()Get the current maximum buffer size for binary messages.- Returns:
- The current maximum buffer size in bytes
-
setMaxTextMessageBufferSize
void setMaxTextMessageBufferSize(int max) Set the maximum buffer size for text messages.- Parameters:
max- The new maximum buffer size in characters.
-
getMaxTextMessageBufferSize
int getMaxTextMessageBufferSize()Get the maximum buffer size for text messages.- Returns:
- The maximum buffer size in characters.
-
getAsyncRemote
RemoteEndpoint.Async getAsyncRemote()Returns the asynchronous remote endpoint for this session, used for non-blocking message sending.- Returns:
- The asynchronous remote endpoint
-
getBasicRemote
RemoteEndpoint.Basic getBasicRemote()Returns the synchronous remote endpoint for this session, used for blocking message sending.- Returns:
- The synchronous remote endpoint
-
getId
String getId()Provides a unique identifier for the session. This identifier should not be relied upon to be generated from a secure random source.- Returns:
- A unique identifier for the session.
-
close
Close the connection to the remote end point using the codeCloseReason.CloseCodes.NORMAL_CLOSUREand an empty reason phrase.- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Throws:
IOException- if an I/O error occurs while the WebSocket session is being closed.
-
close
Close the connection to the remote end point using the specified code and reason phrase.- Parameters:
closeReason- The reason the WebSocket session is being closed.- Throws:
IOException- if an I/O error occurs while the WebSocket session is being closed.
-
getRequestURI
URI getRequestURI()Returns the URI of the request that initiated this WebSocket session.- Returns:
- The request URI
-
getRequestParameterMap
-
getQueryString
String getQueryString()Returns the query string from the request that initiated this WebSocket session.- Returns:
- The query string
-
getPathParameters
-
getUserProperties
-
getUserPrincipal
Principal getUserPrincipal()Returns the principal of the user associated with this session, ornullif the user is not authenticated.- Returns:
- The user principal
-
getOpenSessions
-
addMessageHandler
<T> void addMessageHandler(Class<T> clazz, MessageHandler.Partial<T> handler) throws IllegalStateException Registers aMessageHandlerfor partial incoming messages. Only oneMessageHandlermay be registered for each message type (text or binary, pong messages are never presented as partial messages).- Type Parameters:
T- The type of message that the given handler is intended for- Parameters:
clazz- The Class that implements Thandler- The message handler for an incoming message- Throws:
IllegalStateException- If a message handler has already been registered for the associated message type- Since:
- WebSocket 1.1
-
addMessageHandler
<T> void addMessageHandler(Class<T> clazz, MessageHandler.Whole<T> handler) throws IllegalStateException Registers aMessageHandlerfor whole incoming messages. Only oneMessageHandlermay be registered for each message type (text, binary, pong).- Type Parameters:
T- The type of message that the given handler is intended for- Parameters:
clazz- The Class that implements Thandler- The message handler for an incoming message- Throws:
IllegalStateException- If a message handler has already been registered for the associated message type- Since:
- WebSocket 1.1
-