Top Description Inners Methods
org.eclipse.jetty.websocket.api

public Interface Session

extends Configurable, Closeable
Imports
java.io.Closeable, java.net.SocketAddress, java.nio.ByteBuffer, org.eclipse.jetty.websocket.api.annotations.WebSocket

Session represents an active link of communication with a remote WebSocket endpoint.

Session APIs can be used to configure the various parameters that control the behavior of the WebSocket communication, such as setMaxTextMessageSize(long), and to send WebSocket frames or messages to the other peer.

The passive link of communication that receives WebSocket events is Listener.

Nested and Inner Type Summary

Modifier and TypeClass and Description
public static interface
Session.Listener

The passive link of communication with a remote WebSocket endpoint.

Method Summary

Modifier and TypeMethod and Description
public default void
close()

Implements java.io.Closeable.close.

Equivalent to close(StatusCode.NORMAL, null, Callback.NOOP).

public void
close(int
the status code
statusCode
,
String
the (optional) reason
reason
,
Callback
callback to notify when the send operation is complete
callback
)

Sends a websocket CLOSE frame, with status code and reason, notifying the given callback when the frame send is completed, either successfully or with a failure.

public void
demand()

Explicitly demands for WebSocket events.

public void
disconnect()

Abruptly closes the WebSocket connection without sending a CLOSE frame.

public SocketAddress
getLocalSocketAddress()

Returns: the local SocketAddress for the connection, if available

public String

Returns:

the WebSocket protocol version
getProtocolVersion
()

Returns the version of the WebSocket protocol currently being used.

public SocketAddress
getRemoteSocketAddress()

Returns: the remote SocketAddress for the connection, if available

public UpgradeRequest
getUpgradeRequest()

Returns: the UpgradeRequest used to create this session

public UpgradeResponse
getUpgradeResponse()

Returns: the UpgradeResponse used to create this session

public boolean
isOpen()

Returns: whether the session is open

public boolean
isSecure()

Returns: whether the underlying socket is using a secure transport

public void
sendBinary(ByteBuffer
the message bytes to send
buffer
,
Callback
callback to notify when the send operation is complete
callback
)

Initiates the asynchronous send of a BINARY message, notifying the given callback when the message send is completed, either successfully or with a failure.

public void
sendPartialBinary(ByteBuffer
the frame bytes to send
buffer
,
boolean
whether this is the last frame of the message
last
,
Callback
callback to notify when the send operation is complete
callback
)

Initiates the asynchronous send of a BINARY frame, possibly part of a larger binary message, notifying the given callback when the frame send is completed, either successfully or with a failure.

public void
sendPartialText(String
the frame text to send
text
,
boolean
whether this is the last frame of the message
last
,
Callback
callback to notify when the send operation is complete
callback
)

Initiates the asynchronous send of a TEXT frame, possibly part of a larger binary message, notifying the given callback when the frame send is completed, either successfully or with a failure.

public void
sendPing(ByteBuffer
the data to be carried in the PING frame
applicationData
,
Callback
callback to notify when the send operation is complete
callback
)

Initiates the asynchronous send of a PING frame, notifying the given callback when the frame send is completed, either successfully or with a failure.

public void
sendPong(ByteBuffer
the data to be carried in the PONG frame
applicationData
,
Callback
callback to notify when the send operation is complete
callback
)

Initiates the asynchronous send of a PONG frame, notifying the given callback when the frame send is completed, either successfully or with a failure.

public void
sendText(String
the message text to send
text
,
Callback
callback to notify when the send operation is complete
callback
)

Initiates the asynchronous send of a TEXT message, notifying the given callback when the message send is completed, either successfully or with a failure.

Inherited from org.eclipse.jetty.websocket.api.Configurable:
getIdleTimeoutgetInputBufferSizegetMaxBinaryMessageSizegetMaxFrameSizegetMaxOutgoingFramesgetMaxTextMessageSizegetOutputBufferSizeisAutoFragmentsetAutoFragmentsetIdleTimeoutsetInputBufferSizesetMaxBinaryMessageSizesetMaxFrameSizesetMaxOutgoingFramessetMaxTextMessageSizesetOutputBufferSize

Method Detail

closeback to summary
public default void close()

Implements java.io.Closeable.close.

