Top Description Methods
javax.websocket

public Interface Session

extends Closeable
Imports
java.io.Closeable, .IOException, java.net.URI, java.security.Principal, java.util.List, .Map, .Set

A Web Socket session represents a conversation between two web socket endpoints. As soon as the websocket handshake completes successfully, the web socket implementation provides the endpoint an open websocket session. The endpoint can then register interest in incoming messages that are part of this newly created session by providing a MessageHandler to the session, and can send messages to the other end of the conversation by means of the RemoteEndpoint object obtained from this session.

Once the session is closed, it is no longer valid for use by applications. Calling any of its methods (with the exception of the close() methods) once the session has been closed will result in an java.lang.IllegalStateException being thrown. Developers should retrieve any information from the session during the Endpoint#onClose method. Following the convention of java.io.Closeable calling the Session close() methods after the Session has been closed has no effect.

Session objects may be called by multiple threads. Implementations must ensure the integrity of the mutable properties of the session under such circumstances.

Author
dannycoward

Method Summary

Modifier and TypeMethod and Description
public void
addMessageHandler(MessageHandler
the MessageHandler to be added.
handler
)

Register to handle to incoming messages in this conversation.

public <T> void
addMessageHandler(Class<T>
type of the message processed by message handler to be registered.
clazz
,
MessageHandler.Whole<T>
whole message handler to be added.
handler
)

Register to handle to incoming messages in this conversation.

public <T> void
addMessageHandler(Class<T>
type of the message processed by message handler to be registered.
clazz
,
MessageHandler.Partial<T>
partial message handler to be added.
handler
)

Register to handle to incoming messages in this conversation.

public void
close()

Redeclares java.io.Closeable.close.

Close the current conversation with a normal status code and no reason phrase.
public void
close(CloseReason
the reason for the closure.
closeReason
)

Close the current conversation, giving a reason for the closure.

public RemoteEndpoint.Async

Returns:

the remote endpoint.
getAsyncRemote
()

Return a reference a RemoteEndpoint object representing the peer of this conversation that is able to send messages asynchronously to the peer.

public RemoteEndpoint.Basic

Returns:

the remote endpoint.
getBasicRemote
()

Return a reference a RemoteEndpoint object representing the peer of this conversation that is able to send messages synchronously to the peer.

public WebSocketContainer

Returns:

the container.
getContainer
()

Return the container that this session is part of.

public String

Returns:

the unique identifier for this session instance.
getId
()

Returns a string containing the unique identifier assigned to this session.

public int

Returns:

the maximum binary message size that can be buffered.
getMaxBinaryMessageBufferSize
()

The maximum length of incoming binary messages that this Session can buffer.

public long

Returns:

the timeout in milliseconds.
getMaxIdleTimeout
()

Return the number of milliseconds before this conversation may be closed by the container if it is inactive, i.e. no messages are either sent or received in that time.

public int

Returns:

the maximum text message size that can be buffered.
getMaxTextMessageBufferSize
()

The maximum length of incoming text messages that this Session can buffer.

public Set<MessageHandler>

Returns:

the set of message handlers.
getMessageHandlers
()

Return an unmodifiable copy of the set of MessageHandlers for this Session.

public List<Extension>

Returns:

the negotiated extensions.
getNegotiatedExtensions
()

Return the list of extensions currently in use for this conversation.

public String

Returns:

the negotiated subprotocol, or the empty string if there isn't one.
getNegotiatedSubprotocol
()

Return the sub protocol agreed during the websocket handshake for this conversation.

public Set<Session>

Returns:

the set of sessions, open at the time of return.
getOpenSessions
()

Return a copy of the Set of all the open web socket sessions that represent connections to the same endpoint to which this session represents a connection.

public Map<String, String>

Returns:

the unmodifiable map of path parameters. The key of the map is the parameter name, the values in the map are the parameter values.
getPathParameters
()

Return a map of the path parameter names and values used associated with the request this session was opened under.

public String

Returns:

the protocol version.
getProtocolVersion
()

Returns the version of the websocket protocol currently being used.

public String

Returns:

the query string
getQueryString
()

Return the query string associated with the request this session was opened under.

public Map<String, List<String>>

Returns:

the unmodifiable map of the request parameters.
getRequestParameterMap
()

Return the request parameters associated with the request this session was opened under.

public URI

Returns:

the request URI.
getRequestURI
()

Return the URI under which this session was opened, including the query string if there is one.

public Principal

Returns:

the user principal.
getUserPrincipal
()

Return the authenticated user for this Session or null if no user is authenticated for this session.

public Map<String, Object>

Returns:

an editable Map of application data.
getUserProperties
()

While the session is open, this method returns a Map that the developer may use to store application specific information relating to this session instance.

public boolean

Returns:

whether the session is active.
isOpen
()

Return true if and only if the underlying socket is open.

public boolean

Returns:

whether its using a secure transport.
isSecure
()

Return true if and only if the underlying socket is using a secure transport.

public void
removeMessageHandler(MessageHandler
the handler to be removed.
handler
)

