Class WebsocketConnection
- All Implemented Interfaces:
Proxy
Provides support for the WebSocket protocol.
To connect to a WebSocket server, create a Session and call
Session.websocketConnectAsync(org.gnome.soup.Message, java.lang.String, java.lang.String[], int, org.gnome.gio.Cancellable, org.gnome.gio.AsyncReadyCallback). To accept WebSocket
connections, create a Server and add a handler to it with
Server.addWebsocketHandler(java.lang.String, java.lang.String, java.lang.String[], org.gnome.soup.ServerWebsocketCallback).
(Lower-level support is available via
Soup.websocketClientPrepareHandshake(org.gnome.soup.Message, java.lang.String, java.lang.String[], org.gnome.gobject.TypeClass[]) and
Soup.websocketClientVerifyHandshake(org.gnome.soup.Message, org.gnome.gobject.TypeClass[], org.javagi.base.Out<org.gnome.glib.List<org.gnome.soup.WebsocketExtension>>), for handling the client side of the
WebSocket handshake, and Soup.websocketServerProcessHandshake(org.gnome.soup.ServerMessage, java.lang.String, java.lang.String[], org.gnome.gobject.TypeClass[], org.javagi.base.Out<org.gnome.glib.List<org.gnome.soup.WebsocketExtension>>) for
handling the server side.)
SoupWebsocketConnection handles the details of WebSocket communication. You
can use sendText(java.lang.String) and
sendBinary(byte[]) to send data, and the
WebsocketConnection::message signal to receive data.
(SoupWebsocketConnection currently only supports asynchronous I/O.)
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classInner class implementing a builder pattern to construct a GObject with properties.static interfaceFunctional interface declaration of theClosedCallbackcallback.static interfaceFunctional interface declaration of theClosingCallbackcallback.static interfaceFunctional interface declaration of theErrorCallbackcallback.static interfaceFunctional interface declaration of theMessageCallbackcallback.static interfaceFunctional interface declaration of thePongCallbackcallback.static classThe abstract base class forWebsocketConnection.Nested classes/interfaces inherited from class org.gnome.gobject.GObject
GObject.NotifyCallback, GObject.ObjectClass -
Constructor Summary
ConstructorsConstructorDescriptionCreates a new WebsocketConnection.WebsocketConnection(MemorySegment address) Create a WebsocketConnection proxy instance for the provided memory address.WebsocketConnection(IOStream stream, Uri uri, WebsocketConnectionType type, @Nullable String origin, @Nullable String protocol, List<WebsocketExtension> extensions) Creates aSoupWebsocketConnectiononstreamwith the given activeextensions. -
Method Summary
Modifier and TypeMethodDescriptionprotected WebsocketConnectionasParent()Returns this instance as if it were its parent type.static WebsocketConnection.Builder<? extends WebsocketConnection.Builder> builder()AWebsocketConnection.Builderobject constructs aWebsocketConnectionwith the specified properties.voidClose the connection in an orderly fashion.voidEmits the "closed" signal.voidEmits the "closing" signal.voidEmits the "error" signal.voidemitMessage(int type, byte @Nullable [] message) Emits the "message" signal.voidemitPong(byte @Nullable [] message) Emits the "pong" signal.shortGet the close code received from the WebSocket peer.Get the close data received from the WebSocket peer.Get the connection type (client/server) of the connection.Get the extensions chosen via negotiation with the peer.Get the I/O stream the WebSocket is communicating over.intGets the keepalive interval in seconds or 0 if disabled.intGets the keepalive pong timeout in seconds or 0 if disabled.longGets the maximum payload size allowed for incoming packets.@Nullable StringGet the origin of the WebSocket.@Nullable StringGet the protocol chosen via negotiation with the peer.getState()Get the current state of the WebSocket.static @Nullable TypegetType()Get the GType of the WebsocketConnection classgetUri()Get the URI of the WebSocket.Emitted when the connection has completely closed.This signal will be emitted during an orderly close.onError(WebsocketConnection.ErrorCallback handler) Emitted when an error occurred on the WebSocket.Emitted when we receive a message from the peer.onPong(WebsocketConnection.PongCallback handler) Emitted when we receive a Pong frame (solicited or unsolicited) from the peer.voidsendBinary(@org.jspecify.annotations.Nullable byte @Nullable [] data) Send a binary message to the peer.voidsendMessage(WebsocketDataType type, byte[] message) Send a message of the giventypeto the peer.voidSend anull-terminated text (UTF-8) message to the peer.voidsetKeepaliveInterval(int interval) Sets the interval in seconds on when to send a ping message which will serve as a keepalive message.voidsetKeepalivePongTimeout(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.voidsetMaxIncomingPayloadSize(long maxIncomingPayloadSize) Sets the maximum payload size allowed for incoming packets.Methods inherited from class org.gnome.gobject.GObject
addToggleRef, addWeakPointer, bindProperty, bindProperty, bindProperty, bindPropertyFull, bindPropertyFull, bindPropertyWithClosures, bindPropertyWithClosures, compatControl, connect, connect, connect, constructed, disconnect, dispatchPropertiesChanged, dispose, dupData, dupQdata, emit, emitNotify, finalize_, forceFloating, freezeNotify, get, getData, getMemoryLayout, getProperty, getProperty, getProperty, getQdata, getv, interfaceFindProperty, interfaceInstallProperty, interfaceListProperties, isFloating, newInstance, newInstance, newv, notify, notify, notifyByPspec, onNotify, ref, refSink, removeToggleRef, removeWeakPointer, replaceData, replaceQdata, runDispose, set, setData, setDataFull, setProperty, setProperty, setProperty, setQdata, setQdataFull, setv, stealData, stealQdata, takeRef, thawNotify, unref, watchClosure, weakRef, weakUnref, withPropertiesMethods inherited from class org.gnome.gobject.TypeInstance
callParent, callParent, cast, getPrivate, readGClass, writeGClassMethods inherited from class org.javagi.base.ProxyInstance
equals, handle, hashCode
-
Constructor Details
-
WebsocketConnection
Create a WebsocketConnection proxy 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 aSoupWebsocketConnectiononstreamwith the given activeextensions.This should be called after completing the handshake to begin using the WebSocket protocol.
- Parameters:
stream- aGIOStreamconnected to the WebSocket serveruri- the URI of the connectiontype- the type of connection (client/side)origin- the Origin of the clientprotocol- the subprotocol in useextensions- aGListofSoupWebsocketExtensionobjects
-
WebsocketConnection
public WebsocketConnection()Creates a new WebsocketConnection.
-
-
Method Details
-
getType
Get the GType of the WebsocketConnection class- Returns:
- the GType
-
asParent
Returns this instance as if it were its parent type. This is mostly synonymous to the Javasuperkeyword, but will set the native typeclass function pointers to the parent type. When overriding a native virtual method in Java, "chaining up" withsuper.methodName()doesn't work, because it invokes the overridden function pointer again. To chain up, callasParent().methodName(). This will call the native function pointer of this virtual method in the typeclass of the parent type. -
close
Close the connection in an orderly fashion.Note that until the
WebsocketConnection::closedsignal fires, the connection is not yet completely closed. The close message is not even sent until the main loop runs.The
codeanddataare sent to the peer along with the close request. IfcodeisWebsocketCloseCode.NO_STATUSa close message with no body (without code and data) is sent. Note that thedatamust be UTF-8 valid.- Parameters:
code- close codedata- 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.CLOSEDstate. The value will often be in theWebsocketCloseCodeenumeration, but may also be an application defined close code.- Returns:
- the close code or zero.
-
getCloseData
Get the close data received from the WebSocket peer.This only becomes valid once the WebSocket is in the
WebsocketState.CLOSEDstate. 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
Get the connection type (client/server) of the connection.- Returns:
- the connection type
-
getExtensions
Get the extensions chosen via negotiation with the peer.- Returns:
- a
GListofSoupWebsocketExtensionobjects
-
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
-
getProtocol
Get the protocol chosen via negotiation with the peer.- Returns:
- the chosen protocol
-
getState
-
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(@org.jspecify.annotations.Nullable byte @Nullable [] data) Send a binary message to the peer.If
lengthis 0,datamay benull.The message is queued to be sent and will be sent when the main loop is run.
- Parameters:
data- the message contents
-
sendMessage
Send a message of the giventypeto the peer. Note that this method, allows to send text messages containingnullcharacters.The message is queued to be sent and will be sent when the main loop is run.
- Parameters:
type- the type of message contentsmessage- the message data asGBytes
-
sendText
Send anull-terminated text (UTF-8) message to the peer.If you need to send text messages containing
nullcharacters usesendMessage(org.gnome.soup.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
public SignalConnection<WebsocketConnection.ClosedCallback> onClosed(WebsocketConnection.ClosedCallback handler) Emitted when the connection has completely closed.This happens either due to an orderly close from the peer, one initiated via
close(short, java.lang.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. SeeonClosed(org.gnome.soup.WebsocketConnection.ClosedCallback). -
onClosing
public SignalConnection<WebsocketConnection.ClosingCallback> onClosing(WebsocketConnection.ClosingCallback handler) 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. SeeonClosing(org.gnome.soup.WebsocketConnection.ClosingCallback). -
onError
public SignalConnection<WebsocketConnection.ErrorCallback> onError(WebsocketConnection.ErrorCallback handler) Emitted when an error occurred on the WebSocket.This may be fired multiple times. Fatal errors will be followed by the
WebsocketConnection::closedsignal being emitted.- Parameters:
handler- the signal handler- Returns:
- a signal handler ID to keep track of the signal connection
- See Also:
-
emitError
Emits the "error" signal. SeeonError(org.gnome.soup.WebsocketConnection.ErrorCallback). -
onMessage
public SignalConnection<WebsocketConnection.MessageCallback> onMessage(WebsocketConnection.MessageCallback handler) Emitted when we receive a message from the peer.As a convenience, the
messagedata will always benull-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. SeeonMessage(org.gnome.soup.WebsocketConnection.MessageCallback). -
onPong
public SignalConnection<WebsocketConnection.PongCallback> onPong(WebsocketConnection.PongCallback handler) Emitted when we receive a Pong frame (solicited or unsolicited) from the peer.As a convenience, the
messagedata will always benull-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. SeeonPong(org.gnome.soup.WebsocketConnection.PongCallback). -
builder
AWebsocketConnection.Builderobject constructs aWebsocketConnectionwith the specified properties. Use the variousset...()methods to set properties, and finish construction withWebsocketConnection.Builder.build().- Returns:
- the builder object
-