class_websocketclient.rst 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. :github_url: hide
  2. .. Generated automatically by doc/tools/make_rst.py in Godot's source tree.
  3. .. DO NOT EDIT THIS FILE, but the WebSocketClient.xml source instead.
  4. .. The source is found in doc/classes or modules/<name>/doc_classes.
  5. .. _class_WebSocketClient:
  6. WebSocketClient
  7. ===============
  8. **Inherits:** :ref:`WebSocketMultiplayerPeer<class_WebSocketMultiplayerPeer>` **<** :ref:`MultiplayerPeer<class_MultiplayerPeer>` **<** :ref:`PacketPeer<class_PacketPeer>` **<** :ref:`RefCounted<class_RefCounted>` **<** :ref:`Object<class_Object>`
  9. A WebSocket client implementation.
  10. Description
  11. -----------
  12. This class implements a WebSocket client compatible with any RFC 6455-compliant WebSocket server.
  13. This client can be optionally used as a multiplayer peer for the :ref:`MultiplayerAPI<class_MultiplayerAPI>`.
  14. After starting the client (:ref:`connect_to_url<class_WebSocketClient_method_connect_to_url>`), you will need to :ref:`MultiplayerPeer.poll<class_MultiplayerPeer_method_poll>` it at regular intervals (e.g. inside :ref:`Node._process<class_Node_method__process>`).
  15. You will receive appropriate signals when connecting, disconnecting, or when new data is available.
  16. \ **Note:** When exporting to Android, make sure to enable the ``INTERNET`` permission in the Android export preset before exporting the project or using one-click deploy. Otherwise, network communication of any kind will be blocked by Android.
  17. Properties
  18. ----------
  19. +-----------------------------------------------+----------------------------------------------------------------------------------------+
  20. | :ref:`X509Certificate<class_X509Certificate>` | :ref:`trusted_ssl_certificate<class_WebSocketClient_property_trusted_ssl_certificate>` |
  21. +-----------------------------------------------+----------------------------------------------------------------------------------------+
  22. | :ref:`bool<class_bool>` | :ref:`verify_ssl<class_WebSocketClient_property_verify_ssl>` |
  23. +-----------------------------------------------+----------------------------------------------------------------------------------------+
  24. Methods
  25. -------
  26. +---------------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  27. | :ref:`Error<enum_@GlobalScope_Error>` | :ref:`connect_to_url<class_WebSocketClient_method_connect_to_url>` **(** :ref:`String<class_String>` url, :ref:`PackedStringArray<class_PackedStringArray>` protocols=PackedStringArray(), :ref:`bool<class_bool>` gd_mp_api=false, :ref:`PackedStringArray<class_PackedStringArray>` custom_headers=PackedStringArray() **)** |
  28. +---------------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  29. | void | :ref:`disconnect_from_host<class_WebSocketClient_method_disconnect_from_host>` **(** :ref:`int<class_int>` code=1000, :ref:`String<class_String>` reason="" **)** |
  30. +---------------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  31. | :ref:`String<class_String>` | :ref:`get_connected_host<class_WebSocketClient_method_get_connected_host>` **(** **)** |const| |
  32. +---------------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  33. | :ref:`int<class_int>` | :ref:`get_connected_port<class_WebSocketClient_method_get_connected_port>` **(** **)** |const| |
  34. +---------------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  35. Signals
  36. -------
  37. .. _class_WebSocketClient_signal_connection_closed:
  38. - **connection_closed** **(** :ref:`bool<class_bool>` was_clean_close **)**
  39. Emitted when the connection to the server is closed. ``was_clean_close`` will be ``true`` if the connection was shutdown cleanly.
  40. ----
  41. .. _class_WebSocketClient_signal_connection_error:
  42. - **connection_error** **(** **)**
  43. Emitted when the connection to the server fails.
  44. ----
  45. .. _class_WebSocketClient_signal_connection_established:
  46. - **connection_established** **(** :ref:`String<class_String>` protocol **)**
  47. Emitted when a connection with the server is established, ``protocol`` will contain the sub-protocol agreed with the server.
  48. ----
  49. .. _class_WebSocketClient_signal_data_received:
  50. - **data_received** **(** **)**
  51. Emitted when a WebSocket message is received.
  52. \ **Note:** This signal is *not* emitted when used as high-level multiplayer peer.
  53. ----
  54. .. _class_WebSocketClient_signal_server_close_request:
  55. - **server_close_request** **(** :ref:`int<class_int>` code, :ref:`String<class_String>` reason **)**
  56. Emitted when the server requests a clean close. You should keep polling until you get a :ref:`connection_closed<class_WebSocketClient_signal_connection_closed>` signal to achieve the clean close. See :ref:`WebSocketPeer.close<class_WebSocketPeer_method_close>` for more details.
  57. Property Descriptions
  58. ---------------------
  59. .. _class_WebSocketClient_property_trusted_ssl_certificate:
  60. - :ref:`X509Certificate<class_X509Certificate>` **trusted_ssl_certificate**
  61. +----------+------------------------------------+
  62. | *Setter* | set_trusted_ssl_certificate(value) |
  63. +----------+------------------------------------+
  64. | *Getter* | get_trusted_ssl_certificate() |
  65. +----------+------------------------------------+
  66. If specified, this :ref:`X509Certificate<class_X509Certificate>` will be the only one accepted when connecting to an SSL host. Any other certificate provided by the server will be regarded as invalid.
  67. \ **Note:** Specifying a custom ``trusted_ssl_certificate`` is not supported in HTML5 exports due to browsers restrictions.
  68. ----
  69. .. _class_WebSocketClient_property_verify_ssl:
  70. - :ref:`bool<class_bool>` **verify_ssl**
  71. +----------+-------------------------------+
  72. | *Setter* | set_verify_ssl_enabled(value) |
  73. +----------+-------------------------------+
  74. | *Getter* | is_verify_ssl_enabled() |
  75. +----------+-------------------------------+
  76. If ``true``, SSL certificate verification is enabled.
  77. \ **Note:** You must specify the certificates to be used in the Project Settings for it to work when exported.
  78. Method Descriptions
  79. -------------------
  80. .. _class_WebSocketClient_method_connect_to_url:
  81. - :ref:`Error<enum_@GlobalScope_Error>` **connect_to_url** **(** :ref:`String<class_String>` url, :ref:`PackedStringArray<class_PackedStringArray>` protocols=PackedStringArray(), :ref:`bool<class_bool>` gd_mp_api=false, :ref:`PackedStringArray<class_PackedStringArray>` custom_headers=PackedStringArray() **)**
  82. Connects to the given URL requesting one of the given ``protocols`` as sub-protocol. If the list empty (default), no sub-protocol will be requested.
  83. If ``true`` is passed as ``gd_mp_api``, the client will behave like a multiplayer peer for the :ref:`MultiplayerAPI<class_MultiplayerAPI>`, connections to non-Godot servers will not work, and :ref:`data_received<class_WebSocketClient_signal_data_received>` will not be emitted.
  84. If ``false`` is passed instead (default), you must call :ref:`PacketPeer<class_PacketPeer>` functions (``put_packet``, ``get_packet``, etc.) on the :ref:`WebSocketPeer<class_WebSocketPeer>` returned via ``get_peer(1)`` and not on this object directly (e.g. ``get_peer(1).put_packet(data)``).
  85. You can optionally pass a list of ``custom_headers`` to be added to the handshake HTTP request.
  86. \ **Note:** To avoid mixed content warnings or errors in HTML5, 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 SSL certificate. Do not connect directly via the IP address for ``wss://`` connections, as it won't match with the SSL certificate.
  87. \ **Note:** Specifying ``custom_headers`` is not supported in HTML5 exports due to browsers restrictions.
  88. ----
  89. .. _class_WebSocketClient_method_disconnect_from_host:
  90. - void **disconnect_from_host** **(** :ref:`int<class_int>` code=1000, :ref:`String<class_String>` reason="" **)**
  91. Disconnects this client from the connected host. See :ref:`WebSocketPeer.close<class_WebSocketPeer_method_close>` for more information.
  92. ----
  93. .. _class_WebSocketClient_method_get_connected_host:
  94. - :ref:`String<class_String>` **get_connected_host** **(** **)** |const|
  95. Returns the IP address of the currently connected host.
  96. ----
  97. .. _class_WebSocketClient_method_get_connected_port:
  98. - :ref:`int<class_int>` **get_connected_port** **(** **)** |const|
  99. Returns the IP port of the currently connected host.
  100. .. |virtual| replace:: :abbr:`virtual (This method should typically be overridden by the user to have any effect.)`
  101. .. |const| replace:: :abbr:`const (This method has no side effects. It doesn't modify any of the instance's member variables.)`
  102. .. |vararg| replace:: :abbr:`vararg (This method accepts any number of arguments after the ones described here.)`
  103. .. |constructor| replace:: :abbr:`constructor (This method is used to construct a type.)`
  104. .. |static| replace:: :abbr:`static (This method doesn't need an instance to be called, so it can be called directly using the class name.)`
  105. .. |operator| replace:: :abbr:`operator (This method describes a valid operator to use with this type as left-hand operand.)`