Class WebsocketConnection

All Implemented Interfaces:
Proxy

@Generated("org.javagi.JavaGI") public final class WebsocketConnection extends GObject

The WebSocket Protocol

Provides support for the WebSocket protocol.

To connect to a WebSocket server, create a Session and call Session.websocketConnectAsync(Message, String, String[], int, Cancellable, AsyncReadyCallback). To accept WebSocket connections, create a Server and add a handler to it with Server.addWebsocketHandler(String, String, String[], ServerWebsocketCallback).

(Lower-level support is available via Soup.websocketClientPrepareHandshake(Message, String, String[], TypeClass[]) and Soup.websocketClientVerifyHandshake(Message, TypeClass[], Out), for handling the client side of the WebSocket handshake, and Soup.websocketServerProcessHandshake(ServerMessage, String, String[], TypeClass[], Out) for handling the server side.)

WebsocketConnection handles the details of WebSocket communication. You can use sendText(String) and sendBinary(byte[]) to send data, and the WebsocketConnection::message signal to receive data. (WebsocketConnection currently only supports asynchronous I/O.)

  • Constructor Details

    • WebsocketConnection

      public WebsocketConnection(MemorySegment address)
      Create a WebsocketConnection instance for the provided memory address.
      Parameters:
      address - the memory address of the native object
    • WebsocketConnection

      public WebsocketConnection(IOStream stream, Uri uri, WebsocketConnectionType type, @Nullable String origin, @Nullable String protocol, List<WebsocketExtension> extensions)

      Creates a WebsocketConnection on stream with the given active extensions.

      This should be called after completing the handshake to begin using the WebSocket protocol.

      Parameters:
      stream - a GIOStream connected to the WebSocket server
      uri - the URI of the connection
      type - the type of connection (client/side)
      origin - the Origin of the client
      protocol - the subprotocol in use
      extensions - a GList of SoupWebsocketExtension objects
    • WebsocketConnection

      public WebsocketConnection()
      Create a new WebsocketConnection.
  • Method Details

    • getType

      public static @Nullable Type getType()
      Get the GType of the WebsocketConnection class.
      Returns:
      the GType
    • getMemoryLayout

      public static MemoryLayout getMemoryLayout()
      The memory layout of the native struct.
      Returns:
      the memory layout
    • asParent

      protected WebsocketConnection asParent()
      Return this instance as if it were its parent type. Comparable to the Java super keyword, but ensures the parent typeclass is also used in native code.
      Overrides:
      asParent in class GObject
      Returns:
      the instance as if it were its parent type
    • close

      public void close(short code, @Nullable String data)

      Close the connection in an orderly fashion.

      Note that until the WebsocketConnection::closed signal fires, the connection is not yet completely closed. The close message is not even sent until the main loop runs.

      The code and data are sent to the peer along with the close request. If code is WebsocketCloseCode.NO_STATUS a close message with no body (without code and data) is sent. Note that the data must be UTF-8 valid.

      Parameters:
      code - close code
      data - close data
    • getCloseCode

      public short getCloseCode()

      Get the close code received from the WebSocket peer.

      This only becomes valid once the WebSocket is in the WebsocketState.CLOSED state. The value will often be in the WebsocketCloseCode enumeration, but may also be an application defined close code.

      Returns:
      the close code or zero.
    • getCloseData

      public String getCloseData()

      Get the close data received from the WebSocket peer.

      This only becomes valid once the WebSocket is in the WebsocketState.CLOSED state. The data may be freed once the main loop is run, so copy it if you need to keep it around.

      Returns:
      the close data or null
    • getConnectionType

      public WebsocketConnectionType getConnectionType()
      Get the connection type (client/server) of the connection.
      Returns:
      the connection type
    • getExtensions

      public List<WebsocketExtension> getExtensions()
      Get the extensions chosen via negotiation with the peer.
      Returns:
      a GList of SoupWebsocketExtension objects
    • getIoStream

      public IOStream getIoStream()
      Get the I/O stream the WebSocket is communicating over.
      Returns:
      the WebSocket's I/O stream.
    • getKeepaliveInterval

      public int getKeepaliveInterval()
      Gets the keepalive interval in seconds or 0 if disabled.
      Returns:
      the keepalive interval.
    • getKeepalivePongTimeout

      public int getKeepalivePongTimeout()
      Gets the keepalive pong timeout in seconds or 0 if disabled.
      Returns:
      the keepalive pong timeout.
      Since:
      3.6
    • getMaxIncomingPayloadSize

      public long getMaxIncomingPayloadSize()
      Gets the maximum payload size allowed for incoming packets.
      Returns:
      the maximum payload size.
    • getOrigin

      public @Nullable String getOrigin()
      Get the origin of the WebSocket.
      Returns:
      the origin
    • getProtocol

      public @Nullable String getProtocol()
      Get the protocol chosen via negotiation with the peer.
      Returns:
      the chosen protocol
    • getState

      public WebsocketState getState()
      Get the current state of the WebSocket.
      Returns:
      the state
    • getUri

      public Uri getUri()

      Get the URI of the WebSocket.

      For servers this represents the address of the WebSocket, and for clients it is the address connected to.

      Returns:
      the URI
    • sendBinary

      public void sendBinary(@Nullable byte @Nullable [] data)

      Send a binary message to the peer.

      If length is 0, data may be null.

      The message is queued to be sent and will be sent when the main loop is run.

      Parameters:
      data - the message contents
    • sendMessage

      public void sendMessage(WebsocketDataType type, byte[] message)

      Send a message of the given type to the peer. Note that this method, allows to send text messages containing null characters.

      The message is queued to be sent and will be sent when the main loop is run.

      Parameters:
      type - the type of message contents
      message - the message data as GBytes
    • sendText

      public void sendText(String text)

      Send a null-terminated text (UTF-8) message to the peer.

      If you need to send text messages containing null characters use sendMessage(WebsocketDataType, byte[]) instead.

      The message is queued to be sent and will be sent when the main loop is run.

      Parameters:
      text - the message contents
    • setKeepaliveInterval

      public void setKeepaliveInterval(int interval)

      Sets the interval in seconds on when to send a ping message which will serve as a keepalive message.

      If set to 0 the keepalive message is disabled.

      Parameters:
      interval - the interval to send a ping message or 0 to disable it
    • setKeepalivePongTimeout

      public void setKeepalivePongTimeout(int pongTimeout)

      Set the timeout in seconds for when the absence of a pong from a keepalive ping is assumed to be caused by a faulty connection.

      If set to 0 then the absence of pongs from keepalive pings is ignored.

      Parameters:
      pongTimeout - the timeout in seconds
      Since:
      3.6
    • setMaxIncomingPayloadSize

      public void setMaxIncomingPayloadSize(long maxIncomingPayloadSize)

      Sets the maximum payload size allowed for incoming packets.

      It does not limit the outgoing packet size.

      Parameters:
      maxIncomingPayloadSize - the maximum payload size
    • onClosed

      Emitted when the connection has completely closed.

      This happens either due to an orderly close from the peer, one initiated via close(short, String) or a fatal error condition that caused a close.

      This signal will be emitted once.

      Parameters:
      handler - the signal handler
      Returns:
      a signal handler ID to keep track of the signal connection
      See Also:
    • emitClosed

      public void emitClosed()
      Emits the "closed" signal. See onClosed(WebsocketConnection.ClosedCallback).
    • onClosing

      This signal will be emitted during an orderly close.
      Parameters:
      handler - the signal handler
      Returns:
      a signal handler ID to keep track of the signal connection
      See Also:
    • emitClosing

      public void emitClosing()
      Emits the "closing" signal. See onClosing(WebsocketConnection.ClosingCallback).
    • onError

      Emitted when an error occurred on the WebSocket.

      This may be fired multiple times. Fatal errors will be followed by the WebsocketConnection::closed signal being emitted.

      Parameters:
      handler - the signal handler
      Returns:
      a signal handler ID to keep track of the signal connection
      See Also:
    • emitError

      public void emitError(@Nullable GError error)
      Emits the "error" signal. See onError(WebsocketConnection.ErrorCallback).
    • onMessage

      Emitted when we receive a message from the peer.

      As a convenience, the message data will always be null-terminated, but the NUL byte will not be included in the length count.

      Parameters:
      handler - the signal handler
      Returns:
      a signal handler ID to keep track of the signal connection
      See Also:
    • emitMessage

      public void emitMessage(int type, byte @Nullable [] message)
      Emits the "message" signal. See onMessage(WebsocketConnection.MessageCallback).
    • onPong

      Emitted when we receive a Pong frame (solicited or unsolicited) from the peer.

      As a convenience, the message data will always be null-terminated, but the NUL byte will not be included in the length count.

      Parameters:
      handler - the signal handler
      Returns:
      a signal handler ID to keep track of the signal connection
      See Also:
    • emitPong

      public void emitPong(byte @Nullable [] message)
      Emits the "pong" signal. See onPong(WebsocketConnection.PongCallback).
    • builder

      public static WebsocketConnection.Builder<? extends WebsocketConnection.Builder> builder()
      A WebsocketConnection.Builder object constructs a WebsocketConnection with the specified properties. Use the various set...() methods to set properties, and finish construction with WebsocketConnection.Builder.build().
      Returns:
      the builder object