1.24 <SoupWebsocketConnection>

Class: <SoupWebsocketConnection>

It derives from <GObject>.

The WebSocket Protocol

Provides support for the WebSocket protocol.

To connect to a WebSocket server, create a Session and call Session.websocket_connect_async. To accept WebSocket connections, create a Server and add a handler to it with Server.add_websocket_handler.

(Lower-level support is available via websocket_client_prepare_handshake and websocket_client_verify_handshake, for handling the client side of the WebSocket handshake, and websocket_server_process_handshake for handling the server side.)

WebsocketConnection handles the details of WebSocket communication. You can use WebsocketConnection.send_text and WebsocketConnection.send_binary to send data, and the "message" signal to receive data. (WebsocketConnection currently only supports asynchronous I/O.)

Properties

Property: websocket-connection:connection-type self

The type of connection (client/server).

Its value is a <SoupWebsocketConnectionType> (or, to set it, one of its symbols).

It can only be set when the object is made, with #:connection-type.

Property: websocket-connection:extensions self

List of WebsocketExtension objects that are active in the connection.

Its value is a foreign pointer (or, to set it, a bytevector).

It can only be set when the object is made, with #:extensions.

Property: websocket-connection:io-stream self

The underlying IO stream the WebSocket is communicating over.

The input and output streams must be pollable streams.

Its value is a <GIOStream>, or #f.

It can only be set when the object is made, with #:io-stream.

Property: websocket-connection:keepalive-interval self

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.

Its value is an unsigned 32-bit integer.

Set it with (set! (websocket-connection:keepalive-interval self) value).

Property: websocket-connection:keepalive-pong-timeout self

Timeout in seconds for when the absence of a pong from a keepalive ping is assumed to be caused by a faulty connection. The WebSocket will be transitioned to a closed state when this happens.

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

Its value is an unsigned 32-bit integer.

Set it with (set! (websocket-connection:keepalive-pong-timeout self) value).

Property: websocket-connection:max-incoming-payload-size self

The maximum payload size for incoming packets.

The protocol expects or 0 to not limit it.

Its value is an unsigned 64-bit integer.

Set it with (set! (websocket-connection:max-incoming-payload-size self) value).

Property: websocket-connection:origin self

The client’s Origin.

Its value is a string, or #f.

It can only be set when the object is made, with #:origin.

Property: websocket-connection:protocol self

The chosen protocol, or #f if a protocol was not agreed upon.

Its value is a string, or #f.

It can only be set when the object is made, with #:protocol.

Property: websocket-connection:state self

The current state of the WebSocket.

Its value is a <SoupWebsocketState>.

It is read only.

Property: websocket-connection:uri self

The URI of the WebSocket.

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

Its value is a <GUri>, or #f.

It can only be set when the object is made, with #:uri.

Procedures