Remove the given MessageHandler from the set belonging to this session.

public void
setMaxBinaryMessageBufferSize(int
the maximum length.
length
)

Sets the maximum length of incoming binary messages that this Session can buffer.

public void
setMaxIdleTimeout(long
the number of milliseconds.
milliseconds
)

Set the non-zero number of milliseconds before this session will be closed by the container if it is inactive, ie no messages are either sent or received.

public void
setMaxTextMessageBufferSize(int
the maximum length.
length
)

Sets the maximum length of incoming text messages that this Session can buffer.

Method Detail

addMessageHandlerback to summary
public void addMessageHandler(MessageHandler handler) throws IllegalStateException

Register to handle to incoming messages in this conversation. A maximum of one message handler per native websocket message type (text, binary, pong) may be added to each Session. I.e. a maximum of one message handler to handle incoming text messages a maximum of one message handler for handling incoming binary messages, and a maximum of one for handling incoming pong messages. For further details of which message handlers handle which of the native websocket message types please see MessageHandler.Whole and MessageHandler.Partial. Adding more than one of any one type will result in a runtime exception.

This method is not safe to use unless you are providing an anonymous class derived directly from javax.websocket.MessageHandler.Whole or javax.websocket.MessageHandler.Partial. In all other cases (Lambda Expressions, more complex inheritance or generic type arrangements), one of the following methods have to be used: addMessageHandler(Class, javax.websocket.MessageHandler.Whole) or addMessageHandler(Class, javax.websocket.MessageHandler.Partial).

Parameters
handler:MessageHandler

the MessageHandler to be added.

Exceptions
IllegalStateException:
if there is already a MessageHandler registered for the same native websocket message type as this handler.
addMessageHandlerback to summary
public <T> void addMessageHandler(Class<T> clazz, MessageHandler.Whole<T> handler)

Register to handle to incoming messages in this conversation. A maximum of one message handler per native websocket message type (text, binary, pong) may be added to each Session. I.e. a maximum of one message handler to handle incoming text messages a maximum of one message handler for handling incoming binary messages, and a maximum of one for handling incoming pong messages. For further details of which message handlers handle which of the native websocket message types please see MessageHandler.Whole and MessageHandler.Partial. Adding more than one of any one type will result in a runtime exception.

Parameters
clazz:Class<T>

type of the message processed by message handler to be registered.

handler:MessageHandler.Whole<T>

whole message handler to be added.

Exceptions
IllegalStateException:
if there is already a MessageHandler registered for the same native websocket message type as this handler.
Since
1.1
addMessageHandlerback to summary
public <T> void addMessageHandler(Class<T> clazz, MessageHandler.Partial<T> handler)

Register to handle to incoming messages in this conversation. A maximum of one message handler per native websocket message type (text, binary, pong) may be added to each Session. I.e. a maximum of one message handler to handle incoming text messages a maximum of one message handler for handling incoming binary messages, and a maximum of one for handling incoming pong messages. For further details of which message handlers handle which of the native websocket message types please see MessageHandler.Whole and MessageHandler.Partial. Adding more than one of any one type will result in a runtime exception.

Parameters
clazz:Class<T>

type of the message processed by message handler to be registered.

handler:MessageHandler.Partial<T>

partial message handler to be added.

Exceptions
IllegalStateException:
if there is already a MessageHandler registered for the same native websocket message type as this handler.
Since
1.1
closeback to summary
public void close() throws IOException

Redeclares java.io.Closeable.close.

Close the current conversation with a normal status code and no reason phrase.

Annotations
@Override
Exceptions
IOException:
if there was a connection error closing the connection.
closeback to summary
public void close(CloseReason closeReason) throws IOException

Close the current conversation, giving a reason for the closure. The close call causes the implementation to attempt notify the client of the close as soon as it can. This may cause the sending of unsent messages immediately prior to the close notification. After the close notification has been sent the implementation notifies the endpoint's onClose method. Note the websocket specification defines the acceptable uses of status codes and reason phrases. If the application cannot determine a suitable close code to use for the closeReason, it is recommended to use CloseReason.CloseCodes#NO_STATUS_CODE.

Parameters
closeReason:CloseReason

the reason for the closure.

Exceptions
IOException:
if there was a connection error closing the connection
getAsyncRemoteback to summary
public RemoteEndpoint.Async getAsyncRemote()

Return a reference a RemoteEndpoint object representing the peer of this conversation that is able to send messages asynchronously to the peer.

Returns:RemoteEndpoint.Async

the remote endpoint.

getBasicRemoteback to summary
public RemoteEndpoint.Basic getBasicRemote()

Return a reference a RemoteEndpoint object representing the peer of this conversation that is able to send messages synchronously to the peer.

Returns:RemoteEndpoint.Basic

the remote endpoint.

getContainerback to summary
public WebSocketContainer getContainer()

Return the container that this session is part of.

Returns:WebSocketContainer

the container.

getIdback to summary
public String getId()

Returns a string containing the unique identifier assigned to this session. The identifier is assigned by the web socket implementation and is implementation dependent.

