class_websocketpeer.rst 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533
  1. :github_url: hide
  2. .. DO NOT EDIT THIS FILE!!!
  3. .. Generated automatically from Godot engine sources.
  4. .. Generator: https://github.com/godotengine/godot/tree/master/doc/tools/make_rst.py.
  5. .. XML source: https://github.com/godotengine/godot/tree/master/modules/websocket/doc_classes/WebSocketPeer.xml.
  6. .. _class_WebSocketPeer:
  7. WebSocketPeer
  8. =============
  9. **Inherits:** :ref:`PacketPeer<class_PacketPeer>` **<** :ref:`RefCounted<class_RefCounted>` **<** :ref:`Object<class_Object>`
  10. A WebSocket connection.
  11. .. rst-class:: classref-introduction-group
  12. Description
  13. -----------
  14. 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.
  15. 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>`.
  16. 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>`.
  17. .. tabs::
  18. .. code-tab:: gdscript
  19. extends Node
  20. var socket = WebSocketPeer.new()
  21. func _ready():
  22. socket.connect_to_url("wss://example.com")
  23. func _process(delta):
  24. socket.poll()
  25. var state = socket.get_ready_state()
  26. if state == WebSocketPeer.STATE_OPEN:
  27. while socket.get_available_packet_count():
  28. print("Packet: ", socket.get_packet())
  29. elif state == WebSocketPeer.STATE_CLOSING:
  30. # Keep polling to achieve proper close.
  31. pass
  32. elif state == WebSocketPeer.STATE_CLOSED:
  33. var code = socket.get_close_code()
  34. var reason = socket.get_close_reason()
  35. print("WebSocket closed with code: %d, reason %s. Clean: %s" % [code, reason, code != -1])
  36. set_process(false) # Stop processing.
  37. To use the peer as part of a WebSocket server refer to :ref:`accept_stream()<class_WebSocketPeer_method_accept_stream>` and the online tutorial.
  38. .. rst-class:: classref-reftable-group
  39. Properties
  40. ----------
  41. .. table::
  42. :widths: auto
  43. +---------------------------------------------------+--------------------------------------------------------------------------------+-------------------------+
  44. | :ref:`PackedStringArray<class_PackedStringArray>` | :ref:`handshake_headers<class_WebSocketPeer_property_handshake_headers>` | ``PackedStringArray()`` |
  45. +---------------------------------------------------+--------------------------------------------------------------------------------+-------------------------+
  46. | :ref:`float<class_float>` | :ref:`heartbeat_interval<class_WebSocketPeer_property_heartbeat_interval>` | ``0.0`` |
  47. +---------------------------------------------------+--------------------------------------------------------------------------------+-------------------------+
  48. | :ref:`int<class_int>` | :ref:`inbound_buffer_size<class_WebSocketPeer_property_inbound_buffer_size>` | ``65535`` |
  49. +---------------------------------------------------+--------------------------------------------------------------------------------+-------------------------+
  50. | :ref:`int<class_int>` | :ref:`max_queued_packets<class_WebSocketPeer_property_max_queued_packets>` | ``4096`` |
  51. +---------------------------------------------------+--------------------------------------------------------------------------------+-------------------------+
  52. | :ref:`int<class_int>` | :ref:`outbound_buffer_size<class_WebSocketPeer_property_outbound_buffer_size>` | ``65535`` |
  53. +---------------------------------------------------+--------------------------------------------------------------------------------+-------------------------+
  54. | :ref:`PackedStringArray<class_PackedStringArray>` | :ref:`supported_protocols<class_WebSocketPeer_property_supported_protocols>` | ``PackedStringArray()`` |
  55. +---------------------------------------------------+--------------------------------------------------------------------------------+-------------------------+
  56. .. rst-class:: classref-reftable-group
  57. Methods
  58. -------
  59. .. table::
  60. :widths: auto
  61. +----------------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  62. | :ref:`Error<enum_@GlobalScope_Error>` | :ref:`accept_stream<class_WebSocketPeer_method_accept_stream>`\ (\ stream\: :ref:`StreamPeer<class_StreamPeer>`\ ) |
  63. +----------------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  64. | |void| | :ref:`close<class_WebSocketPeer_method_close>`\ (\ code\: :ref:`int<class_int>` = 1000, reason\: :ref:`String<class_String>` = ""\ ) |
  65. +----------------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  66. | :ref:`Error<enum_@GlobalScope_Error>` | :ref:`connect_to_url<class_WebSocketPeer_method_connect_to_url>`\ (\ url\: :ref:`String<class_String>`, tls_client_options\: :ref:`TLSOptions<class_TLSOptions>` = null\ ) |
  67. +----------------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  68. | :ref:`int<class_int>` | :ref:`get_close_code<class_WebSocketPeer_method_get_close_code>`\ (\ ) |const| |
  69. +----------------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  70. | :ref:`String<class_String>` | :ref:`get_close_reason<class_WebSocketPeer_method_get_close_reason>`\ (\ ) |const| |
  71. +----------------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  72. | :ref:`String<class_String>` | :ref:`get_connected_host<class_WebSocketPeer_method_get_connected_host>`\ (\ ) |const| |
  73. +----------------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  74. | :ref:`int<class_int>` | :ref:`get_connected_port<class_WebSocketPeer_method_get_connected_port>`\ (\ ) |const| |
  75. +----------------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  76. | :ref:`int<class_int>` | :ref:`get_current_outbound_buffered_amount<class_WebSocketPeer_method_get_current_outbound_buffered_amount>`\ (\ ) |const| |
  77. +----------------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  78. | :ref:`State<enum_WebSocketPeer_State>` | :ref:`get_ready_state<class_WebSocketPeer_method_get_ready_state>`\ (\ ) |const| |
  79. +----------------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  80. | :ref:`String<class_String>` | :ref:`get_requested_url<class_WebSocketPeer_method_get_requested_url>`\ (\ ) |const| |
  81. +----------------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  82. | :ref:`String<class_String>` | :ref:`get_selected_protocol<class_WebSocketPeer_method_get_selected_protocol>`\ (\ ) |const| |
  83. +----------------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  84. | |void| | :ref:`poll<class_WebSocketPeer_method_poll>`\ (\ ) |
  85. +----------------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  86. | :ref:`Error<enum_@GlobalScope_Error>` | :ref:`send<class_WebSocketPeer_method_send>`\ (\ message\: :ref:`PackedByteArray<class_PackedByteArray>`, write_mode\: :ref:`WriteMode<enum_WebSocketPeer_WriteMode>` = 1\ ) |
  87. +----------------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  88. | :ref:`Error<enum_@GlobalScope_Error>` | :ref:`send_text<class_WebSocketPeer_method_send_text>`\ (\ message\: :ref:`String<class_String>`\ ) |
  89. +----------------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  90. | |void| | :ref:`set_no_delay<class_WebSocketPeer_method_set_no_delay>`\ (\ enabled\: :ref:`bool<class_bool>`\ ) |
  91. +----------------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  92. | :ref:`bool<class_bool>` | :ref:`was_string_packet<class_WebSocketPeer_method_was_string_packet>`\ (\ ) |const| |
  93. +----------------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  94. .. rst-class:: classref-section-separator
  95. ----
  96. .. rst-class:: classref-descriptions-group
  97. Enumerations
  98. ------------
  99. .. _enum_WebSocketPeer_WriteMode:
  100. .. rst-class:: classref-enumeration
  101. enum **WriteMode**: :ref:`🔗<enum_WebSocketPeer_WriteMode>`
  102. .. _class_WebSocketPeer_constant_WRITE_MODE_TEXT:
  103. .. rst-class:: classref-enumeration-constant
  104. :ref:`WriteMode<enum_WebSocketPeer_WriteMode>` **WRITE_MODE_TEXT** = ``0``
  105. Specifies that WebSockets messages should be transferred as text payload (only valid UTF-8 is allowed).
  106. .. _class_WebSocketPeer_constant_WRITE_MODE_BINARY:
  107. .. rst-class:: classref-enumeration-constant
  108. :ref:`WriteMode<enum_WebSocketPeer_WriteMode>` **WRITE_MODE_BINARY** = ``1``
  109. Specifies that WebSockets messages should be transferred as binary payload (any byte combination is allowed).
  110. .. rst-class:: classref-item-separator
  111. ----
  112. .. _enum_WebSocketPeer_State:
  113. .. rst-class:: classref-enumeration
  114. enum **State**: :ref:`🔗<enum_WebSocketPeer_State>`
  115. .. _class_WebSocketPeer_constant_STATE_CONNECTING:
  116. .. rst-class:: classref-enumeration-constant
  117. :ref:`State<enum_WebSocketPeer_State>` **STATE_CONNECTING** = ``0``
  118. Socket has been created. The connection is not yet open.
  119. .. _class_WebSocketPeer_constant_STATE_OPEN:
  120. .. rst-class:: classref-enumeration-constant
  121. :ref:`State<enum_WebSocketPeer_State>` **STATE_OPEN** = ``1``
  122. The connection is open and ready to communicate.
  123. .. _class_WebSocketPeer_constant_STATE_CLOSING:
  124. .. rst-class:: classref-enumeration-constant
  125. :ref:`State<enum_WebSocketPeer_State>` **STATE_CLOSING** = ``2``
  126. 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.
  127. .. _class_WebSocketPeer_constant_STATE_CLOSED:
  128. .. rst-class:: classref-enumeration-constant
  129. :ref:`State<enum_WebSocketPeer_State>` **STATE_CLOSED** = ``3``
  130. The connection is closed or couldn't be opened.
  131. .. rst-class:: classref-section-separator
  132. ----
  133. .. rst-class:: classref-descriptions-group
  134. Property Descriptions
  135. ---------------------
  136. .. _class_WebSocketPeer_property_handshake_headers:
  137. .. rst-class:: classref-property
  138. :ref:`PackedStringArray<class_PackedStringArray>` **handshake_headers** = ``PackedStringArray()`` :ref:`🔗<class_WebSocketPeer_property_handshake_headers>`
  139. .. rst-class:: classref-property-setget
  140. - |void| **set_handshake_headers**\ (\ value\: :ref:`PackedStringArray<class_PackedStringArray>`\ )
  141. - :ref:`PackedStringArray<class_PackedStringArray>` **get_handshake_headers**\ (\ )
  142. The extra HTTP headers to be sent during the WebSocket handshake.
  143. \ **Note:** Not supported in Web exports due to browsers' restrictions.
  144. **Note:** The returned array is *copied* and any changes to it will not update the original property value. See :ref:`PackedStringArray<class_PackedStringArray>` for more details.
  145. .. rst-class:: classref-item-separator
  146. ----
  147. .. _class_WebSocketPeer_property_heartbeat_interval:
  148. .. rst-class:: classref-property
  149. :ref:`float<class_float>` **heartbeat_interval** = ``0.0`` :ref:`🔗<class_WebSocketPeer_property_heartbeat_interval>`
  150. .. rst-class:: classref-property-setget
  151. - |void| **set_heartbeat_interval**\ (\ value\: :ref:`float<class_float>`\ )
  152. - :ref:`float<class_float>` **get_heartbeat_interval**\ (\ )
  153. 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.
  154. \ **Note:** Has no effect in Web exports due to browser restrictions.
  155. .. rst-class:: classref-item-separator
  156. ----
  157. .. _class_WebSocketPeer_property_inbound_buffer_size:
  158. .. rst-class:: classref-property
  159. :ref:`int<class_int>` **inbound_buffer_size** = ``65535`` :ref:`🔗<class_WebSocketPeer_property_inbound_buffer_size>`
  160. .. rst-class:: classref-property-setget
  161. - |void| **set_inbound_buffer_size**\ (\ value\: :ref:`int<class_int>`\ )
  162. - :ref:`int<class_int>` **get_inbound_buffer_size**\ (\ )
  163. The size of the input buffer in bytes (roughly the maximum amount of memory that will be allocated for the inbound packets).
  164. .. rst-class:: classref-item-separator
  165. ----
  166. .. _class_WebSocketPeer_property_max_queued_packets:
  167. .. rst-class:: classref-property
  168. :ref:`int<class_int>` **max_queued_packets** = ``4096`` :ref:`🔗<class_WebSocketPeer_property_max_queued_packets>`
  169. .. rst-class:: classref-property-setget
  170. - |void| **set_max_queued_packets**\ (\ value\: :ref:`int<class_int>`\ )
  171. - :ref:`int<class_int>` **get_max_queued_packets**\ (\ )
  172. The maximum amount of packets that will be allowed in the queues (both inbound and outbound).
  173. .. rst-class:: classref-item-separator
  174. ----
  175. .. _class_WebSocketPeer_property_outbound_buffer_size:
  176. .. rst-class:: classref-property
  177. :ref:`int<class_int>` **outbound_buffer_size** = ``65535`` :ref:`🔗<class_WebSocketPeer_property_outbound_buffer_size>`
  178. .. rst-class:: classref-property-setget
  179. - |void| **set_outbound_buffer_size**\ (\ value\: :ref:`int<class_int>`\ )
  180. - :ref:`int<class_int>` **get_outbound_buffer_size**\ (\ )
  181. The size of the input buffer in bytes (roughly the maximum amount of memory that will be allocated for the outbound packets).
  182. .. rst-class:: classref-item-separator
  183. ----
  184. .. _class_WebSocketPeer_property_supported_protocols:
  185. .. rst-class:: classref-property
  186. :ref:`PackedStringArray<class_PackedStringArray>` **supported_protocols** = ``PackedStringArray()`` :ref:`🔗<class_WebSocketPeer_property_supported_protocols>`
  187. .. rst-class:: classref-property-setget
  188. - |void| **set_supported_protocols**\ (\ value\: :ref:`PackedStringArray<class_PackedStringArray>`\ )
  189. - :ref:`PackedStringArray<class_PackedStringArray>` **get_supported_protocols**\ (\ )
  190. The WebSocket sub-protocols allowed during the WebSocket handshake.
  191. **Note:** The returned array is *copied* and any changes to it will not update the original property value. See :ref:`PackedStringArray<class_PackedStringArray>` for more details.
  192. .. rst-class:: classref-section-separator
  193. ----
  194. .. rst-class:: classref-descriptions-group
  195. Method Descriptions
  196. -------------------
  197. .. _class_WebSocketPeer_method_accept_stream:
  198. .. rst-class:: classref-method
  199. :ref:`Error<enum_@GlobalScope_Error>` **accept_stream**\ (\ stream\: :ref:`StreamPeer<class_StreamPeer>`\ ) :ref:`🔗<class_WebSocketPeer_method_accept_stream>`
  200. 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>`.
  201. \ **Note:** Not supported in Web exports due to browsers' restrictions.
  202. .. rst-class:: classref-item-separator
  203. ----
  204. .. _class_WebSocketPeer_method_close:
  205. .. rst-class:: classref-method
  206. |void| **close**\ (\ code\: :ref:`int<class_int>` = 1000, reason\: :ref:`String<class_String>` = ""\ ) :ref:`🔗<class_WebSocketPeer_method_close>`
  207. 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.
  208. \ **Note:** To achieve a clean close, you will need to keep polling until :ref:`STATE_CLOSED<class_WebSocketPeer_constant_STATE_CLOSED>` is reached.
  209. \ **Note:** The Web export might not support all status codes. Please refer to browser-specific documentation for more details.
  210. .. rst-class:: classref-item-separator
  211. ----
  212. .. _class_WebSocketPeer_method_connect_to_url:
  213. .. rst-class:: classref-method
  214. :ref:`Error<enum_@GlobalScope_Error>` **connect_to_url**\ (\ url\: :ref:`String<class_String>`, tls_client_options\: :ref:`TLSOptions<class_TLSOptions>` = null\ ) :ref:`🔗<class_WebSocketPeer_method_connect_to_url>`
  215. 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>`.
  216. \ **Note:** This method is non-blocking, and will return :ref:`@GlobalScope.OK<class_@GlobalScope_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 :ref:`poll()<class_WebSocketPeer_method_poll>` (e.g. during :ref:`Node<class_Node>` process) and check the result of :ref:`get_ready_state()<class_WebSocketPeer_method_get_ready_state>` to know whether the connection succeeds or fails.
  217. \ **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.
  218. .. rst-class:: classref-item-separator
  219. ----
  220. .. _class_WebSocketPeer_method_get_close_code:
  221. .. rst-class:: classref-method
  222. :ref:`int<class_int>` **get_close_code**\ (\ ) |const| :ref:`🔗<class_WebSocketPeer_method_get_close_code>`
  223. 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>`.
  224. .. rst-class:: classref-item-separator
  225. ----
  226. .. _class_WebSocketPeer_method_get_close_reason:
  227. .. rst-class:: classref-method
  228. :ref:`String<class_String>` **get_close_reason**\ (\ ) |const| :ref:`🔗<class_WebSocketPeer_method_get_close_reason>`
  229. 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>`.
  230. .. rst-class:: classref-item-separator
  231. ----
  232. .. _class_WebSocketPeer_method_get_connected_host:
  233. .. rst-class:: classref-method
  234. :ref:`String<class_String>` **get_connected_host**\ (\ ) |const| :ref:`🔗<class_WebSocketPeer_method_get_connected_host>`
  235. Returns the IP address of the connected peer.
  236. \ **Note:** Not available in the Web export.
  237. .. rst-class:: classref-item-separator
  238. ----
  239. .. _class_WebSocketPeer_method_get_connected_port:
  240. .. rst-class:: classref-method
  241. :ref:`int<class_int>` **get_connected_port**\ (\ ) |const| :ref:`🔗<class_WebSocketPeer_method_get_connected_port>`
  242. Returns the remote port of the connected peer.
  243. \ **Note:** Not available in the Web export.
  244. .. rst-class:: classref-item-separator
  245. ----
  246. .. _class_WebSocketPeer_method_get_current_outbound_buffered_amount:
  247. .. rst-class:: classref-method
  248. :ref:`int<class_int>` **get_current_outbound_buffered_amount**\ (\ ) |const| :ref:`🔗<class_WebSocketPeer_method_get_current_outbound_buffered_amount>`
  249. Returns the current amount of data in the outbound websocket buffer. **Note:** Web exports use WebSocket.bufferedAmount, while other platforms use an internal buffer.
  250. .. rst-class:: classref-item-separator
  251. ----
  252. .. _class_WebSocketPeer_method_get_ready_state:
  253. .. rst-class:: classref-method
  254. :ref:`State<enum_WebSocketPeer_State>` **get_ready_state**\ (\ ) |const| :ref:`🔗<class_WebSocketPeer_method_get_ready_state>`
  255. Returns the ready state of the connection.
  256. .. rst-class:: classref-item-separator
  257. ----
  258. .. _class_WebSocketPeer_method_get_requested_url:
  259. .. rst-class:: classref-method
  260. :ref:`String<class_String>` **get_requested_url**\ (\ ) |const| :ref:`🔗<class_WebSocketPeer_method_get_requested_url>`
  261. 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>`).
  262. .. rst-class:: classref-item-separator
  263. ----
  264. .. _class_WebSocketPeer_method_get_selected_protocol:
  265. .. rst-class:: classref-method
  266. :ref:`String<class_String>` **get_selected_protocol**\ (\ ) |const| :ref:`🔗<class_WebSocketPeer_method_get_selected_protocol>`
  267. Returns the selected WebSocket sub-protocol for this connection or an empty string if the sub-protocol has not been selected yet.
  268. .. rst-class:: classref-item-separator
  269. ----
  270. .. _class_WebSocketPeer_method_poll:
  271. .. rst-class:: classref-method
  272. |void| **poll**\ (\ ) :ref:`🔗<class_WebSocketPeer_method_poll>`
  273. Updates the connection state and receive incoming packets. Call this function regularly to keep it in a clean state.
  274. .. rst-class:: classref-item-separator
  275. ----
  276. .. _class_WebSocketPeer_method_send:
  277. .. rst-class:: classref-method
  278. :ref:`Error<enum_@GlobalScope_Error>` **send**\ (\ message\: :ref:`PackedByteArray<class_PackedByteArray>`, write_mode\: :ref:`WriteMode<enum_WebSocketPeer_WriteMode>` = 1\ ) :ref:`🔗<class_WebSocketPeer_method_send>`
  279. 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>`.
  280. .. rst-class:: classref-item-separator
  281. ----
  282. .. _class_WebSocketPeer_method_send_text:
  283. .. rst-class:: classref-method
  284. :ref:`Error<enum_@GlobalScope_Error>` **send_text**\ (\ message\: :ref:`String<class_String>`\ ) :ref:`🔗<class_WebSocketPeer_method_send_text>`
  285. 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).
  286. .. rst-class:: classref-item-separator
  287. ----
  288. .. _class_WebSocketPeer_method_set_no_delay:
  289. .. rst-class:: classref-method
  290. |void| **set_no_delay**\ (\ enabled\: :ref:`bool<class_bool>`\ ) :ref:`🔗<class_WebSocketPeer_method_set_no_delay>`
  291. Disable Nagle's algorithm on the underlying TCP socket (default). See :ref:`StreamPeerTCP.set_no_delay()<class_StreamPeerTCP_method_set_no_delay>` for more information.
  292. \ **Note:** Not available in the Web export.
  293. .. rst-class:: classref-item-separator
  294. ----
  295. .. _class_WebSocketPeer_method_was_string_packet:
  296. .. rst-class:: classref-method
  297. :ref:`bool<class_bool>` **was_string_packet**\ (\ ) |const| :ref:`🔗<class_WebSocketPeer_method_was_string_packet>`
  298. Returns ``true`` if the last received packet was sent as a text payload. See :ref:`WriteMode<enum_WebSocketPeer_WriteMode>`.
  299. .. |virtual| replace:: :abbr:`virtual (This method should typically be overridden by the user to have any effect.)`
  300. .. |required| replace:: :abbr:`required (This method is required to be overridden when extending its base class.)`
  301. .. |const| replace:: :abbr:`const (This method has no side effects. It doesn't modify any of the instance's member variables.)`
  302. .. |vararg| replace:: :abbr:`vararg (This method accepts any number of arguments after the ones described here.)`
  303. .. |constructor| replace:: :abbr:`constructor (This method is used to construct a type.)`
  304. .. |static| replace:: :abbr:`static (This method doesn't need an instance to be called, so it can be called directly using the class name.)`
  305. .. |operator| replace:: :abbr:`operator (This method describes a valid operator to use with this type as left-hand operand.)`
  306. .. |bitfield| replace:: :abbr:`BitField (This value is an integer composed as a bitmask of the following flags.)`
  307. .. |void| replace:: :abbr:`void (No return value.)`