Equivalent to close(StatusCode.NORMAL, null, Callback.NOOP).

Annotations
@Override
See Also
close(int, String, Callback), disconnect()
closeback to summary
public void close(int statusCode, String reason, Callback callback)

Sends a websocket CLOSE frame, with status code and reason, notifying the given callback when the frame send is completed, either successfully or with a failure.

Parameters
statusCode:int

the status code

reason:String

the (optional) reason

callback:Callback

callback to notify when the send operation is complete

See Also
StatusCode, close(), disconnect()
demandback to summary
public void demand()

Explicitly demands for WebSocket events.

This method should be called only when the WebSocket endpoint is not demanding automatically, as defined by WebSocket#autoDemand() and Listener.AutoDemanding.

In general, invoking this method results in a listener method or an annotated method to be called when the corresponding event is ready to be delivered.

For WebSocket endpoints that wants to receive frame events (for example by overriding Listener#onWebSocketFrame(Frame, Callback)), invoking this method will result in a frame event being delivered to the listener/annotated method when a new frame is received.

For WebSocket endpoints that want to receive whole message events (for example by overriding Listener#onWebSocketText(String)), invoking this method will result in a message event being delivered to the listener/annotated method when a new message is received. The implementation will automatically demand for more frames until a whole message is assembled and then deliver the whole message as event.

Note that even when the WebSocket endpoint is interested in whole messages, calling this method is necessary not only to possibly receive the next whole message, but also to receive control frames (such as PING or CLOSE frames). Failing to call this method after receiving a whole message results in the CLOSE frame event to not be processed, and therefore for the endpoint to not notice when the other peer closed the WebSocket communication.

Exceptions
IllegalStateException:
if the WebSocket endpoint is auto-demanding
disconnectback to summary
public void disconnect()

Abruptly closes the WebSocket connection without sending a CLOSE frame.

See Also
close(int, String, Callback)
getLocalSocketAddressback to summary
public SocketAddress getLocalSocketAddress()
Returns:SocketAddress

the local SocketAddress for the connection, if available

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 upgrade request.

Returns:String

the WebSocket protocol version

getRemoteSocketAddressback to summary
public SocketAddress getRemoteSocketAddress()
Returns:SocketAddress

the remote SocketAddress for the connection, if available

getUpgradeRequestback to summary
public UpgradeRequest getUpgradeRequest()
Returns:UpgradeRequest

the UpgradeRequest used to create this session

getUpgradeResponseback to summary
public UpgradeResponse getUpgradeResponse()
Returns:UpgradeResponse

the UpgradeResponse used to create this session

isOpenback to summary
public boolean isOpen()
Returns:boolean

whether the session is open

isSecureback to summary
public boolean isSecure()
Returns:boolean

whether the underlying socket is using a secure transport

sendBinaryback to summary
public void sendBinary(ByteBuffer buffer, Callback callback)

Initiates the asynchronous send of a BINARY message, notifying the given callback when the message send is completed, either successfully or with a failure.

Parameters
buffer:ByteBuffer

the message bytes to send

callback:Callback

callback to notify when the send operation is complete

sendPartialBinaryback to summary
public void sendPartialBinary(ByteBuffer buffer, boolean last, Callback callback)

Initiates the asynchronous send of a BINARY frame, possibly part of a larger binary message, notifying the given callback when the frame send is completed, either successfully or with a failure.

Non-final frames must be sent with the parameter last=false. The final frame must be sent with last=true.

Parameters
buffer:ByteBuffer

the frame bytes to send

last:boolean

whether this is the last frame of the message

callback:Callback

callback to notify when the send operation is complete

sendPartialTextback to summary
public void sendPartialText(String text, boolean last, Callback callback)

Initiates the asynchronous send of a TEXT frame, possibly part of a larger binary message, notifying the given callback when the frame send is completed, either successfully or with a failure.

Non-final frames must be sent with the parameter last=false. The final frame must be sent with last=true.

Parameters
text:String

the frame text to send

last:boolean

whether this is the last frame of the message

callback:Callback

callback to notify when the send operation is complete

sendPingback to summary
public void sendPing(ByteBuffer applicationData, Callback callback)

Initiates the asynchronous send of a PING frame, notifying the given callback when the frame send is completed, either successfully or with a failure.

Parameters
applicationData:ByteBuffer

the data to be carried in the PING frame

callback:Callback

callback to notify when the send operation is complete

sendPongback to summary
public void sendPong(ByteBuffer applicationData, Callback callback)

Initiates the asynchronous send of a PONG frame, notifying the given callback when the frame send is completed, either successfully or with a failure.

Parameters
applicationData:ByteBuffer

the data to be carried in the PONG frame

callback:Callback

callback to notify when the send operation is complete

sendTextback to summary
public void sendText(String text, Callback callback)

Initiates the asynchronous send of a TEXT message, notifying the given callback when the message send is completed, either successfully or with a failure.

Parameters
text:String

the message text to send

callback:Callback

callback to notify when the send operation is complete

org.eclipse.jetty.websocket.api back to summary

public Interface Session.Listener

Known Direct Subinterfaces
org.eclipse.jetty.websocket.api.Session.Listener.AutoDemanding
Known Direct Implementers
org.eclipse.jetty.websocket.api.Session.Listener.Abstract

The passive link of communication with a remote WebSocket endpoint.

Applications provide WebSocket endpoints that implement this interface to receive WebSocket events from the remote peer, and can use Session for configuration and to send WebSocket frames or messages to the other peer.

Nested and Inner Type Summary

Modifier and TypeClass and Description
public abstract static class
public abstract static class
public static interface
Session.Listener.AutoDemanding

Tag interface that signals that the WebSocket endpoint is demanding for WebSocket frames automatically.

Method Summary

Modifier and TypeMethod and Description
public default void
onWebSocketBinary(ByteBuffer
the raw payload array received
payload
,
Callback callback)

A WebSocket BINARY message has been received.

public default void
onWebSocketClose(int
the close status code
statusCode
,
String
the optional reason for the close
reason
)

The WebSocket Session has been closed.

public default void
onWebSocketError(Throwable
the error that occurred
cause
)

A WebSocket error has occurred during the processing of WebSocket frames.

public default void
onWebSocketFrame(Frame
the received frame
frame
,
Callback callback)

A WebSocket frame has been received.

public default void
onWebSocketOpen(Session
the WebSocket session
session
)

A WebSocket Session has opened successfully and is ready to be used.

public default void
onWebSocketPartialBinary(ByteBuffer
the BINARY frame payload
payload
,
boolean
whether this is the last frame
last
,
Callback
the callback to complete when the payload has been processed
callback
)

A WebSocket BINARY (or associated CONTINUATION) frame has been received.

public default void
onWebSocketPartialText(String
the text message payload

Note that due to framing, there is a above average chance of any UTF8 sequences being split on the border between two frames will result in either the previous frame, or the next frame having an invalid UTF8 sequence, but the combined frames having a valid UTF8 sequence.

The String being provided here will not end in a split UTF8 sequence. Instead this partial sequence will be held over until the next frame is received.

payload
,
boolean
whether this is the last frame
last
)

A WebSocket TEXT (or associated CONTINUATION) frame has been received.

public default void
onWebSocketPing(ByteBuffer
the PING payload
payload
)

A WebSocket PING frame has been received.

public default void
onWebSocketPong(ByteBuffer
the PONG payload
payload
)

A WebSocket PONG frame has been received.

public default void
onWebSocketText(String
the text payload
message
)

A WebSocket TEXT message has been received.

Method Detail

onWebSocketBinaryback to summary
public default void onWebSocketBinary(ByteBuffer payload, Callback callback)

A WebSocket BINARY message has been received.

Parameters
payload:ByteBuffer

the raw payload array received

onWebSocketCloseback to summary
public default void onWebSocketClose(int statusCode, String reason)

The WebSocket Session has been closed.

Parameters
statusCode:int

the close status code

reason:String

the optional reason for the close

onWebSocketErrorback to summary
public default void onWebSocketError(Throwable cause)

A WebSocket error has occurred during the processing of WebSocket frames.

Usually errors occurs from bad or malformed incoming packets, for example text frames that do not contain UTF-8 bytes, frames that are too big, or other violations of the WebSocket specification.

The WebSocket Session will be closed, but applications may explicitly close the Session providing a different status code or reason.

Parameters
cause:Throwable

the error that occurred

onWebSocketFrameback to summary
public default void onWebSocketFrame(Frame frame, Callback callback)

A WebSocket frame has been received.

The received frames may be control frames such as PING, PONG or CLOSE, or data frames either BINARY or TEXT.

Parameters
frame:Frame

the received frame

onWebSocketOpenback to summary
public default void onWebSocketOpen(Session session)

A WebSocket Session has opened successfully and is ready to be used.

Applications can store the given Session as a field so it can be used to send messages back to the other peer.

Parameters
session:Session

the WebSocket session

onWebSocketPartialBinaryback to summary
public default void onWebSocketPartialBinary(ByteBuffer payload, boolean last, Callback callback)

A WebSocket BINARY (or associated CONTINUATION) frame has been received.

The ByteBuffer is read-only, and will be recycled when the callback is completed.

Parameters
payload:ByteBuffer

the BINARY frame payload

last:boolean

whether this is the last frame

callback:Callback

the callback to complete when the payload has been processed

onWebSocketPartialTextback to summary
public default void onWebSocketPartialText(String payload, boolean last)

A WebSocket TEXT (or associated CONTINUATION) frame has been received.

Parameters
payload:String

the text message payload

Note that due to framing, there is a above average chance of any UTF8 sequences being split on the border between two frames will result in either the previous frame, or the next frame having an invalid UTF8 sequence, but the combined frames having a valid UTF8 sequence.

The String being provided here will not end in a split UTF8 sequence. Instead this partial sequence will be held over until the next frame is received.

last:boolean

whether this is the last frame

onWebSocketPingback to summary
public default void onWebSocketPing(ByteBuffer payload)

A WebSocket PING frame has been received.

Parameters
payload:ByteBuffer

the PING payload

onWebSocketPongback to summary
public default void onWebSocketPong(ByteBuffer payload)

A WebSocket PONG frame has been received.

Parameters
payload:ByteBuffer

the PONG payload

onWebSocketTextback to summary
public default void onWebSocketText(String message)

A WebSocket TEXT message has been received.

Parameters
message:String

the text payload

org.eclipse.jetty.websocket.api back to summary

public abstract Class Session.Listener.Abstract

extends Object
implements Listener
Class Inheritance
All Implemented Interfaces
org.eclipse.jetty.websocket.api.Session.Listener
Known Direct Subclasses
org.eclipse.jetty.websocket.api.Session.Listener.AbstractAutoDemanding

Field Summary

Modifier and TypeField and Description
private volatile Session

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public Session
public boolean
public void
onWebSocketOpen(Session
the WebSocket session
session
)

Overrides default org.eclipse.jetty.websocket.api.Session.Listener.onWebSocketOpen.

A WebSocket Session has opened successfully and is ready to be used.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

sessionback to summary
private volatile Session session

Constructor Detail

Abstractback to summary
public Abstract()

Method Detail

getSessionback to summary
public Session getSession()
isOpenback to summary
public boolean isOpen()
onWebSocketOpenback to summary
public void onWebSocketOpen(Session session)

Overrides default org.eclipse.jetty.websocket.api.Session.Listener.onWebSocketOpen.

Doc from org.eclipse.jetty.websocket.api.Session.Listener.onWebSocketOpen.

A WebSocket Session has opened successfully and is ready to be used.

Applications can store the given Session as a field so it can be used to send messages back to the other peer.

Parameters
session:Session

the WebSocket session

Annotations
@Override
org.eclipse.jetty.websocket.api back to summary

public abstract Class Session.Listener.AbstractAutoDemanding

extends Abstract
implements AutoDemanding
Class Inheritance
All Implemented Interfaces
org.eclipse.jetty.websocket.api.Session.Listener.AutoDemanding, org.eclipse.jetty.websocket.api.Session.Listener

Constructor Summary

AccessConstructor and Description
public

Constructor Detail

AbstractAutoDemandingback to summary
public AbstractAutoDemanding()
org.eclipse.jetty.websocket.api back to summary

public Interface Session.Listener.AutoDemanding

extends Listener
Known Direct Implementers
org.eclipse.jetty.websocket.api.Session.Listener.AbstractAutoDemanding

Tag interface that signals that the WebSocket endpoint is demanding for WebSocket frames automatically.

See Also
WebSocket#autoDemand()