Interface WebSocketSession


public interface WebSocketSession
One established WebSocket connection.

A session belongs to one connection and is owned by the transport that created it. Calls are safe from any thread; send(com.jsgalactic.axiom.websocket.WebSocketMessage), ping(byte[]) and close() throw IllegalStateException once the session has ended, so a handler must not retain a session beyond WebSocketHandler.onClose(com.jsgalactic.axiom.websocket.WebSocketSession, int, java.lang.String). Writes from a handler may be buffered: a peer that stops reading makes the transport stop reading frames until its writes drain, and a connection that stays stalled is subject to the listener's idle and response timeouts. request() and execution() give the immutable metadata of the GET that opened this session; they remain readable after close.

  • Method Summary

    Modifier and Type
    Method
    Description
    void
    Closes the session normally, with code 1000 and an empty reason.
    void
    close(int code, String reason)
    Closes the session with a protocol status code and a reason.
    Returns the immutable identity metadata of the handshake execution.
    boolean
    Returns whether the session can still send and receive.
    void
    ping(byte[] bytes)
    Sends a ping with an application payload.
    Returns the remote socket address when the transport has one.
    Returns the immutable handshake request.
    void
    Sends a text or binary message, preserving its kind.
    void
    sendBinary(byte[] bytes)
    Sends a binary message from a copy of the supplied bytes.
    void
    Sends a text message.
  • Method Details

    • send

      void send(WebSocketMessage message)
      Sends a text or binary message, preserving its kind.
      Parameters:
      message - message to send; copied by the transport
      Throws:
      IllegalStateException - if the session is closed
    • sendText

      void sendText(String text)
      Sends a text message.
      Parameters:
      text - text to send
      Throws:
      IllegalStateException - if the session is closed
    • sendBinary

      void sendBinary(byte[] bytes)
      Sends a binary message from a copy of the supplied bytes.
      Parameters:
      bytes - bytes to send; copied
      Throws:
      IllegalStateException - if the session is closed
    • ping

      void ping(byte[] bytes)
      Sends a ping with an application payload. A peer that answers with a pong does not invoke WebSocketHandler.onMessage(com.jsgalactic.axiom.websocket.WebSocketSession, com.jsgalactic.axiom.websocket.WebSocketMessage); pongs are handled by the transport.
      Parameters:
      bytes - ping payload; copied
      Throws:
      IllegalStateException - if the session is closed
    • close

      void close()
      Closes the session normally, with code 1000 and an empty reason. The close callback runs once, whether the peer, the application or the listener ended the session.
    • close

      void close(int code, String reason)
      Closes the session with a protocol status code and a reason.
      Parameters:
      code - a valid RFC 6455 close code; 1005 and 1006 are reserved and rejected
      reason - reason text, at most 123 UTF-8 bytes; may be empty
      Throws:
      IllegalArgumentException - if the code is reserved or the reason is too long
      IllegalStateException - if the session is closed
    • isOpen

      boolean isOpen()
      Returns whether the session can still send and receive.
      Returns:
      true until the close callback begins
    • remoteAddress

      Optional<InetSocketAddress> remoteAddress()
      Returns the remote socket address when the transport has one. An in-memory test session has no socket and returns empty.
      Returns:
      peer address, if available
    • request

      Request request()
      Returns the immutable handshake request. Its headers include the upgrade fields the peer sent.
      Returns:
      request that opened the session
    • execution

      ExecutionContext execution()
      Returns the immutable identity metadata of the handshake execution.
      Returns:
      execution context of the handshake