Method: websocket-connection:close self code [data]
Method: close self code [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. If code is no-status a close message with no body (without code and data) is sent. Note that the data must be UTF-8 valid.

self

Passed: a <SoupWebsocketConnection>.

the WebSocket

code

Passed: an unsigned 16-bit integer.

close code

data

Passed, or left out: a string, or #f.

close data

Method: websocket-connection:get-close-code self ⇒ %return
Method: get-close-code self ⇒ %return

Get the close code received from the WebSocket peer.

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

self

Passed: a <SoupWebsocketConnection>.

the WebSocket

%return

Returned: an unsigned 16-bit integer.

the close code or zero.

Method: websocket-connection:get-close-data self ⇒ %return
Method: get-close-data self ⇒ %return

Get the close data received from the WebSocket peer.

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

self

Passed: a <SoupWebsocketConnection>.

the WebSocket

%return

Returned: a string.

the close data or #f

Method: websocket-connection:get-connection-type self ⇒ %return
Method: get-connection-type self ⇒ %return

Get the connection type (client/server) of the connection.

self

Passed: a <SoupWebsocketConnection>.

the WebSocket

%return

Returned: a <SoupWebsocketConnectionType>.

the connection type

Method: websocket-connection:get-extensions self ⇒ %return
Method: get-extensions self ⇒ %return

Get the extensions chosen via negotiation with the peer.

self

Passed: a <SoupWebsocketConnection>.

the WebSocket

%return

Returned: a list, each a <SoupWebsocketExtension>.

a GList of <SoupWebsocketExtension> objects

Method: websocket-connection:get-io-stream self ⇒ %return
Method: get-io-stream self ⇒ %return

Get the I/O stream the WebSocket is communicating over.

self

Passed: a <SoupWebsocketConnection>.

the WebSocket

%return

Returned: a <GIOStream>.

the WebSocket’s I/O stream.

Method: websocket-connection:get-keepalive-interval self ⇒ %return
Method: get-keepalive-interval self ⇒ %return

Gets the keepalive interval in seconds or 0 if disabled.

self

Passed: a <SoupWebsocketConnection>.

the WebSocket

%return

Returned: an unsigned 32-bit integer.

the keepalive interval.

Method: websocket-connection:get-keepalive-pong-timeout self ⇒ %return
Method: get-keepalive-pong-timeout self ⇒ %return

Gets the keepalive pong timeout in seconds or 0 if disabled.

self

Passed: a <SoupWebsocketConnection>.

the WebSocket

%return

Returned: an unsigned 32-bit integer.

the keepalive pong timeout.

Method: websocket-connection:get-max-incoming-payload-size self ⇒ %return
Method: get-max-incoming-payload-size self ⇒ %return

Gets the maximum payload size allowed for incoming packets.

self

Passed: a <SoupWebsocketConnection>.

the WebSocket

%return

Returned: an unsigned 64-bit integer.

the maximum payload size.

Method: websocket-connection:get-origin self ⇒ %return
Method: get-origin self ⇒ %return

Get the origin of the WebSocket.

self

Passed: a <SoupWebsocketConnection>.

the WebSocket

%return

Returned: a string, or #f.

the origin

Method: websocket-connection:get-protocol self ⇒ %return
Method: get-protocol self ⇒ %return

Get the protocol chosen via negotiation with the peer.

self

Passed: a <SoupWebsocketConnection>.

the WebSocket

%return

Returned: a string, or #f.

the chosen protocol

Method: websocket-connection:get-state self ⇒ %return
Method: get-state self ⇒ %return

Get the current state of the WebSocket.

self

Passed: a <SoupWebsocketConnection>.

the WebSocket

%return

Returned: a <SoupWebsocketState>.

the state

Method: websocket-connection:get-uri self ⇒ %return
Method: get-uri self ⇒ %return

Get the URI of the WebSocket.

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

self

Passed: a <SoupWebsocketConnection>.

the WebSocket

%return

Returned: a <GUri>.

the URI

Procedure: websocket-connection:new stream uri type origin protocol extensions ⇒ %return

Creates a WebsocketConnection on stream with the given active extensions.

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

stream

Passed: a <GIOStream>.

a <GIOStream> connected to the WebSocket server

uri

Passed: a <GUri>.

the URI of the connection

type

Passed: a <SoupWebsocketConnectionType>, or one of its symbols.

the type of connection (client/side)

origin

Passed: a string, or #f.

the Origin of the client

protocol

Passed: a string, or #f.

the subprotocol in use

extensions

Passed: a list, each a <SoupWebsocketExtension>.

a GList of <SoupWebsocketExtension> objects

%return

Returned: a <SoupWebsocketConnection>.

a new <SoupWebsocketConnection>

Method: websocket-connection:send-binary self data
Method: send-binary self data

Send a binary message to the peer.

If length is 0, data may be #f.

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

self

Passed: a <SoupWebsocketConnection>.

the WebSocket

data

Passed: a bytevector, or a list or vector, each an unsigned 8-bit integer, or #f.

the message contents

length

Not passed: guile-gi gives the length of data.

the length of data

Method: websocket-connection:send-message self type message
Method: send-message self type message

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

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

self

Passed: a <SoupWebsocketConnection>.

the WebSocket

type

Passed: a <SoupWebsocketDataType>, or one of its symbols.

the type of message contents

message

Passed: a <GBytes>.

the message data as <GBytes>

Method: websocket-connection:send-text self text
Method: send-text self text

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

If you need to send text messages containing #f characters use WebsocketConnection.send_message instead.

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

self

Passed: a <SoupWebsocketConnection>.

the WebSocket

text

Passed: a string.

the message contents

Method: websocket-connection:set-keepalive-interval self interval
Method: set-keepalive-interval self 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.

self

Passed: a <SoupWebsocketConnection>.

the WebSocket

interval

Passed: an unsigned 32-bit integer.

the interval to send a ping message or 0 to disable it

Method: websocket-connection:set-keepalive-pong-timeout self pong-timeout
Method: set-keepalive-pong-timeout self pong-timeout

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.

self

Passed: a <SoupWebsocketConnection>.

the WebSocket

pong-timeout

Passed: an unsigned 32-bit integer.

the timeout in seconds

Method: websocket-connection:set-max-incoming-payload-size self max-incoming-payload-size
Method: set-max-incoming-payload-size self max-incoming-payload-size

Sets the maximum payload size allowed for incoming packets.

It does not limit the outgoing packet size.

self

Passed: a <SoupWebsocketConnection>.

the WebSocket

max-incoming-payload-size

Passed: an unsigned 64-bit integer.

the maximum payload size

Signals

Signal: websocket-connection:closed

Connect a handler with (connect object websocket-connection:closed handler). It is called as (handler self).

Emitted when the connection has completely closed.

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

This signal will be emitted once.

self

Passed: a <SoupWebsocketConnection>.

Signal: websocket-connection:closing

Connect a handler with (connect object websocket-connection:closing handler). It is called as (handler self).

This signal will be emitted during an orderly close.

self

Passed: a <SoupWebsocketConnection>.

Signal: websocket-connection:error

Connect a handler with (connect object websocket-connection:error handler). It is called as (handler self error).

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

Passed: a <SoupWebsocketConnection>.

error

Passed: a <GError>.

the error that occured

Signal: websocket-connection:message

Connect a handler with (connect object websocket-connection:message handler). It is called as (handler self type message).

Emitted when we receive a message from the peer.

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

self

Passed: a <SoupWebsocketConnection>.

type

Passed: a 32-bit integer.

the type of message contents

message

Passed: a <GBytes>.

the message data

Signal: websocket-connection:pong

Connect a handler with (connect object websocket-connection:pong handler). It is called as (handler self message).

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

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

self

Passed: a <SoupWebsocketConnection>.

message

Passed: a <GBytes>.

the application data (if any)