Documentation
¶
Overview ¶
Package WebSocketPeer provides methods for working with WebSocketPeer object instances.
Index ¶
- type Advanced
- type Any
- type Expanded
- type Extension
- type ID
- type Instance
- func (self Instance) AcceptStream(stream StreamPeer.Instance) error
- func (self Instance) AsObject() [1]gd.Object
- func (self Instance) AsPacketPeer() PacketPeer.Instance
- func (self Instance) AsRefCounted() [1]gd.RefCounted
- func (self Instance) AsWebSocketPeer() Instance
- func (self Instance) Close()
- func (self Instance) ConnectToUrl(url string) error
- func (self Instance) GetCloseCode() int
- func (self Instance) GetCloseReason() string
- func (self Instance) GetConnectedHost() string
- func (self Instance) GetConnectedPort() int
- func (self Instance) GetCurrentOutboundBufferedAmount() int
- func (self Instance) GetReadyState() State
- func (self Instance) GetRequestedUrl() string
- func (self Instance) GetSelectedProtocol() string
- func (self Instance) HandshakeHeaders() []string
- func (self Instance) HeartbeatInterval() Float.X
- func (self Instance) ID() ID
- func (self Instance) InboundBufferSize() int
- func (self Instance) MaxQueuedPackets() int
- func (self Instance) OutboundBufferSize() int
- func (self Instance) Poll()
- func (self Instance) Send(message []byte) error
- func (self Instance) SendText(message string) error
- func (self Instance) SetHandshakeHeaders(value []string)
- func (self Instance) SetHeartbeatInterval(value Float.X)
- func (self Instance) SetInboundBufferSize(value int)
- func (self Instance) SetMaxQueuedPackets(value int)
- func (self Instance) SetNoDelay(enabled bool)
- func (self *Instance) SetObject(obj [1]gd.Object) bool
- func (self Instance) SetOutboundBufferSize(value int)
- func (self Instance) SetSupportedProtocols(value []string)
- func (self Instance) SupportedProtocols() []string
- func (self Instance) Virtual(name string) reflect.Value
- func (self Instance) WasStringPacket() bool
- type State
- type WriteMode
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Advanced ¶
type Advanced = class
Advanced exposes a 1:1 low-level instance of the class, undocumented, for those who know what they are doing.
type Expanded ¶
type Expanded [1]gdclass.WebSocketPeer
func (Expanded) Close ¶
Closes this WebSocket connection. [param code] is the status code for the closure (see RFC 6455 section 7.4 for a list of valid status codes). [param reason] is the human readable reason for closing the connection (can be any UTF-8 string that's smaller than 123 bytes). If [param code] is negative, the connection will be closed immediately without notifying the remote peer. [b]Note:[/b] To achieve a clean close, you will need to keep polling until [constant STATE_CLOSED] is reached. [b]Note:[/b] The Web export might not support all status codes. Please refer to browser-specific documentation for more details.
func (Expanded) ConnectToUrl ¶
func (self Expanded) ConnectToUrl(url string, tls_client_options TLSOptions.Instance) error
Connects to the given URL. TLS certificates will be verified against the hostname when connecting using the [code]wss://[/code] protocol. You can pass the optional [param tls_client_options] parameter to customize the trusted certification authorities, or disable the common name verification. See [method TLSOptions.client] and [method TLSOptions.client_unsafe]. [b]Note:[/b] This method is non-blocking, and will return [constant OK] before the connection is established as long as the provided parameters are valid and the peer is not in an invalid state (e.g. already connected). Regularly call [method poll] (e.g. during [Node] process) and check the result of [method get_ready_state] to know whether the connection succeeds or fails. [b]Note:[/b] To avoid mixed content warnings or errors in Web, you may have to use a [param url] that starts with [code]wss://[/code] (secure) instead of [code]ws://[/code]. 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 [code]wss://[/code] connections, as it won't match with the TLS certificate.
type Extension ¶
Extension can be embedded in a new struct to create an extension of this class. T should be the type that is embedding this Extension
func (*Extension[T]) AsPacketPeer ¶
func (self *Extension[T]) AsPacketPeer() PacketPeer.Instance
func (*Extension[T]) AsRefCounted ¶
func (self *Extension[T]) AsRefCounted() [1]gd.RefCounted
func (*Extension[T]) AsWebSocketPeer ¶
type ID ¶
ID is a typed object ID (reference) to an instance of this class, use it to store references to objects with unknown lifetimes, as an ID will not panic on use if the underlying object has been destroyed.
type Instance ¶
type Instance [1]gdclass.WebSocketPeer
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 [method PacketPeer.put_packet], and WebSocket text frames using [method send] (prefer text frames when interacting with text-based API). You can check the frame type of the last packet via [method was_string_packet]. To start a WebSocket client, first call [method connect_to_url], then regularly call [method poll] (e.g. during [Node] process). You can query the socket state via [method get_ready_state], get the number of pending packets using [method PacketPeer.get_available_packet_count], and retrieve them via [method PacketPeer.get_packet]. [codeblocks] [gdscript] extends Node
var socket = WebSocketPeer.new()
func _ready():
socket.connect_to_url("wss://example.com")
func _process(delta):
socket.poll() var state = socket.get_ready_state() if state == WebSocketPeer.STATE_OPEN: while socket.get_available_packet_count(): print("Packet: ", socket.get_packet()) elif state == WebSocketPeer.STATE_CLOSING: # Keep polling to achieve proper close. pass elif state == WebSocketPeer.STATE_CLOSED: var code = socket.get_close_code() var reason = socket.get_close_reason() print("WebSocket closed with code: %d, reason %s. Clean: %s" % [code, reason, code != -1]) set_process(false) # Stop processing.
[/gdscript] [/codeblocks] To use the peer as part of a WebSocket server refer to [method accept_stream] and the online tutorial.
var Nil Instance
Nil is a nil/null instance of the class. Equivalent to the zero value.
func (Instance) AcceptStream ¶
func (self Instance) AcceptStream(stream StreamPeer.Instance) error
Accepts a peer connection performing the HTTP handshake as a WebSocket server. The [param stream] must be a valid TCP stream retrieved via [method TCPServer.take_connection], or a TLS stream accepted via [method StreamPeerTLS.accept_stream]. [b]Note:[/b] Not supported in Web exports due to browsers' restrictions.
func (Instance) AsPacketPeer ¶
func (self Instance) AsPacketPeer() PacketPeer.Instance
func (Instance) AsRefCounted ¶
func (self Instance) AsRefCounted() [1]gd.RefCounted
func (Instance) AsWebSocketPeer ¶
func (Instance) Close ¶
func (self Instance) Close()
Closes this WebSocket connection. [param code] is the status code for the closure (see RFC 6455 section 7.4 for a list of valid status codes). [param reason] is the human readable reason for closing the connection (can be any UTF-8 string that's smaller than 123 bytes). If [param code] is negative, the connection will be closed immediately without notifying the remote peer. [b]Note:[/b] To achieve a clean close, you will need to keep polling until [constant STATE_CLOSED] is reached. [b]Note:[/b] The Web export might not support all status codes. Please refer to browser-specific documentation for more details.
func (Instance) ConnectToUrl ¶
Connects to the given URL. TLS certificates will be verified against the hostname when connecting using the [code]wss://[/code] protocol. You can pass the optional [param tls_client_options] parameter to customize the trusted certification authorities, or disable the common name verification. See [method TLSOptions.client] and [method TLSOptions.client_unsafe]. [b]Note:[/b] This method is non-blocking, and will return [constant OK] before the connection is established as long as the provided parameters are valid and the peer is not in an invalid state (e.g. already connected). Regularly call [method poll] (e.g. during [Node] process) and check the result of [method get_ready_state] to know whether the connection succeeds or fails. [b]Note:[/b] To avoid mixed content warnings or errors in Web, you may have to use a [param url] that starts with [code]wss://[/code] (secure) instead of [code]ws://[/code]. 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 [code]wss://[/code] connections, as it won't match with the TLS certificate.
func (Instance) GetCloseCode ¶
Returns the received WebSocket close frame status code, or [code]-1[/code] when the connection was not cleanly closed. Only call this method when [method get_ready_state] returns [constant STATE_CLOSED].
func (Instance) GetCloseReason ¶
Returns the received WebSocket close frame status reason string. Only call this method when [method get_ready_state] returns [constant STATE_CLOSED].
func (Instance) GetConnectedHost ¶
Returns the IP address of the connected peer. [b]Note:[/b] Not available in the Web export.
func (Instance) GetConnectedPort ¶
Returns the remote port of the connected peer. [b]Note:[/b] Not available in the Web export.
func (Instance) GetCurrentOutboundBufferedAmount ¶
Returns the current amount of data in the outbound websocket buffer. [b]Note:[/b] Web exports use WebSocket.bufferedAmount, while other platforms use an internal buffer.
func (Instance) GetReadyState ¶
Returns the ready state of the connection. See [enum State].
func (Instance) GetRequestedUrl ¶
Returns the URL requested by this peer. The URL is derived from the [code]url[/code] passed to [method connect_to_url] or from the HTTP headers when acting as server (i.e. when using [method accept_stream]).
func (Instance) GetSelectedProtocol ¶
Returns the selected WebSocket sub-protocol for this connection or an empty string if the sub-protocol has not been selected yet.
func (Instance) HandshakeHeaders ¶
func (Instance) HeartbeatInterval ¶
func (Instance) InboundBufferSize ¶
func (Instance) MaxQueuedPackets ¶
func (Instance) OutboundBufferSize ¶
func (Instance) Poll ¶
func (self Instance) Poll()
Updates the connection state and receive incoming packets. Call this function regularly to keep it in a clean state.
func (Instance) Send ¶
Sends the given [param message] using the desired [param write_mode]. When sending a [String], prefer using [method send_text].
func (Instance) SendText ¶
Sends the given [param message] using WebSocket text mode. Prefer this method over [method PacketPeer.put_packet] when interacting with third-party text-based API (e.g. when using [JSON] formatted messages).
func (Instance) SetHandshakeHeaders ¶
func (Instance) SetHeartbeatInterval ¶
func (Instance) SetInboundBufferSize ¶
func (Instance) SetMaxQueuedPackets ¶
func (Instance) SetNoDelay ¶
Disable Nagle's algorithm on the underlying TCP socket (default). See [method StreamPeerTCP.set_no_delay] for more information. [b]Note:[/b] Not available in the Web export.
func (Instance) SetOutboundBufferSize ¶
func (Instance) SetSupportedProtocols ¶
func (Instance) SupportedProtocols ¶
func (Instance) WasStringPacket ¶
Returns [code]true[/code] if the last received packet was sent as a text payload. See [enum WriteMode].
type State ¶
type State int //gd:WebSocketPeer.State
const ( /*Socket has been created. The connection is not yet open.*/ StateConnecting State = 0 /*The connection is open and ready to communicate.*/ StateOpen State = 1 /*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.*/ StateClosing State = 2 /*The connection is closed or couldn't be opened.*/ StateClosed State = 3 )