WebSocketPeer
A manually polled WebSocket connection with message boundaries.
ConnectToUrl begins a handshake; AcceptStream accepts a suitable TCP/TLS stream. Continue Poll and inspect GetReadyState: startup Error.Ok does not mean Open. Read through GetPacket/GetPacketError and distinguish text/bytes with WasStringPacket.
Close begins closing; continue polling until Closed or deadline. Raw WebSocket does not synchronize Entity instances or automatically provide SceneMultiplayer RPC. WSS requires verified TLS options and a suitable server.
Declarations checked against C# SDK 1.0.0. Expand a name to see declarations and documentation.
Constructors1
WebSocketPeerConstructor
public WebSocketPeer();Properties6
HandshakeHeadersProperty
public string[] HandshakeHeaders { get; set; }The extra HTTP headers to be sent during the WebSocket handshake. Note: Not supported in Web exports due to browser restrictions.
HeartbeatIntervalProperty
public double HeartbeatInterval { get; set; }The interval (in seconds) at which the peer will automatically send WebSocket "ping" control frames. When set to 0, no "ping" control frames will be sent.
Note: Has no effect in Web exports due to browser restrictions.
InboundBufferSizeProperty
public int InboundBufferSize { get; set; }The size of the input buffer in bytes (roughly the maximum amount of memory that will be allocated for the inbound packets).
MaxQueuedPacketsProperty
public int MaxQueuedPackets { get; set; }The maximum amount of packets that will be allowed in the queues (both inbound and outbound).
OutboundBufferSizeProperty
public int OutboundBufferSize { get; set; }The size of the input buffer in bytes (roughly the maximum amount of memory that will be allocated for the outbound packets).
SupportedProtocolsProperty
public string[] SupportedProtocols { get; set; }Proposed/accepted WebSocket subprotocols. A subprotocol name does not automatically define your game message format.
Methods31
AcceptStreamError
public Error AcceptStream(StreamPeer stream);Begins a server WebSocket handshake over a suitable connected stream. Establish TLS first for WSS; ordinary TCP does not add encryption.
| Name | Type | Default |
|---|---|---|
stream | StreamPeer | — |
ErrorClosevoid
public void Close(int code = 1000, string reason = "");Begins closing with a code/reason. Continue Poll until Closed; owner removal can interrupt the closing handshake.
| Name | Type | Default |
|---|---|---|
code | int | 1000 |
reason | string | "" |
ConnectToUrlError
public Error ConnectToUrl(string url, TlsOptions tlsClientOptions = null);Begins WS/WSS connection. After Error.Ok continue Poll until Open; use verified TLS options for WSS.
| Name | Type | Default |
|---|---|---|
url | string | — |
tlsClientOptions | TlsOptions | null |
ErrorGetCloseCodeint
public int GetCloseCode();Close code when available. Abrupt loss or failed handshake need not have normal code 1000; interpret with state/diagnostics.
intGetCloseReasonstring
public string GetCloseReason();Returns the received WebSocket close frame status reason string. Only call this method when WebSocketPeer.GetReadyState returns WebSocketPeer.State.Closed.
stringGetConnectedHoststring
public string GetConnectedHost();Returns the IP address of the connected peer. Note: Not available in the Web export.
stringGetConnectedPortushort
public ushort GetConnectedPort();Returns the remote port of the connected peer. Note: Not available in the Web export.
ushortGetCurrentOutboundBufferedAmountint
public int 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.
intGetHandshakeHeadersstring[]
public string[] GetHandshakeHeaders();string[]GetHeartbeatIntervaldouble
public double GetHeartbeatInterval();doubleGetInboundBufferSizeint
public int GetInboundBufferSize();intGetMaxQueuedPacketsint
public int GetMaxQueuedPackets();intGetOutboundBufferSizeint
public int GetOutboundBufferSize();intGetReadyStateWebSocketPeer.State
public WebSocketPeer.State GetReadyState();Returns the ready state of the connection.
WebSocketPeer.StateGetRequestedUrlstring
public string GetRequestedUrl();Returns the URL requested by this peer. The URL is derived from the url passed to WebSocketPeer.ConnectToUrl or from the HTTP headers when acting as server (i.e. when using WebSocketPeer.AcceptStream).
stringGetSelectedProtocolstring
public string GetSelectedProtocol();Returns the selected WebSocket sub-protocol for this connection or an empty string if the sub-protocol has not been selected yet.
stringGetSupportedProtocolsstring[]
public string[] GetSupportedProtocols();string[]Pollvoid
public void Poll();Advances handshake, incoming data and closing. Continue in Closing until Closed or your deadline.
Send2 overloads
public Error Send(byte[] message, WebSocketPeer.WriteMode writeMode = WebSocketPeer.WriteMode.Binary);Sends bytes with the selected WriteMode. Binary payload and UTF-8 text are distinct message types; agree the application format separately.
| Name | Type | Default |
|---|---|---|
message | byte[] | — |
writeMode | WebSocketPeer.WriteMode | WebSocketPeer.WriteMode.Binary |
Errorpublic Error Send(ReadOnlySpan<byte> message, WebSocketPeer.WriteMode writeMode);Sends bytes with the selected WriteMode. Binary payload and UTF-8 text are distinct message types; agree the application format separately.
| Name | Type | Default |
|---|---|---|
message | ReadOnlySpan<byte> | — |
writeMode | WebSocketPeer.WriteMode | — |
ErrorSendTextError
public Error SendText(string message);Sends UTF-8 text on an open connection. Check Error and an application byte limit; successful sending does not establish server-side action completion.
| Name | Type | Default |
|---|---|---|
message | string | — |
ErrorSetHandshakeHeaders2 overloads
public void SetHandshakeHeaders(ReadOnlySpan<string> protocols);| Name | Type | Default |
|---|---|---|
protocols | ReadOnlySpan<string> | — |
public void SetHandshakeHeaders(string[] protocols);| Name | Type | Default |
|---|---|---|
protocols | string[] | — |
SetHeartbeatIntervalvoid
public void SetHeartbeatInterval(double interval);| Name | Type | Default |
|---|---|---|
interval | double | — |
SetInboundBufferSizevoid
public void SetInboundBufferSize(int bufferSize);| Name | Type | Default |
|---|---|---|
bufferSize | int | — |
SetMaxQueuedPacketsvoid
public void SetMaxQueuedPackets(int bufferSize);| Name | Type | Default |
|---|---|---|
bufferSize | int | — |
SetNoDelayvoid
public void SetNoDelay(bool enabled);Disable Nagle's algorithm on the underlying TCP socket (default). See StreamPeerTcp.SetNoDelay for more information.
Note: Not available in the Web export.
| Name | Type | Default |
|---|---|---|
enabled | bool | — |
SetOutboundBufferSizevoid
public void SetOutboundBufferSize(int bufferSize);| Name | Type | Default |
|---|---|---|
bufferSize | int | — |
SetSupportedProtocols2 overloads
public void SetSupportedProtocols(ReadOnlySpan<string> protocols);| Name | Type | Default |
|---|---|---|
protocols | ReadOnlySpan<string> | — |
public void SetSupportedProtocols(string[] protocols);| Name | Type | Default |
|---|---|---|
protocols | string[] | — |
WasStringPacketbool
public bool WasStringPacket();Reports the last received packet type. Inspect after GetPacket; it does not describe every future message on the connection.
boolWebSocketPeer.State
| Value | Description |
|---|---|
Closed = 3 | The connection is closed or couldn't be opened. |
Closing = 2 | The connection is in the process of closing. This means a close request has been sent to the remote peer but confirmation has not been received. |
Connecting = 0 | Socket has been created. The connection is not yet open. |
Open = 1 | The connection is open and ready to communicate. |
WebSocketPeer.WriteMode
| Value | Description |
|---|---|
Binary = 1 | Specifies that WebSockets messages should be transferred as binary payload (any byte combination is allowed). |
Text = 0 | Specifies that WebSockets messages should be transferred as text payload (only valid UTF-8 is allowed). |