Interface WebSocket

All Superinterfaces:
org.teavm.jso.JSObject

public interface WebSocket extends org.teavm.jso.JSObject
One end of a WebSocket.

The only connection a client can open to a Worker that is not a single request. A Worker accepts one by answering a request with status 101 and the other half of a pair; a Durable Object accepts one into its own set, which is what lets several clients reach the same object.

 WebSocketPair pair = WebSocketPair.create();
 state.acceptWebSocket(pair.server());
 return Bytebox.upgrade(pair.client());

Messages arrive on the object's handler rather than through a listener, because a Worker has no ambient loop to run one in.

Since:
1.0.0
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final int
    The application's own error, and the highest code an application may send.
    static final int
    The endpoint is going away.
    static final int
    Normal closure: the purpose the connection was opened for is finished.
    static final int
    A message the endpoint cannot accept, which is the code to use for a protocol error.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    Attaches state that survives hibernation.
    Returns what attach(dev.gmitch215.bytebox.js.TSObject) stored, or null.
    default void
    Closes the connection normally.
    void
    close(int code, String reason)
    Closes the connection.
    int
    Returns the ready state.
    default boolean
    Returns whether the connection can still carry a message.
    default void
    send(byte[] message)
    Sends bytes.
    void
    send(String message)
    Sends text.
    void
    send(org.teavm.jso.typedarrays.ArrayBuffer message)
    Sends bytes.
    default void
    sendJson(TSObject message)
    Sends a value as JSON.

    Methods inherited from interface org.teavm.jso.JSObject

    cast
  • Field Details

    • NORMAL

      static final int NORMAL
      Normal closure: the purpose the connection was opened for is finished.
      See Also:
    • GOING_AWAY

      static final int GOING_AWAY
      The endpoint is going away.
      See Also:
    • UNACCEPTABLE

      static final int UNACCEPTABLE
      A message the endpoint cannot accept, which is the code to use for a protocol error.
      See Also:
    • APPLICATION_ERROR

      static final int APPLICATION_ERROR
      The application's own error, and the highest code an application may send.
      See Also:
  • Method Details

    • send

      void send(String message)
      Sends text.
      Parameters:
      message - the text
    • send

      void send(org.teavm.jso.typedarrays.ArrayBuffer message)
      Sends bytes.
      Parameters:
      message - the bytes
    • send

      default void send(byte[] message)
      Sends bytes.
      Parameters:
      message - the bytes
    • sendJson

      default void sendJson(TSObject message)
      Sends a value as JSON.
      Parameters:
      message - the value
    • close

      void close(int code, String reason)
      Closes the connection.
      Parameters:
      code - why, one of the constants here or an application code from 4000 to 4999
      reason - a description, at most 123 bytes once encoded
    • close

      default void close()
      Closes the connection normally.
    • attach

      void attach(TSObject value)
      Attaches state that survives hibernation.

      A hibernating Durable Object drops its memory and runs its constructor again when a message arrives, so anything the handler needs about this connection has to live on the connection. At most 2 KB once serialised.

      Parameters:
      value - the state
    • attachment

      TSObject attachment()
      Returns what attach(dev.gmitch215.bytebox.js.TSObject) stored, or null.
      Returns:
      what attach(dev.gmitch215.bytebox.js.TSObject) stored, or null
    • getReadyState

      int getReadyState()
      Returns the ready state.

      0 connecting, 1 open, 2 closing, 3 closed.

      Returns:
      the ready state
    • isOpen

      default boolean isOpen()
      Returns whether the connection can still carry a message.
      Returns:
      whether the connection can still carry a message