Returns:String

the unique identifier for this session instance.

getMaxBinaryMessageBufferSizeback to summary
public int getMaxBinaryMessageBufferSize()

The maximum length of incoming binary messages that this Session can buffer. If the implementation receives a binary message that it cannot buffer because it is too large, it must close the session with a close code of CloseReason.CloseCodes#TOO_BIG.

Returns:int

the maximum binary message size that can be buffered.

getMaxIdleTimeoutback to summary
public long getMaxIdleTimeout()

Return the number of milliseconds before this conversation may be closed by the container if it is inactive, i.e. no messages are either sent or received in that time.

Returns:long

the timeout in milliseconds.

getMaxTextMessageBufferSizeback to summary
public int getMaxTextMessageBufferSize()

The maximum length of incoming text messages that this Session can buffer. If the implementation receives a text message that it cannot buffer because it is too large, it must close the session with a close code of CloseReason.CloseCodes#TOO_BIG.

Returns:int

the maximum text message size that can be buffered.

getMessageHandlersback to summary
public Set<MessageHandler> getMessageHandlers()

Return an unmodifiable copy of the set of MessageHandlers for this Session.

Returns:Set<MessageHandler>

the set of message handlers.

getNegotiatedExtensionsback to summary
public List<Extension> getNegotiatedExtensions()

Return the list of extensions currently in use for this conversation.

Returns:List<Extension>

the negotiated extensions.

getNegotiatedSubprotocolback to summary
public String getNegotiatedSubprotocol()

Return the sub protocol agreed during the websocket handshake for this conversation.

Returns:String

the negotiated subprotocol, or the empty string if there isn't one.

getOpenSessionsback to summary
public Set<Session> getOpenSessions()

Return a copy of the Set of all the open web socket sessions that represent connections to the same endpoint to which this session represents a connection. The Set includes the session this method is called on. These sessions may not still be open at any point after the return of this method. For example, iterating over the set at a later time may yield one or more closed sessions. Developers should use session.isOpen() to check.

Returns:Set<Session>

the set of sessions, open at the time of return.

getPathParametersback to summary
public Map<String, String> getPathParameters()

Return a map of the path parameter names and values used associated with the request this session was opened under.

Returns:Map<String, String>

the unmodifiable map of path parameters. The key of the map is the parameter name, the values in the map are the parameter values.

getProtocolVersionback to summary
public String getProtocolVersion()

Returns the version of the websocket protocol currently being used. This is taken as the value of the Sec-WebSocket-Version header used in the opening handshake. i.e. "13".

Returns:String

the protocol version.

getQueryStringback to summary
public String getQueryString()

Return the query string associated with the request this session was opened under.

Returns:String

the query string

getRequestParameterMapback to summary
public Map<String, List<String>> getRequestParameterMap()

Return the request parameters associated with the request this session was opened under.

Returns:Map<String, List<String>>

the unmodifiable map of the request parameters.

getRequestURIback to summary
public URI getRequestURI()

Return the URI under which this session was opened, including the query string if there is one.

Returns:URI

the request URI.

getUserPrincipalback to summary
public Principal getUserPrincipal()

Return the authenticated user for this Session or null if no user is authenticated for this session.

Returns:Principal

the user principal.

getUserPropertiesback to summary
public Map<String, Object> getUserProperties()

While the session is open, this method returns a Map that the developer may use to store application specific information relating to this session instance. The developer may retrieve information from this Map at any time between the opening of the session and during the onClose() method. But outside that time, any information stored using this Map may no longer be kept by the container. Web socket applications running on distributed implementations of the web container should make any application specific objects stored here java.io.Serializable, or the object may not be recreated after a failover.

Returns:Map<String, Object>

an editable Map of application data.

isOpenback to summary
public boolean isOpen()

Return true if and only if the underlying socket is open.

Returns:boolean

whether the session is active.

isSecureback to summary
public boolean isSecure()

Return true if and only if the underlying socket is using a secure transport.

Returns:boolean

whether its using a secure transport.

removeMessageHandlerback to summary
public void removeMessageHandler(MessageHandler handler)

Remove the given MessageHandler from the set belonging to this session. This method may block if the given handler is processing a message until it is no longer in use.

Parameters
handler:MessageHandler

the handler to be removed.

setMaxBinaryMessageBufferSizeback to summary
public void setMaxBinaryMessageBufferSize(int length)

Sets the maximum length of incoming binary messages that this Session can buffer.

Parameters
length:int

the maximum length.

setMaxIdleTimeoutback to summary
public void setMaxIdleTimeout(long milliseconds)

Set the non-zero number of milliseconds before this session will be closed by the container if it is inactive, ie no messages are either sent or received. A value that is 0 or negative indicates the session will never timeout due to inactivity.

Parameters
milliseconds:long

the number of milliseconds.

setMaxTextMessageBufferSizeback to summary
public void setMaxTextMessageBufferSize(int length)

Sets the maximum length of incoming text messages that this Session can buffer.

Parameters
length:int

the maximum length.