WebSocketServer

Inherits: WebSocketMultiplayerPeer < NetworkedMultiplayerPeer < PacketPeer < Reference < Object

A WebSocket server implementation.

Description

This class implements a WebSocket server that can also support the high-level multiplayer API.

After starting the server (listen), you will need to NetworkedMultiplayerPeer.poll it at regular intervals (e.g. inside Node._process). When clients connect, disconnect, or send data, you will receive the appropriate signal.

Note: Not available in HTML5 exports.

Properties

String

bind_ip

"*"

X509Certificate

ca_chain

float

handshake_timeout

3.0

CryptoKey

private_key

X509Certificate

ssl_certificate

Methods

void

disconnect_peer ( int id, int code=1000, String reason="" )

String

get_peer_address ( int id ) const

int

get_peer_port ( int id ) const

bool

has_peer ( int id ) const

bool

is_listening ( ) const

Error

listen ( int port, PoolStringArray protocols=PoolStringArray( ), bool gd_mp_api=false )

void

set_extra_headers ( PoolStringArray headers=PoolStringArray( ) )

void

stop ( )


Signals

client_close_request ( int id, int code, String reason )

Emitted when a client requests a clean close. You should keep polling until you get a client_disconnected signal with the same id to achieve the clean close. See WebSocketPeer.close for more details.


client_connected ( int id, String protocol )

Emitted when a new client connects. "protocol" will be the sub-protocol agreed with the client.


client_disconnected ( int id, bool was_clean_close )

Emitted when a client disconnects. was_clean_close will be true if the connection was shutdown cleanly.


data_received ( int id )

Emitted when a new message is received.

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


Property Descriptions

String bind_ip = "*"

When not set to * will restrict incoming connections to the specified IP address. Setting bind_ip to 127.0.0.1 will cause the server to listen only to the local host.


X509Certificate ca_chain

When using SSL (see private_key and ssl_certificate), you can set this to a valid X509Certificate to be provided as additional CA chain information during the SSL handshake.


float handshake_timeout = 3.0

  • void set_handshake_timeout ( float value )

  • float get_handshake_timeout ( )

The time in seconds before a pending client (i.e. a client that has not yet finished the HTTP handshake) is considered stale and forcefully disconnected.


CryptoKey private_key

When set to a valid CryptoKey (along with ssl_certificate) will cause the server to require SSL instead of regular TCP (i.e. the wss:// protocol).


X509Certificate ssl_certificate

When set to a valid X509Certificate (along with private_key) will cause the server to require SSL instead of regular TCP (i.e. the wss:// protocol).


Method Descriptions

void disconnect_peer ( int id, int code=1000, String reason="" )

Disconnects the peer identified by id from the server. See WebSocketPeer.close for more information.


String get_peer_address ( int id ) const

Returns the IP address of the given peer.


int get_peer_port ( int id ) const

Returns the remote port of the given peer.


bool has_peer ( int id ) const

Returns true if a peer with the given ID is connected.


bool is_listening ( ) const

Returns true if the server is actively listening on a port.


Error listen ( int port, PoolStringArray protocols=PoolStringArray( ), bool gd_mp_api=false )

Starts listening on the given port.

You can specify the desired subprotocols via the "protocols" array. If the list empty (default), no sub-protocol will be requested.

If true is passed as gd_mp_api, the server will behave like a network peer for the MultiplayerAPI, connections from non-Godot clients 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(id) to communicate with the peer with given id (e.g. get_peer(id).get_available_packet_count).


void set_extra_headers ( PoolStringArray headers=PoolStringArray( ) )

Sets additional headers to be sent to clients during the HTTP handshake.


void stop ( )

Stops the server and clear its state.