123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504 |
- :github_url: hide
- .. DO NOT EDIT THIS FILE!!!
- .. Generated automatically from Godot engine sources.
- .. Generator: https://github.com/godotengine/godot/tree/4.2/doc/tools/make_rst.py.
- .. XML source: https://github.com/godotengine/godot/tree/4.2/modules/websocket/doc_classes/WebSocketPeer.xml.
- .. _class_WebSocketPeer:
- WebSocketPeer
- =============
- **Inherits:** :ref:`PacketPeer<class_PacketPeer>` **<** :ref:`RefCounted<class_RefCounted>` **<** :ref:`Object<class_Object>`
- A WebSocket connection.
- .. rst-class:: classref-introduction-group
- Description
- -----------
- 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 :ref:`PacketPeer.put_packet<class_PacketPeer_method_put_packet>`, and WebSocket text frames using :ref:`send<class_WebSocketPeer_method_send>` (prefer text frames when interacting with text-based API). You can check the frame type of the last packet via :ref:`was_string_packet<class_WebSocketPeer_method_was_string_packet>`.
- To start a WebSocket client, first call :ref:`connect_to_url<class_WebSocketPeer_method_connect_to_url>`, then regularly call :ref:`poll<class_WebSocketPeer_method_poll>` (e.g. during :ref:`Node<class_Node>` process). You can query the socket state via :ref:`get_ready_state<class_WebSocketPeer_method_get_ready_state>`, get the number of pending packets using :ref:`PacketPeer.get_available_packet_count<class_PacketPeer_method_get_available_packet_count>`, and retrieve them via :ref:`PacketPeer.get_packet<class_PacketPeer_method_get_packet>`.
- .. tabs::
- .. code-tab:: 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.
- To use the peer as part of a WebSocket server refer to :ref:`accept_stream<class_WebSocketPeer_method_accept_stream>` and the online tutorial.
- .. rst-class:: classref-reftable-group
- Properties
- ----------
- .. table::
- :widths: auto
- +---------------------------------------------------+--------------------------------------------------------------------------------+-------------------------+
- | :ref:`PackedStringArray<class_PackedStringArray>` | :ref:`handshake_headers<class_WebSocketPeer_property_handshake_headers>` | ``PackedStringArray()`` |
- +---------------------------------------------------+--------------------------------------------------------------------------------+-------------------------+
- | :ref:`int<class_int>` | :ref:`inbound_buffer_size<class_WebSocketPeer_property_inbound_buffer_size>` | ``65535`` |
- +---------------------------------------------------+--------------------------------------------------------------------------------+-------------------------+
- | :ref:`int<class_int>` | :ref:`max_queued_packets<class_WebSocketPeer_property_max_queued_packets>` | ``2048`` |
- +---------------------------------------------------+--------------------------------------------------------------------------------+-------------------------+
- | :ref:`int<class_int>` | :ref:`outbound_buffer_size<class_WebSocketPeer_property_outbound_buffer_size>` | ``65535`` |
- +---------------------------------------------------+--------------------------------------------------------------------------------+-------------------------+
- | :ref:`PackedStringArray<class_PackedStringArray>` | :ref:`supported_protocols<class_WebSocketPeer_property_supported_protocols>` | ``PackedStringArray()`` |
- +---------------------------------------------------+--------------------------------------------------------------------------------+-------------------------+
- .. rst-class:: classref-reftable-group
- Methods
- -------
- .. table::
- :widths: auto
- +----------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
- | :ref:`Error<enum_@GlobalScope_Error>` | :ref:`accept_stream<class_WebSocketPeer_method_accept_stream>` **(** :ref:`StreamPeer<class_StreamPeer>` stream **)** |
- +----------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
- | void | :ref:`close<class_WebSocketPeer_method_close>` **(** :ref:`int<class_int>` code=1000, :ref:`String<class_String>` reason="" **)** |
- +----------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
- | :ref:`Error<enum_@GlobalScope_Error>` | :ref:`connect_to_url<class_WebSocketPeer_method_connect_to_url>` **(** :ref:`String<class_String>` url, :ref:`TLSOptions<class_TLSOptions>` tls_client_options=null **)** |
- +----------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
- | :ref:`int<class_int>` | :ref:`get_close_code<class_WebSocketPeer_method_get_close_code>` **(** **)** |const| |
- +----------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
- | :ref:`String<class_String>` | :ref:`get_close_reason<class_WebSocketPeer_method_get_close_reason>` **(** **)** |const| |
- +----------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
- | :ref:`String<class_String>` | :ref:`get_connected_host<class_WebSocketPeer_method_get_connected_host>` **(** **)** |const| |
- +----------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
- | :ref:`int<class_int>` | :ref:`get_connected_port<class_WebSocketPeer_method_get_connected_port>` **(** **)** |const| |
- +----------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
- | :ref:`int<class_int>` | :ref:`get_current_outbound_buffered_amount<class_WebSocketPeer_method_get_current_outbound_buffered_amount>` **(** **)** |const| |
- +----------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
- | :ref:`State<enum_WebSocketPeer_State>` | :ref:`get_ready_state<class_WebSocketPeer_method_get_ready_state>` **(** **)** |const| |
- +----------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
- | :ref:`String<class_String>` | :ref:`get_requested_url<class_WebSocketPeer_method_get_requested_url>` **(** **)** |const| |
- +----------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
- | :ref:`String<class_String>` | :ref:`get_selected_protocol<class_WebSocketPeer_method_get_selected_protocol>` **(** **)** |const| |
- +----------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
- | void | :ref:`poll<class_WebSocketPeer_method_poll>` **(** **)** |
- +----------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
- | :ref:`Error<enum_@GlobalScope_Error>` | :ref:`send<class_WebSocketPeer_method_send>` **(** :ref:`PackedByteArray<class_PackedByteArray>` message, :ref:`WriteMode<enum_WebSocketPeer_WriteMode>` write_mode=1 **)** |
- +----------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
- | :ref:`Error<enum_@GlobalScope_Error>` | :ref:`send_text<class_WebSocketPeer_method_send_text>` **(** :ref:`String<class_String>` message **)** |
- +----------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
- | void | :ref:`set_no_delay<class_WebSocketPeer_method_set_no_delay>` **(** :ref:`bool<class_bool>` enabled **)** |
- +----------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
- | :ref:`bool<class_bool>` | :ref:`was_string_packet<class_WebSocketPeer_method_was_string_packet>` **(** **)** |const| |
- +----------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
- .. rst-class:: classref-section-separator
- ----
- .. rst-class:: classref-descriptions-group
- Enumerations
- ------------
- .. _enum_WebSocketPeer_WriteMode:
- .. rst-class:: classref-enumeration
- enum **WriteMode**:
- .. _class_WebSocketPeer_constant_WRITE_MODE_TEXT:
- .. rst-class:: classref-enumeration-constant
- :ref:`WriteMode<enum_WebSocketPeer_WriteMode>` **WRITE_MODE_TEXT** = ``0``
- Specifies that WebSockets messages should be transferred as text payload (only valid UTF-8 is allowed).
- .. _class_WebSocketPeer_constant_WRITE_MODE_BINARY:
- .. rst-class:: classref-enumeration-constant
- :ref:`WriteMode<enum_WebSocketPeer_WriteMode>` **WRITE_MODE_BINARY** = ``1``
- Specifies that WebSockets messages should be transferred as binary payload (any byte combination is allowed).
- .. rst-class:: classref-item-separator
- ----
- .. _enum_WebSocketPeer_State:
- .. rst-class:: classref-enumeration
- enum **State**:
- .. _class_WebSocketPeer_constant_STATE_CONNECTING:
- .. rst-class:: classref-enumeration-constant
- :ref:`State<enum_WebSocketPeer_State>` **STATE_CONNECTING** = ``0``
- Socket has been created. The connection is not yet open.
- .. _class_WebSocketPeer_constant_STATE_OPEN:
- .. rst-class:: classref-enumeration-constant
- :ref:`State<enum_WebSocketPeer_State>` **STATE_OPEN** = ``1``
- The connection is open and ready to communicate.
- .. _class_WebSocketPeer_constant_STATE_CLOSING:
- .. rst-class:: classref-enumeration-constant
- :ref:`State<enum_WebSocketPeer_State>` **STATE_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.
- .. _class_WebSocketPeer_constant_STATE_CLOSED:
- .. rst-class:: classref-enumeration-constant
- :ref:`State<enum_WebSocketPeer_State>` **STATE_CLOSED** = ``3``
- The connection is closed or couldn't be opened.
- .. rst-class:: classref-section-separator
- ----
- .. rst-class:: classref-descriptions-group
- Property Descriptions
- ---------------------
- .. _class_WebSocketPeer_property_handshake_headers:
- .. rst-class:: classref-property
- :ref:`PackedStringArray<class_PackedStringArray>` **handshake_headers** = ``PackedStringArray()``
- .. rst-class:: classref-property-setget
- - void **set_handshake_headers** **(** :ref:`PackedStringArray<class_PackedStringArray>` value **)**
- - :ref:`PackedStringArray<class_PackedStringArray>` **get_handshake_headers** **(** **)**
- The extra HTTP headers to be sent during the WebSocket handshake.
- \ **Note:** Not supported in Web exports due to browsers' restrictions.
- .. rst-class:: classref-item-separator
- ----
- .. _class_WebSocketPeer_property_inbound_buffer_size:
- .. rst-class:: classref-property
- :ref:`int<class_int>` **inbound_buffer_size** = ``65535``
- .. rst-class:: classref-property-setget
- - void **set_inbound_buffer_size** **(** :ref:`int<class_int>` value **)**
- - :ref:`int<class_int>` **get_inbound_buffer_size** **(** **)**
- The size of the input buffer in bytes (roughly the maximum amount of memory that will be allocated for the inbound packets).
- .. rst-class:: classref-item-separator
- ----
- .. _class_WebSocketPeer_property_max_queued_packets:
- .. rst-class:: classref-property
- :ref:`int<class_int>` **max_queued_packets** = ``2048``
- .. rst-class:: classref-property-setget
- - void **set_max_queued_packets** **(** :ref:`int<class_int>` value **)**
- - :ref:`int<class_int>` **get_max_queued_packets** **(** **)**
- The maximum amount of packets that will be allowed in the queues (both inbound and outbound).
- .. rst-class:: classref-item-separator
- ----
- .. _class_WebSocketPeer_property_outbound_buffer_size:
- .. rst-class:: classref-property
- :ref:`int<class_int>` **outbound_buffer_size** = ``65535``
- .. rst-class:: classref-property-setget
- - void **set_outbound_buffer_size** **(** :ref:`int<class_int>` value **)**
- - :ref:`int<class_int>` **get_outbound_buffer_size** **(** **)**
- The size of the input buffer in bytes (roughly the maximum amount of memory that will be allocated for the outbound packets).
- .. rst-class:: classref-item-separator
- ----
- .. _class_WebSocketPeer_property_supported_protocols:
- .. rst-class:: classref-property
- :ref:`PackedStringArray<class_PackedStringArray>` **supported_protocols** = ``PackedStringArray()``
- .. rst-class:: classref-property-setget
- - void **set_supported_protocols** **(** :ref:`PackedStringArray<class_PackedStringArray>` value **)**
- - :ref:`PackedStringArray<class_PackedStringArray>` **get_supported_protocols** **(** **)**
- The WebSocket sub-protocols allowed during the WebSocket handshake.
- .. rst-class:: classref-section-separator
- ----
- .. rst-class:: classref-descriptions-group
- Method Descriptions
- -------------------
- .. _class_WebSocketPeer_method_accept_stream:
- .. rst-class:: classref-method
- :ref:`Error<enum_@GlobalScope_Error>` **accept_stream** **(** :ref:`StreamPeer<class_StreamPeer>` stream **)**
- Accepts a peer connection performing the HTTP handshake as a WebSocket server. The ``stream`` must be a valid TCP stream retrieved via :ref:`TCPServer.take_connection<class_TCPServer_method_take_connection>`, or a TLS stream accepted via :ref:`StreamPeerTLS.accept_stream<class_StreamPeerTLS_method_accept_stream>`.
- \ **Note:** Not supported in Web exports due to browsers' restrictions.
- .. rst-class:: classref-item-separator
- ----
- .. _class_WebSocketPeer_method_close:
- .. rst-class:: classref-method
- void **close** **(** :ref:`int<class_int>` code=1000, :ref:`String<class_String>` reason="" **)**
- 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 :ref:`STATE_CLOSED<class_WebSocketPeer_constant_STATE_CLOSED>` is reached.
- \ **Note:** The Web export might not support all status codes. Please refer to browser-specific documentation for more details.
- .. rst-class:: classref-item-separator
- ----
- .. _class_WebSocketPeer_method_connect_to_url:
- .. rst-class:: classref-method
- :ref:`Error<enum_@GlobalScope_Error>` **connect_to_url** **(** :ref:`String<class_String>` url, :ref:`TLSOptions<class_TLSOptions>` tls_client_options=null **)**
- Connects to the given URL. TLS certificates will be verified against the hostname when connecting using the ``wss://`` protocol. You can pass the optional ``tls_client_options`` parameter to customize the trusted certification authorities, or disable the common name verification. See :ref:`TLSOptions.client<class_TLSOptions_method_client>` and :ref:`TLSOptions.client_unsafe<class_TLSOptions_method_client_unsafe>`.
- \ **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.
- .. rst-class:: classref-item-separator
- ----
- .. _class_WebSocketPeer_method_get_close_code:
- .. rst-class:: classref-method
- :ref:`int<class_int>` **get_close_code** **(** **)** |const|
- Returns the received WebSocket close frame status code, or ``-1`` when the connection was not cleanly closed. Only call this method when :ref:`get_ready_state<class_WebSocketPeer_method_get_ready_state>` returns :ref:`STATE_CLOSED<class_WebSocketPeer_constant_STATE_CLOSED>`.
- .. rst-class:: classref-item-separator
- ----
- .. _class_WebSocketPeer_method_get_close_reason:
- .. rst-class:: classref-method
- :ref:`String<class_String>` **get_close_reason** **(** **)** |const|
- Returns the received WebSocket close frame status reason string. Only call this method when :ref:`get_ready_state<class_WebSocketPeer_method_get_ready_state>` returns :ref:`STATE_CLOSED<class_WebSocketPeer_constant_STATE_CLOSED>`.
- .. rst-class:: classref-item-separator
- ----
- .. _class_WebSocketPeer_method_get_connected_host:
- .. rst-class:: classref-method
- :ref:`String<class_String>` **get_connected_host** **(** **)** |const|
- Returns the IP address of the connected peer.
- \ **Note:** Not available in the Web export.
- .. rst-class:: classref-item-separator
- ----
- .. _class_WebSocketPeer_method_get_connected_port:
- .. rst-class:: classref-method
- :ref:`int<class_int>` **get_connected_port** **(** **)** |const|
- Returns the remote port of the connected peer.
- \ **Note:** Not available in the Web export.
- .. rst-class:: classref-item-separator
- ----
- .. _class_WebSocketPeer_method_get_current_outbound_buffered_amount:
- .. rst-class:: classref-method
- :ref:`int<class_int>` **get_current_outbound_buffered_amount** **(** **)** |const|
- Returns the current amount of data in the outbound websocket buffer. **Note:** Web exports use WebSocket.bufferedAmount, while other platforms use an internal buffer.
- .. rst-class:: classref-item-separator
- ----
- .. _class_WebSocketPeer_method_get_ready_state:
- .. rst-class:: classref-method
- :ref:`State<enum_WebSocketPeer_State>` **get_ready_state** **(** **)** |const|
- Returns the ready state of the connection. See :ref:`State<enum_WebSocketPeer_State>`.
- .. rst-class:: classref-item-separator
- ----
- .. _class_WebSocketPeer_method_get_requested_url:
- .. rst-class:: classref-method
- :ref:`String<class_String>` **get_requested_url** **(** **)** |const|
- Returns the URL requested by this peer. The URL is derived from the ``url`` passed to :ref:`connect_to_url<class_WebSocketPeer_method_connect_to_url>` or from the HTTP headers when acting as server (i.e. when using :ref:`accept_stream<class_WebSocketPeer_method_accept_stream>`).
- .. rst-class:: classref-item-separator
- ----
- .. _class_WebSocketPeer_method_get_selected_protocol:
- .. rst-class:: classref-method
- :ref:`String<class_String>` **get_selected_protocol** **(** **)** |const|
- Returns the selected WebSocket sub-protocol for this connection or an empty string if the sub-protocol has not been selected yet.
- .. rst-class:: classref-item-separator
- ----
- .. _class_WebSocketPeer_method_poll:
- .. rst-class:: classref-method
- void **poll** **(** **)**
- Updates the connection state and receive incoming packets. Call this function regularly to keep it in a clean state.
- .. rst-class:: classref-item-separator
- ----
- .. _class_WebSocketPeer_method_send:
- .. rst-class:: classref-method
- :ref:`Error<enum_@GlobalScope_Error>` **send** **(** :ref:`PackedByteArray<class_PackedByteArray>` message, :ref:`WriteMode<enum_WebSocketPeer_WriteMode>` write_mode=1 **)**
- Sends the given ``message`` using the desired ``write_mode``. When sending a :ref:`String<class_String>`, prefer using :ref:`send_text<class_WebSocketPeer_method_send_text>`.
- .. rst-class:: classref-item-separator
- ----
- .. _class_WebSocketPeer_method_send_text:
- .. rst-class:: classref-method
- :ref:`Error<enum_@GlobalScope_Error>` **send_text** **(** :ref:`String<class_String>` message **)**
- Sends the given ``message`` using WebSocket text mode. Prefer this method over :ref:`PacketPeer.put_packet<class_PacketPeer_method_put_packet>` when interacting with third-party text-based API (e.g. when using :ref:`JSON<class_JSON>` formatted messages).
- .. rst-class:: classref-item-separator
- ----
- .. _class_WebSocketPeer_method_set_no_delay:
- .. rst-class:: classref-method
- void **set_no_delay** **(** :ref:`bool<class_bool>` enabled **)**
- Disable Nagle's algorithm on the underling TCP socket (default). See :ref:`StreamPeerTCP.set_no_delay<class_StreamPeerTCP_method_set_no_delay>` for more information.
- \ **Note:** Not available in the Web export.
- .. rst-class:: classref-item-separator
- ----
- .. _class_WebSocketPeer_method_was_string_packet:
- .. rst-class:: classref-method
- :ref:`bool<class_bool>` **was_string_packet** **(** **)** |const|
- Returns ``true`` if the last received packet was sent as a text payload. See :ref:`WriteMode<enum_WebSocketPeer_WriteMode>`.
- .. |virtual| replace:: :abbr:`virtual (This method should typically be overridden by the user to have any effect.)`
- .. |const| replace:: :abbr:`const (This method has no side effects. It doesn't modify any of the instance's member variables.)`
- .. |vararg| replace:: :abbr:`vararg (This method accepts any number of arguments after the ones described here.)`
- .. |constructor| replace:: :abbr:`constructor (This method is used to construct a type.)`
- .. |static| replace:: :abbr:`static (This method doesn't need an instance to be called, so it can be called directly using the class name.)`
- .. |operator| replace:: :abbr:`operator (This method describes a valid operator to use with this type as left-hand operand.)`
- .. |bitfield| replace:: :abbr:`BitField (This value is an integer composed as a bitmask of the following flags.)`
|