Table of Contents

Class WebSocketPeer

Namespace
Godot
Assembly
GodotSharp.dll

This class represents WebSocket connection, and can be used as a WebSocket client (RFC 6455-compliant) or as a remote peer of a WebSocket server.

You can send WebSocket binary frames using PutPacket(byte[]), and WebSocket text frames using Send(byte[], WriteMode) (prefer text frames when interacting with text-based API). You can check the frame type of the last packet via WasStringPacket().

To start a WebSocket client, first call ConnectToUrl(string, TlsOptions), then regularly call Poll() (e.g. during Node process). You can query the socket state via GetReadyState(), get the number of pending packets using GetAvailablePacketCount(), and retrieve them via GetPacket().

To use the peer as part of a WebSocket server refer to AcceptStream(StreamPeer) and the online tutorial.

public class WebSocketPeer : PacketPeer, IDisposable
Inheritance
WebSocketPeer
Implements
Inherited Members

Constructors

WebSocketPeer()

public WebSocketPeer()

Properties

HandshakeHeaders

The extra HTTP headers to be sent during the WebSocket handshake.

Note: Not supported in Web exports due to browsers' restrictions.

public string[] HandshakeHeaders { get; set; }

Property Value

string[]

InboundBufferSize

The size of the input buffer in bytes (roughly the maximum amount of memory that will be allocated for the inbound packets).

public int InboundBufferSize { get; set; }

Property Value

int

MaxQueuedPackets

The maximum amount of packets that will be allowed in the queues (both inbound and outbound).

public int MaxQueuedPackets { get; set; }

Property Value

int

OutboundBufferSize

The size of the input buffer in bytes (roughly the maximum amount of memory that will be allocated for the outbound packets).

public int OutboundBufferSize { get; set; }

Property Value

int

SupportedProtocols

The WebSocket sub-protocols allowed during the WebSocket handshake.

public string[] SupportedProtocols { get; set; }

Property Value

string[]

Methods

AcceptStream(StreamPeer)

Accepts a peer connection performing the HTTP handshake as a WebSocket server. The stream must be a valid TCP stream retrieved via TakeConnection(), or a TLS stream accepted via AcceptStream(StreamPeer, TlsOptions).

Note: Not supported in Web exports due to browsers' restrictions.

public Error AcceptStream(StreamPeer stream)

Parameters

stream StreamPeer

Returns

Error

Close(int, string)

Closes this WebSocket connection. code is the status code for the closure (see RFC 6455 section 7.4 for a list of valid status codes). reason is the human readable reason for closing the connection (can be any UTF-8 string that's smaller than 123 bytes). If code is negative, the connection will be closed immediately without notifying the remote peer.

Note: To achieve a clean close, you will need to keep polling until Closed is reached.

Note: The Web export might not support all status codes. Please refer to browser-specific documentation for more details.

public void Close(int code = 1000, string reason = "")

Parameters

code int
reason string

ConnectToUrl(string, TlsOptions)

Connects to the given URL. TLS certificates will be verified against the hostname when connecting using the wss:// protocol. You can pass the optional tlsClientOptions parameter to customize the trusted certification authorities, or disable the common name verification. See Client(X509Certificate, string) and ClientUnsafe(X509Certificate).

Note: To avoid mixed content warnings or errors in Web, 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 TLS certificate. Do not connect directly via the IP address for wss:// connections, as it won't match with the TLS certificate.

public Error ConnectToUrl(string url, TlsOptions tlsClientOptions = null)

Parameters

url string
tlsClientOptions TlsOptions

Returns

Error

GetCloseCode()

Returns the received WebSocket close frame status code, or -1 when the connection was not cleanly closed. Only call this method when GetReadyState() returns Closed.

public int GetCloseCode()

Returns

int

GetCloseReason()

Returns the received WebSocket close frame status reason string. Only call this method when GetReadyState() returns Closed.

public string GetCloseReason()

Returns

string

GetConnectedHost()

Returns the IP address of the connected peer.

Note: Not available in the Web export.

public string GetConnectedHost()

Returns

string

GetConnectedPort()

Returns the remote port of the connected peer.

Note: Not available in the Web export.

public ushort GetConnectedPort()

Returns

ushort

GetCurrentOutboundBufferedAmount()

Returns the current amount of data in the outbound websocket buffer. Note: Web exports use WebSocket.bufferedAmount, while other platforms use an internal buffer.

public int GetCurrentOutboundBufferedAmount()

Returns

int

GetReadyState()

Returns the ready state of the connection. See WebSocketPeer.State.

public WebSocketPeer.State GetReadyState()

Returns

WebSocketPeer.State

GetRequestedUrl()

Returns the URL requested by this peer. The URL is derived from the url passed to ConnectToUrl(string, TlsOptions) or from the HTTP headers when acting as server (i.e. when using AcceptStream(StreamPeer)).

public string GetRequestedUrl()

Returns

string

GetSelectedProtocol()

Returns the selected WebSocket sub-protocol for this connection or an empty string if the sub-protocol has not been selected yet.

public string GetSelectedProtocol()

Returns

string

HasGodotClassMethod(in godot_string_name)

Check if the type contains a method with the given name. This method is used by Godot to check if a method exists before invoking it. Do not call or override this method.

protected override bool HasGodotClassMethod(in godot_string_name method)

Parameters

method godot_string_name

Name of the method to check for.

Returns

bool

HasGodotClassSignal(in godot_string_name)

Check if the type contains a signal with the given name. This method is used by Godot to check if a signal exists before raising it. Do not call or override this method.

protected override bool HasGodotClassSignal(in godot_string_name signal)

Parameters

signal godot_string_name

Name of the signal to check for.

Returns

bool

InvokeGodotClassMethod(in godot_string_name, NativeVariantPtrArgs, out godot_variant)

Invokes the method with the given name, using the given arguments. This method is used by Godot to invoke methods from the engine side. Do not call or override this method.

protected override bool InvokeGodotClassMethod(in godot_string_name method, NativeVariantPtrArgs args, out godot_variant ret)

Parameters

method godot_string_name

Name of the method to invoke.

args NativeVariantPtrArgs

Arguments to use with the invoked method.

ret godot_variant

Value returned by the invoked method.

Returns

bool

Poll()

Updates the connection state and receive incoming packets. Call this function regularly to keep it in a clean state.

public void Poll()

Send(byte[], WriteMode)

Sends the given message using the desired writeMode. When sending a string, prefer using SendText(string).

public Error Send(byte[] message, WebSocketPeer.WriteMode writeMode = WriteMode.Binary)

Parameters

message byte[]
writeMode WebSocketPeer.WriteMode

Returns

Error

SendText(string)

Sends the given message using WebSocket text mode. Prefer this method over PutPacket(byte[]) when interacting with third-party text-based API (e.g. when using Json formatted messages).

public Error SendText(string message)

Parameters

message string

Returns

Error

SetNoDelay(bool)

Disable Nagle's algorithm on the underling TCP socket (default). See SetNoDelay(bool) for more information.

Note: Not available in the Web export.

public void SetNoDelay(bool enabled)

Parameters

enabled bool

WasStringPacket()

Returns true if the last received packet was sent as a text payload. See WebSocketPeer.WriteMode.

public bool WasStringPacket()

Returns

bool