Top |
SoupWebsocketConnectionType | connection-type | Read / Write / Construct Only |
GIOStream * | io-stream | Read / Write / Construct Only |
gchar * | origin | Read / Write / Construct Only |
gchar * | protocol | Read / Write / Construct Only |
SoupWebsocketState | state | Read |
SoupURI * | uri | Read / Write / Construct Only |
SoupWebsocketConnection provides support for the WebSocket protocol.
To connect to a WebSocket server, create a SoupSession and call
soup_session_websocket_connect_async()
. To accept WebSocket
connections, create a SoupServer and add a handler to it with
soup_server_add_websocket_handler()
.
(Lower-level support is available via
soup_websocket_client_prepare_handshake()
and
soup_websocket_client_verify_handshake()
, for handling the client
side of the WebSocket handshake, and
soup_websocket_server_process_handshake()
for handling the server
side.)
SoupWebsocketConnection handles the details of WebSocket
communication. You can use soup_websocket_connection_send_text()
and soup_websocket_connection_send_binary()
to send data, and the
“message” signal to receive data.
(SoupWebsocketConnection currently only supports asynchronous
I/O.)
void soup_websocket_client_prepare_handshake (SoupMessage *msg
,const char *origin
,char **protocols
);
Adds the necessary headers to msg
to request a WebSocket
handshake. The message body and non-WebSocket-related headers are
not modified.
This is a low-level function; if you use
soup_session_websocket_connect_async()
to create a WebSocket
connection, it will call this for you.
msg |
||
origin |
the "Origin" header to set. |
[allow-none] |
protocols |
list of protocols to offer. |
[allow-none][array zero-terminated=1] |
Since: 2.50
gboolean soup_websocket_client_verify_handshake (SoupMessage *msg
,GError **error
);
Looks at the response status code and headers in msg
and
determines if they contain a valid WebSocket handshake response
(given the handshake request in msg
's request headers).
This is a low-level function; if you use
soup_session_websocket_connect_async()
to create a WebSocket
connection, it will call this for you.
msg |
SoupMessage containing both client and server sides of a WebSocket handshake |
|
error |
return location for a GError |
Since: 2.50
gboolean soup_websocket_server_check_handshake (SoupMessage *msg
,const char *origin
,char **protocols
,GError **error
);
Examines the method and request headers in msg
and determines
whether msg
contains a valid handshake request.
If origin
is non-NULL
, then only requests containing a matching
"Origin" header will be accepted. If protocols
is non-NULL
, then
only requests containing a compatible "Sec-WebSocket-Protocols"
header will be accepted.
Normally soup_websocket_server_process_handshake()
will take care
of this for you, and if you use soup_server_add_websocket_handler()
to handle accepting WebSocket connections, it will call that for
you. However, this function may be useful if you need to perform
more complicated validation; eg, accepting multiple different Origins,
or handling different protocols depending on the path.
msg |
SoupMessage containing the client side of a WebSocket handshake |
|
origin |
expected Origin header. |
[allow-none] |
protocols |
allowed WebSocket protocols. |
[allow-none][array zero-terminated=1] |
error |
return location for a GError |
Since: 2.50
gboolean soup_websocket_server_process_handshake (SoupMessage *msg
,const char *expected_origin
,char **protocols
);
Examines the method and request headers in msg
and (assuming msg
contains a valid handshake request), fills in the handshake
response.
If expected_origin
is non-NULL
, then only requests containing a matching
"Origin" header will be accepted. If protocols
is non-NULL
, then
only requests containing a compatible "Sec-WebSocket-Protocols"
header will be accepted.
This is a low-level function; if you use
soup_server_add_websocket_handler()
to handle accepting WebSocket
connections, it will call this for you.
msg |
SoupMessage containing the client side of a WebSocket handshake |
|
expected_origin |
expected Origin header. |
[allow-none] |
protocols |
allowed WebSocket protocols. |
[allow-none][array zero-terminated=1] |
TRUE
if msg
contained a valid WebSocket handshake
request and was updated to contain a handshake response. FALSE
if not.
Since: 2.50
SoupWebsocketConnection * soup_websocket_connection_new (GIOStream *stream
,SoupURI *uri
,SoupWebsocketConnectionType type
,const char *origin
,const char *protocol
);
Creates a SoupWebsocketConnection on stream
. This should be
called after completing the handshake to begin using the WebSocket
protocol.
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. |
[allow-none] |
protocol |
the subprotocol in use. |
[allow-none] |
Since: 2.50
GIOStream *
soup_websocket_connection_get_io_stream
(SoupWebsocketConnection *self
);
Get the I/O stream the WebSocket is communicating over.
Since: 2.50
SoupWebsocketConnectionType
soup_websocket_connection_get_connection_type
(SoupWebsocketConnection *self
);
Get the connection type (client/server) of the connection.
Since: 2.50
SoupURI *
soup_websocket_connection_get_uri (SoupWebsocketConnection *self
);
Get the URI of the WebSocket.
For servers this represents the address of the WebSocket, and for clients it is the address connected to.
Since: 2.50
const char *
soup_websocket_connection_get_origin (SoupWebsocketConnection *self
);
Get the origin of the WebSocket.
Since: 2.50
const char *
soup_websocket_connection_get_protocol
(SoupWebsocketConnection *self
);
Get the protocol chosen via negotiation with the peer.
Since: 2.50
SoupWebsocketState
soup_websocket_connection_get_state (SoupWebsocketConnection *self
);
Get the current state of the WebSocket.
Since: 2.50
void soup_websocket_connection_send_text (SoupWebsocketConnection *self
,const char *text
);
Send a text (UTF-8) message to the peer.
The message is queued to be sent and will be sent when the main loop is run.
Since: 2.50
void soup_websocket_connection_send_binary (SoupWebsocketConnection *self
,gconstpointer data
,gsize length
);
Send a binary message to the peer.
The message is queued to be sent and will be sent when the main loop is run.
self |
the WebSocket |
|
data |
the message contents. |
[array length=length][element-type guint8] |
length |
the length of |
Since: 2.50
void soup_websocket_connection_close (SoupWebsocketConnection *self
,gushort code
,const char *data
);
Close the connection in an orderly fashion.
Note that until the “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.
Note that the data
must be UTF-8 valid.
Since: 2.50
gushort
soup_websocket_connection_get_close_code
(SoupWebsocketConnection *self
);
Get the close code received from the WebSocket peer.
This only becomes valid once the WebSocket is in the
SOUP_WEBSOCKET_STATE_CLOSED
state. The value will often be in the
SoupWebsocketCloseCode enumeration, but may also be an application
defined close code.
Since: 2.50
const char *
soup_websocket_connection_get_close_data
(SoupWebsocketConnection *self
);
Get the close data received from the WebSocket peer.
This only becomes valid once the WebSocket is in the
SOUP_WEBSOCKET_STATE_CLOSED
state. The data may be freed once
the main loop is run, so copy it if you need to keep it around.
Since: 2.50
#define SOUP_WEBSOCKET_ERROR (soup_websocket_error_get_quark ())
A GError domain for WebSocket-related errors. Used with SoupWebsocketError.
Since: 2.50
typedef struct _SoupWebsocketConnection SoupWebsocketConnection;
A class representing a WebSocket connection.
Since: 2.50
Pre-defined close codes that can be passed to
soup_websocket_connection_close()
or received from
soup_websocket_connection_get_close_code()
. (However, other codes
are also allowed.)
a normal, non-error close |
||
the client/server is going away |
||
a protocol error occurred |
||
the endpoint received data of a type that it does not support. |
||
reserved value indicating that no close code was present; must not be sent. |
||
reserved value indicating that the connection was closed abnormally; must not be sent. |
||
the endpoint received data that was invalid (eg, non-UTF-8 data in a text message). |
||
generic error code indicating some sort of policy violation. |
||
the endpoint received a message that is too big to process. |
||
the client is closing the connection because the server failed to negotiate a required extension. |
||
the server is closing the connection because it was unable to fulfill the request. |
||
reserved value indicating that the TLS handshake failed; must not be sent. |
Since: 2.50
WebSocket-related errors.
a generic error |
||
attempted to handshake with a server that does not appear to understand WebSockets. |
||
the WebSocket handshake failed because some detail was invalid (eg, incorrect accept key). |
||
the WebSocket handshake failed because the "Origin" header was not an allowed value. |
Since: 2.50
“connection-type”
property“connection-type” SoupWebsocketConnectionType
The type of connection (client/server).
Flags: Read / Write / Construct Only
Default value: SOUP_WEBSOCKET_CONNECTION_UNKNOWN
Since: 2.50
“io-stream”
property“io-stream” GIOStream *
The underlying IO stream the WebSocket is communicating over.
The input and output streams must be pollable streams.
Flags: Read / Write / Construct Only
Since: 2.50
“origin”
property“origin” gchar *
The client's Origin.
Flags: Read / Write / Construct Only
Default value: NULL
Since: 2.50
“protocol”
property“protocol” gchar *
The chosen protocol, or NULL
if a protocol was not agreed
upon.
Flags: Read / Write / Construct Only
Default value: NULL
Since: 2.50
“state”
property“state” SoupWebsocketState
The current state of the WebSocket.
Flags: Read
Default value: SOUP_WEBSOCKET_STATE_OPEN
Since: 2.50
“uri”
property“uri” SoupURI *
The URI of the WebSocket.
For servers this represents the address of the WebSocket, and for clients it is the address connected to.
Flags: Read / Write / Construct Only
Since: 2.50
“closed”
signalvoid user_function (SoupWebsocketConnection *self, gpointer user_data)
Emitted when the connection has completely closed, either
due to an orderly close from the peer, one initiated via
soup_websocket_connection_close()
or a fatal error
condition that caused a close.
This signal will be emitted once.
Flags: Run First
Since: 2.50
“closing”
signalvoid user_function (SoupWebsocketConnection *self, gpointer user_data)
This signal will be emitted during an orderly close.
Flags: Run Last
Since: 2.50
“error”
signalvoid user_function (SoupWebsocketConnection *self, GError *error, gpointer user_data)
Emitted when an error occurred on the WebSocket. This may be fired multiple times. Fatal errors will be followed by the “closed” signal being emitted.
self |
the WebSocket |
|
error |
the error that occured |
|
user_data |
user data set when the signal handler was connected. |
Flags: Run First
Since: 2.50
“message”
signalvoid user_function (SoupWebsocketConnection *self, gint type, GBytes *message, gpointer user_data)
Emitted when we receive a message from the peer.
As a convenience, the message
data will always be
NUL-terminated, but the NUL byte will not be included in
the length count.
self |
the WebSocket |
|
type |
the type of message contents |
|
message |
the message data |
|
user_data |
user data set when the signal handler was connected. |
Flags: Run First
Since: 2.50