Class WebSocketSession

java.lang.Object
org.eclipse.jetty.websocket.common.WebSocketSession
All Implemented Interfaces:
Closeable, AutoCloseable, Dumpable, Configurable, Session

public class WebSocketSession extends Object implements Session, Dumpable
  • Constructor Details

  • Method Details

    • demand

      public void demand()
      Description copied from interface: Session

      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 Session.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 Session.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 Session.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.

      Specified by:
      demand in interface Session
    • sendBinary

      public void sendBinary(ByteBuffer buffer, Callback callback)
      Description copied from interface: Session

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

      Specified by:
      sendBinary in interface Session
      Parameters:
      buffer - the message bytes to send
      callback - callback to notify when the send operation is complete
    • sendPartialBinary

      public void sendPartialBinary(ByteBuffer buffer, boolean last, Callback callback)
      Description copied from interface: Session

      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.

      Specified by:
      sendPartialBinary in interface Session
      Parameters:
      buffer - the frame bytes to send
      last - whether this is the last frame of the message
      callback - callback to notify when the send operation is complete
    • sendText

      public void sendText(String text, Callback callback)
      Description copied from interface: Session

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

      Specified by:
      sendText in interface Session
      Parameters:
      text - the message text to send
      callback - callback to notify when the send operation is complete
    • sendPartialText

      public void sendPartialText(String text, boolean last, Callback callback)
      Description copied from interface: Session

      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.

      Specified by:
      sendPartialText in interface Session
      Parameters:
      text - the frame text to send
      last - whether this is the last frame of the message
      callback - callback to notify when the send operation is complete
    • sendPing

      public void sendPing(ByteBuffer applicationData, Callback callback)
      Description copied from interface: Session

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

      Specified by:
      sendPing in interface Session
      Parameters:
      applicationData - the data to be carried in the PING frame
      callback - callback to notify when the send operation is complete
    • sendPong

      public void sendPong(ByteBuffer applicationData, Callback callback)
      Description copied from interface: Session

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

      Specified by:
      sendPong in interface Session
      Parameters:
      applicationData - the data to be carried in the PONG frame
      callback - callback to notify when the send operation is complete
    • close

      public void close(int statusCode, String reason, Callback callback)
      Description copied from interface: Session

      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.

      Specified by:
      close in interface Session
      Parameters:
      statusCode - the status code
      reason - the (optional) reason
      callback - callback to notify when the send operation is complete
      See Also:
    • getIdleTimeout

      public Duration getIdleTimeout()
      Description copied from interface: Configurable
      The duration that a websocket may be idle before being closed by the implementation
      Specified by:
      getIdleTimeout in interface Configurable
      Returns:
      the timeout duration
    • setIdleTimeout

      public void setIdleTimeout(Duration duration)
      Description copied from interface: Configurable
      The duration that a websocket may be idle before being closed by the implementation
      Specified by:
      setIdleTimeout in interface Configurable
      Parameters:
      duration - the timeout duration (may not be null or negative)
    • getInputBufferSize

      public int getInputBufferSize()
      Description copied from interface: Configurable
      The input (read from network layer) buffer size.

      This is the raw read operation buffer size, before the parsing of the websocket frames.

      Specified by:
      getInputBufferSize in interface Configurable
      Returns:
      the raw network buffer input size.
    • setInputBufferSize

      public void setInputBufferSize(int size)
      Description copied from interface: Configurable
      The input (read from network layer) buffer size.
      Specified by:
      setInputBufferSize in interface Configurable
      Parameters:
      size - the size in bytes
    • getOutputBufferSize

      public int getOutputBufferSize()
      Description copied from interface: Configurable
      The 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:
      getOutputBufferSize in interface Configurable
      Returns:
      the raw network buffer output size.
    • setOutputBufferSize

      public void setOutputBufferSize(int size)
      Description copied from interface: Configurable
      The output (write to network layer) buffer size.
      Specified by:
      setOutputBufferSize in interface Configurable
      Parameters:
      size - the size in bytes
    • getMaxBinaryMessageSize

      public long getMaxBinaryMessageSize()
      Description copied from interface: Configurable
      Get 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:
      getMaxBinaryMessageSize in interface Configurable
      Returns:
      the maximum size of a binary message
    • setMaxBinaryMessageSize

      public void setMaxBinaryMessageSize(long size)
      Description copied from interface: Configurable
      The 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:
      setMaxBinaryMessageSize in interface Configurable
      Parameters:
      size - the maximum allowed size of a binary message.
    • getMaxTextMessageSize

      public long getMaxTextMessageSize()
      Description copied from interface: Configurable
      Get 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:
      getMaxTextMessageSize in interface Configurable
      Returns:
      the maximum size of a text message.
    • setMaxTextMessageSize

      public void setMaxTextMessageSize(long size)
      Description copied from interface: Configurable
      The 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:
      setMaxTextMessageSize in interface Configurable
      Parameters:
      size - the maximum allowed size of a text message.
    • getMaxFrameSize

      public long getMaxFrameSize()
      Description copied from interface: Configurable
      The maximum payload size of any WebSocket Frame which can be received.
      Specified by:
      getMaxFrameSize in interface Configurable
      Returns:
      the maximum size of a WebSocket Frame.
    • setMaxFrameSize

      public void setMaxFrameSize(long maxFrameSize)
      Description copied from interface: Configurable
      The 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:
      setMaxFrameSize in interface Configurable
      Parameters:
      maxFrameSize - the maximum allowed size of a WebSocket Frame.
    • isAutoFragment

      public boolean isAutoFragment()
      Description copied from interface: Configurable
      If true, frames are automatically fragmented to respect the maximum frame size.
      Specified by:
      isAutoFragment in interface Configurable
      Returns:
      whether to automatically fragment incoming WebSocket Frames.
    • setAutoFragment

      public void setAutoFragment(boolean autoFragment)
      Description copied from interface: Configurable
      If set to true, frames are automatically fragmented to respect the maximum frame size.
      Specified by:
      setAutoFragment in interface Configurable
      Parameters:
      autoFragment - whether to automatically fragment incoming WebSocket Frames.
    • getMaxOutgoingFrames

      public int getMaxOutgoingFrames()
      Description copied from interface: Configurable
      Get 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 a WritePendingException but the connection is not failed and will remain open.
      Specified by:
      getMaxOutgoingFrames in interface Configurable
      Returns:
      the max number of frames.
    • setMaxOutgoingFrames

      public void setMaxOutgoingFrames(int maxOutgoingFrames)
      Description copied from interface: Configurable
      Set 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 a WritePendingException but the connection is not failed and will remain open.
      Specified by:
      setMaxOutgoingFrames in interface Configurable
      Parameters:
      maxOutgoingFrames - the max number of frames.
    • getProtocolVersion

      public String getProtocolVersion()
      Description copied from interface: Session

      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.

      Specified by:
      getProtocolVersion in interface Session
      Returns:
      the WebSocket protocol version
    • isOpen

      public boolean isOpen()
      Specified by:
      isOpen in interface Session
      Returns:
      whether the session is open
    • isSecure

      public boolean isSecure()
      Specified by:
      isSecure in interface Session
      Returns:
      whether the underlying socket is using a secure transport
    • disconnect

      public void disconnect()
      Description copied from interface: Session

      Abruptly closes the WebSocket connection without sending a CLOSE frame.

      Specified by:
      disconnect in interface Session
      See Also:
    • getLocalSocketAddress

      public SocketAddress getLocalSocketAddress()
      Specified by:
      getLocalSocketAddress in interface Session
      Returns:
      the local SocketAddress for the connection, if available
    • getRemoteSocketAddress

      public SocketAddress getRemoteSocketAddress()
      Specified by:
      getRemoteSocketAddress in interface Session
      Returns:
      the remote SocketAddress for the connection, if available
    • getUpgradeRequest

      public UpgradeRequest getUpgradeRequest()
      Specified by:
      getUpgradeRequest in interface Session
      Returns:
      the UpgradeRequest used to create this session
    • getUpgradeResponse

      public UpgradeResponse getUpgradeResponse()
      Specified by:
      getUpgradeResponse in interface Session
      Returns:
      the UpgradeResponse used to create this session
    • getCoreSession

      public CoreSession getCoreSession()
    • dump

      public void dump(Appendable out, String indent) throws IOException
      Description copied from interface: Dumpable
      Dump this object (and children) into an Appendable using the provided indent after any new lines. The indent should not be applied to the first object dumped.
      Specified by:
      dump in interface Dumpable
      Parameters:
      out - The appendable to dump to
      indent - The indent to apply after any new lines.
      Throws:
      IOException - if unable to write to Appendable
    • dumpSelf

      public String dumpSelf()
      Description copied from interface: Dumpable
      The description of this/self found in the dump. Allows for alternative representation of Object other then .toString() where the long form output of toString() is represented in a cleaner way within the dump infrastructure.
      Specified by:
      dumpSelf in interface Dumpable
      Returns:
      the representation of self
    • toString

      public String toString()
      Overrides:
      toString in class Object