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[], io.github.jwharm.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[], io.github.jwharm.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
Modifier and TypeClassDescriptionstatic class
Inner class implementing a builder pattern to construct a GObject with properties.static interface
Functional interface declaration of theClosedCallback
callback.static interface
Functional interface declaration of theClosingCallback
callback.static interface
Functional interface declaration of theErrorCallback
callback.static interface
Functional interface declaration of theMessageCallback
callback.static interface
Functional interface declaration of thePongCallback
callback.static class
The abstract base class forWebsocketConnection
.Nested classes/interfaces inherited from class org.gnome.gobject.GObject
GObject.NotifyCallback, GObject.ObjectClass
-
Constructor Summary
ConstructorDescriptionWebsocketConnection
(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 aSoupWebsocketConnection
onstream
with the given activeextensions
. -
Method Summary
Modifier and TypeMethodDescriptionprotected WebsocketConnection
asParent()
Returns this instance as if it were its parent type.static WebsocketConnection.Builder
<? extends WebsocketConnection.Builder> builder()
AWebsocketConnection.Builder
object constructs aWebsocketConnection
with the specified properties.void
Close the connection in an orderly fashion.void
Emits the "closed" signal.void
Emits the "closing" signal.void
Emits the "error" signal.void
emitMessage
(int type, Bytes message) Emits the "message" signal.void
Emits the "pong" signal.short
Get 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.int
Gets the keepalive interval in seconds or 0 if disabled.int
Gets the keepalive pong timeout in seconds or 0 if disabled.long
Gets the maximum payload size allowed for incoming packets.Get the origin of the WebSocket.Get the protocol chosen via negotiation with the peer.getState()
Get the current state of the WebSocket.static Type
getType()
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.void
sendBinary
(@org.jetbrains.annotations.Nullable byte[] data) Send a binary message to the peer.void
sendMessage
(WebsocketDataType type, Bytes message) Send a message of the giventype
to the peer.void
Send anull
-terminated text (UTF-8) message to the peer.void
setKeepaliveInterval
(int interval) Sets the interval in seconds on when to send a ping message which will serve as a keepalive message.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.void
setMaxIncomingPayloadSize
(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, 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, withProperties
Methods inherited from class org.gnome.gobject.TypeInstance
callParent, callParent, getPrivate, readGClass, writeGClass
Methods inherited from class io.github.jwharm.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 @Nullable String origin, @Nullable @Nullable String protocol, List<WebsocketExtension> extensions) throws UnsupportedPlatformException Creates aSoupWebsocketConnection
onstream
with the given activeextensions
.This should be called after completing the handshake to begin using the WebSocket protocol.
- Parameters:
stream
- aGIOStream
connected 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
- aGList
ofSoupWebsocketExtension
objects- Throws:
UnsupportedPlatformException
- when run on a platform other than linux
-
-
Method Details
-
getType
-
asParent
Returns this instance as if it were its parent type. This is mostly synonymous to the Javasuper
keyword, 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::closed
signal fires, the connection is not yet completely closed. The close message is not even sent until the main loop runs.The
code
anddata
are sent to the peer along with the close request. Ifcode
isWebsocketCloseCode.NO_STATUS
a close message with no body (without code and data) is sent. Note that thedata
must 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.CLOSED
state. The value will often be in theWebsocketCloseCode
enumeration, 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.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
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
GList
ofSoupWebsocketExtension
objects
-
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.
-
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(@Nullable @org.jetbrains.annotations.Nullable byte[] data) Send a binary message to the peer.If
length
is 0,data
may 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 giventype
to the peer. Note that this method, allows to send text messages containingnull
characters.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
null
characters usesendMessage(org.gnome.soup.WebsocketDataType, org.gnome.glib.Bytes)
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
-
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::closed
signal 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
message
data 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
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
message
data 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
Emits the "pong" signal. SeeonPong(org.gnome.soup.WebsocketConnection.PongCallback)
. -
builder
AWebsocketConnection.Builder
object constructs aWebsocketConnection
with the specified properties. Use the variousset...()
methods to set properties, and finish construction withWebsocketConnection.Builder.build()
.
-