Package org.apache.tomcat.websocket
Class WsSession
java.lang.Object
org.apache.tomcat.websocket.WsSession
- All Implemented Interfaces:
Session
,Closeable
,AutoCloseable
-
Constructor Summary
ConstructorDescriptionWsSession
(ClientEndpointHolder clientEndpointHolder, WsRemoteEndpointImplBase wsRemoteEndpoint, WsWebSocketContainer wsWebSocketContainer, List<Extension> negotiatedExtensions, String subProtocol, Map<String, String> pathParameters, boolean secure, ClientEndpointConfig clientEndpointConfig) Creates a new WebSocket session for communication between the provided client and remote end points.WsSession
(WsRemoteEndpointImplBase wsRemoteEndpoint, WsWebSocketContainer wsWebSocketContainer, URI requestUri, Map<String, List<String>> requestParameterMap, String queryString, Principal userPrincipal, String httpSessionId, List<Extension> negotiatedExtensions, String subProtocol, Map<String, String> pathParameters, boolean secure, ServerEndpointConfig serverEndpointConfig) Creates a new WebSocket session for communication between the provided server and remote end points. -
Method Summary
Modifier and TypeMethodDescriptionvoid
addMessageHandler
(MessageHandler listener) Registers aMessageHandler
for incoming messages.<T> void
addMessageHandler
(Class<T> clazz, MessageHandler.Partial<T> handler) Registers aMessageHandler
for partial incoming messages.<T> void
addMessageHandler
(Class<T> clazz, MessageHandler.Whole<T> handler) Registers aMessageHandler
for whole incoming messages.protected static void
appendCloseReasonWithTruncation
(ByteBuffer msg, String reason) Use protected so unit tests can access this method directly.protected void
protected void
void
close()
Close the connection to the remote end point using the codeCloseReason.CloseCodes.NORMAL_CLOSURE
and an empty reason phrase.void
close
(CloseReason closeReason) Close the connection to the remote end point using the specified code and reason phrase.void
doClose
(CloseReason closeReasonMessage, CloseReason closeReasonLocal) WebSocket 1.0.void
doClose
(CloseReason closeReasonMessage, CloseReason closeReasonLocal, boolean closeSocket) WebSocket 1.0.protected MessageHandler
Get the container that created this session.getId()
Provides a unique identifier for the session.getLocal()
int
Get the current maximum buffer size for binary messages.long
Get the idle timeout for this session.int
Get the maximum buffer size for text messages.Obtain the set of open sessions associated with the same local endpoint as this session.protected MessageHandler.Whole
<PongMessage> protected long
protected MessageHandler
boolean
isClosed()
boolean
isOpen()
boolean
isSecure()
void
onClose
(CloseReason closeReason) Called when a close message is received.protected void
registerFuture
(org.apache.tomcat.websocket.FutureToSendHandler f2sh) Make the session aware of aFutureToSendHandler
that will need to be forcibly closed if the session closes before theFutureToSendHandler
completes.void
removeMessageHandler
(MessageHandler listener) void
resume()
Resumes the reading of the incoming messages.void
setMaxBinaryMessageBufferSize
(int max) Set the current maximum buffer size for binary messages.void
setMaxIdleTimeout
(long timeout) Set the idle timeout for this session.void
setMaxTextMessageBufferSize
(int max) Set the maximum buffer size for text messages.void
suspend()
Suspends the reading of the incoming messages.protected void
unregisterFuture
(org.apache.tomcat.websocket.FutureToSendHandler f2sh) Remove aFutureToSendHandler
from the set of tracked instances.protected void
protected void
-
Constructor Details
-
WsSession
public WsSession(ClientEndpointHolder clientEndpointHolder, WsRemoteEndpointImplBase wsRemoteEndpoint, WsWebSocketContainer wsWebSocketContainer, List<Extension> negotiatedExtensions, String subProtocol, Map<String, String> pathParameters, boolean secure, ClientEndpointConfig clientEndpointConfig) throws DeploymentExceptionCreates a new WebSocket session for communication between the provided client and remote end points. The result ofThread.getContextClassLoader()
at the time this constructor is called will be used when callingEndpoint.onClose(Session, CloseReason)
.- Parameters:
clientEndpointHolder
- The end point managed by this codewsRemoteEndpoint
- The other / remote end pointwsWebSocketContainer
- The container that created this sessionnegotiatedExtensions
- The agreed extensions to use for this sessionsubProtocol
- The agreed sub-protocol to use for this sessionpathParameters
- The path parameters associated with the request that initiated this session ornull
if this is a client sessionsecure
- Was this session initiated over a secure connection?clientEndpointConfig
- The configuration information for the client end point- Throws:
DeploymentException
- if an invalid encode is specified
-
WsSession
public WsSession(WsRemoteEndpointImplBase wsRemoteEndpoint, WsWebSocketContainer wsWebSocketContainer, URI requestUri, Map<String, List<String>> requestParameterMap, String queryString, Principal userPrincipal, String httpSessionId, List<Extension> negotiatedExtensions, String subProtocol, Map<String, throws DeploymentExceptionString> pathParameters, boolean secure, ServerEndpointConfig serverEndpointConfig) Creates a new WebSocket session for communication between the provided server and remote end points. The result ofThread.getContextClassLoader()
at the time this constructor is called will be used when callingEndpoint.onClose(Session, CloseReason)
.- Parameters:
wsRemoteEndpoint
- The other / remote end pointwsWebSocketContainer
- The container that created this sessionrequestUri
- The URI used to connect to this end point ornull
if this is a client sessionrequestParameterMap
- The parameters associated with the request that initiated this session ornull
if this is a client sessionqueryString
- The query string associated with the request that initiated this session ornull
if this is a client sessionuserPrincipal
- The principal associated with the request that initiated this session ornull
if this is a client sessionhttpSessionId
- The HTTP session ID associated with the request that initiated this session ornull
if this is a client sessionnegotiatedExtensions
- The agreed extensions to use for this sessionsubProtocol
- The agreed sub-protocol to use for this sessionpathParameters
- The path parameters associated with the request that initiated this session ornull
if this is a client sessionsecure
- Was this session initiated over a secure connection?serverEndpointConfig
- The configuration information for the server end point- Throws:
DeploymentException
- if an invalid encode is specified
-
-
Method Details
-
getInstanceManager
-
getContainer
Description copied from interface:jakarta.websocket.Session
Get the container that created this session.- Specified by:
getContainer
in interfaceSession
- Returns:
- the container that created this session.
-
addMessageHandler
Description copied from interface:jakarta.websocket.Session
Registers aMessageHandler
for incoming messages. Only oneMessageHandler
may be registered for each message type (text, binary, pong). The message type will be derived at runtime from the providedMessageHandler
instance. It is not always possible to do this so it is better to useSession.addMessageHandler(Class, jakarta.websocket.MessageHandler.Partial)
orSession.addMessageHandler(Class, jakarta.websocket.MessageHandler.Whole)
.- Specified by:
addMessageHandler
in interfaceSession
- Parameters:
listener
- The message handler for a incoming message
-
addMessageHandler
public <T> void addMessageHandler(Class<T> clazz, MessageHandler.Partial<T> handler) throws IllegalStateException Description copied from interface:jakarta.websocket.Session
Registers aMessageHandler
for partial incoming messages. Only oneMessageHandler
may be registered for each message type (text or binary, pong messages are never presented as partial messages).- Specified by:
addMessageHandler
in interfaceSession
- 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 a incoming message- Throws:
IllegalStateException
- If a message handler has already been registered for the associated message type
-
addMessageHandler
public <T> void addMessageHandler(Class<T> clazz, MessageHandler.Whole<T> handler) throws IllegalStateException Description copied from interface:jakarta.websocket.Session
Registers aMessageHandler
for whole incoming messages. Only oneMessageHandler
may be registered for each message type (text, binary, pong).- Specified by:
addMessageHandler
in interfaceSession
- 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 a incoming message- Throws:
IllegalStateException
- If a message handler has already been registered for the associated message type
-
getMessageHandlers
- Specified by:
getMessageHandlers
in interfaceSession
-
removeMessageHandler
- Specified by:
removeMessageHandler
in interfaceSession
-
getProtocolVersion
- Specified by:
getProtocolVersion
in interfaceSession
-
getNegotiatedSubprotocol
- Specified by:
getNegotiatedSubprotocol
in interfaceSession
-
getNegotiatedExtensions
- Specified by:
getNegotiatedExtensions
in interfaceSession
-
isSecure
-
isOpen
-
isClosed
public boolean isClosed() -
getMaxIdleTimeout
public long getMaxIdleTimeout()Description copied from interface:jakarta.websocket.Session
Get the idle timeout for this session.- Specified by:
getMaxIdleTimeout
in interfaceSession
- Returns:
- The current idle timeout for this session in milliseconds. Zero or negative values indicate an infinite timeout.
-
setMaxIdleTimeout
public void setMaxIdleTimeout(long timeout) Description copied from interface:jakarta.websocket.Session
Set the idle timeout for this session.- Specified by:
setMaxIdleTimeout
in interfaceSession
- Parameters:
timeout
- The new idle timeout for this session in milliseconds. Zero or negative values indicate an infinite timeout.
-
setMaxBinaryMessageBufferSize
public void setMaxBinaryMessageBufferSize(int max) Description copied from interface:jakarta.websocket.Session
Set the current maximum buffer size for binary messages.- Specified by:
setMaxBinaryMessageBufferSize
in interfaceSession
- Parameters:
max
- The new maximum buffer size in bytes
-
getMaxBinaryMessageBufferSize
public int getMaxBinaryMessageBufferSize()Description copied from interface:jakarta.websocket.Session
Get the current maximum buffer size for binary messages.- Specified by:
getMaxBinaryMessageBufferSize
in interfaceSession
- Returns:
- The current maximum buffer size in bytes
-
setMaxTextMessageBufferSize
public void setMaxTextMessageBufferSize(int max) Description copied from interface:jakarta.websocket.Session
Set the maximum buffer size for text messages.- Specified by:
setMaxTextMessageBufferSize
in interfaceSession
- Parameters:
max
- The new maximum buffer size in characters.
-
getMaxTextMessageBufferSize
public int getMaxTextMessageBufferSize()Description copied from interface:jakarta.websocket.Session
Get the maximum buffer size for text messages.- Specified by:
getMaxTextMessageBufferSize
in interfaceSession
- Returns:
- The maximum buffer size in characters.
-
getOpenSessions
Description copied from interface:jakarta.websocket.Session
Obtain the set of open sessions associated with the same local endpoint as this session.- Specified by:
getOpenSessions
in interfaceSession
- Returns:
- The set of currently open sessions for the local endpoint that this session is associated with.
-
getAsyncRemote
- Specified by:
getAsyncRemote
in interfaceSession
-
getBasicRemote
- Specified by:
getBasicRemote
in interfaceSession
-
close
Description copied from interface:jakarta.websocket.Session
Close the connection to the remote end point using the codeCloseReason.CloseCodes.NORMAL_CLOSURE
and an empty reason phrase.- Specified by:
close
in interfaceAutoCloseable
- Specified by:
close
in interfaceCloseable
- Specified by:
close
in interfaceSession
- Throws:
IOException
- if an I/O error occurs while the WebSocket session is being closed.
-
close
Description copied from interface:jakarta.websocket.Session
Close the connection to the remote end point using the specified code and reason phrase.- Specified by:
close
in interfaceSession
- 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.
-
doClose
WebSocket 1.0. Section 2.1.5. Need internal close method as spec requires that the local endpoint receives a 1006 on timeout.- Parameters:
closeReasonMessage
- The close reason to pass to the remote endpointcloseReasonLocal
- The close reason to pass to the local endpoint
-
doClose
public void doClose(CloseReason closeReasonMessage, CloseReason closeReasonLocal, boolean closeSocket) WebSocket 1.0. Section 2.1.5. Need internal close method as spec requires that the local endpoint receives a 1006 on timeout.- Parameters:
closeReasonMessage
- The close reason to pass to the remote endpointcloseReasonLocal
- The close reason to pass to the local endpointcloseSocket
- Should the socket be closed immediately rather than waiting for the server to respond
-
onClose
Called when a close message is received. Should only ever happen once. Also called after a protocol error when the ProtocolHandler needs to force the closing of the connection.- Parameters:
closeReason
- The reason contained within the received close message.
-
getSessionCloseTimeout
protected long getSessionCloseTimeout() -
checkCloseTimeout
protected void checkCloseTimeout() -
appendCloseReasonWithTruncation
Use protected so unit tests can access this method directly.- Parameters:
msg
- The messagereason
- The reason
-
registerFuture
protected void registerFuture(org.apache.tomcat.websocket.FutureToSendHandler f2sh) Make the session aware of aFutureToSendHandler
that will need to be forcibly closed if the session closes before theFutureToSendHandler
completes.- Parameters:
f2sh
- The handler
-
unregisterFuture
protected void unregisterFuture(org.apache.tomcat.websocket.FutureToSendHandler f2sh) Remove aFutureToSendHandler
from the set of tracked instances.- Parameters:
f2sh
- The handler
-
getRequestURI
- Specified by:
getRequestURI
in interfaceSession
-
getRequestParameterMap
-
getQueryString
- Specified by:
getQueryString
in interfaceSession
-
getUserPrincipal
- Specified by:
getUserPrincipal
in interfaceSession
-
getUserPrincipalInternal
-
getPathParameters
- Specified by:
getPathParameters
in interfaceSession
-
getId
Description copied from interface:jakarta.websocket.Session
Provides a unique identifier for the session. This identifier should not be relied upon to be generated from a secure random source. -
getUserProperties
- Specified by:
getUserProperties
in interfaceSession
-
getLocal
-
getHttpSessionId
-
getTextMessageHandler
-
getBinaryMessageHandler
-
getPongMessageHandler
-
updateLastActiveRead
protected void updateLastActiveRead() -
updateLastActiveWrite
protected void updateLastActiveWrite() -
checkExpiration
protected void checkExpiration() -
suspend
public void suspend()Suspends the reading of the incoming messages. -
resume
public void resume()Resumes the reading of the incoming messages.
-