WebSocketClient

Inherits: WebSocketMultiplayerPeer < MultiplayerPeer < PacketPeer < RefCounted < Object

A WebSocket client implementation.

Description

This class implements a WebSocket client compatible with any RFC 6455-compliant WebSocket server.

This client can be optionally used as a multiplayer peer for the MultiplayerAPI.

After starting the client (connect_to_url), you will need to MultiplayerPeer.poll it at regular intervals (e.g. inside Node._process).

You will receive appropriate signals when connecting, disconnecting, or when new data is available.

Note: When exporting to Android, make sure to enable the INTERNET permission in the Android export preset before exporting the project or using one-click deploy. Otherwise, network communication of any kind will be blocked by Android.

Methods

Error

connect_to_url ( String url, PackedStringArray protocols=PackedStringArray(), bool gd_mp_api=false, PackedStringArray custom_headers=PackedStringArray() )

void

disconnect_from_host ( int code=1000, String reason="" )

String

get_connected_host ( ) const

int

get_connected_port ( ) const

Signals

  • connection_closed ( bool was_clean_close )

Emitted when the connection to the server is closed. was_clean_close will be true if the connection was shutdown cleanly.


  • connection_error ( )

Emitted when the connection to the server fails.


  • connection_established ( String protocol )

Emitted when a connection with the server is established, protocol will contain the sub-protocol agreed with the server.


  • data_received ( )

Emitted when a WebSocket message is received.

Note: This signal is not emitted when used as high-level multiplayer peer.


  • server_close_request ( int code, String reason )

Emitted when the server requests a clean close. You should keep polling until you get a connection_closed signal to achieve the clean close. See WebSocketPeer.close for more details.

Property Descriptions

Setter

set_trusted_ssl_certificate(value)

Getter

get_trusted_ssl_certificate()

If specified, this X509Certificate will be the only one accepted when connecting to an SSL host. Any other certificate provided by the server will be regarded as invalid.

Note: Specifying a custom trusted_ssl_certificate is not supported in HTML5 exports due to browsers restrictions.


Setter

set_verify_ssl_enabled(value)

Getter

is_verify_ssl_enabled()

If true, SSL certificate verification is enabled.

Note: You must specify the certificates to be used in the Project Settings for it to work when exported.

Method Descriptions

Connects to the given URL requesting one of the given protocols as sub-protocol. If the list empty (default), no sub-protocol will be requested.

If true is passed as gd_mp_api, the client will behave like a multiplayer peer for the MultiplayerAPI, connections to non-Godot servers will not work, and data_received will not be emitted.

If false is passed instead (default), you must call PacketPeer functions (put_packet, get_packet, etc.) on the WebSocketPeer returned via get_peer(1) and not on this object directly (e.g. get_peer(1).put_packet(data)).

You can optionally pass a list of custom_headers to be added to the handshake HTTP request.

Note: To avoid mixed content warnings or errors in HTML5, you may have to use a url that starts with wss:// (secure) instead of ws://. When doing so, make sure to use the fully qualified domain name that matches the one defined in the server's SSL certificate. Do not connect directly via the IP address for wss:// connections, as it won't match with the SSL certificate.

Note: Specifying custom_headers is not supported in HTML5 exports due to browsers restrictions.


  • void disconnect_from_host ( int code=1000, String reason="" )

Disconnects this client from the connected host. See WebSocketPeer.close for more information.


  • String get_connected_host ( ) const

Return the IP address of the currently connected host.


  • int get_connected_port ( ) const

Return the IP port of the currently connected host.