class_websocketserver.rst 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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 WebSocketServer.xml source instead.
  4. .. The source is found in doc/classes or modules/<name>/doc_classes.
  5. .. _class_WebSocketServer:
  6. WebSocketServer
  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 server implementation.
  10. Description
  11. -----------
  12. This class implements a WebSocket server that can also support the high-level multiplayer API.
  13. After starting the server (:ref:`listen<class_WebSocketServer_method_listen>`), 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>`). When clients connect, disconnect, or send data, you will receive the appropriate signal.
  14. \ **Note:** Not available in HTML5 exports.
  15. \ **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.
  16. Properties
  17. ----------
  18. +-----------------------------------------------+----------------------------------------------------------------------------+---------+
  19. | :ref:`String<class_String>` | :ref:`bind_ip<class_WebSocketServer_property_bind_ip>` | ``"*"`` |
  20. +-----------------------------------------------+----------------------------------------------------------------------------+---------+
  21. | :ref:`X509Certificate<class_X509Certificate>` | :ref:`ca_chain<class_WebSocketServer_property_ca_chain>` | |
  22. +-----------------------------------------------+----------------------------------------------------------------------------+---------+
  23. | :ref:`float<class_float>` | :ref:`handshake_timeout<class_WebSocketServer_property_handshake_timeout>` | ``3.0`` |
  24. +-----------------------------------------------+----------------------------------------------------------------------------+---------+
  25. | :ref:`CryptoKey<class_CryptoKey>` | :ref:`private_key<class_WebSocketServer_property_private_key>` | |
  26. +-----------------------------------------------+----------------------------------------------------------------------------+---------+
  27. | :ref:`X509Certificate<class_X509Certificate>` | :ref:`ssl_certificate<class_WebSocketServer_property_ssl_certificate>` | |
  28. +-----------------------------------------------+----------------------------------------------------------------------------+---------+
  29. Methods
  30. -------
  31. +---------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  32. | void | :ref:`disconnect_peer<class_WebSocketServer_method_disconnect_peer>` **(** :ref:`int<class_int>` id, :ref:`int<class_int>` code=1000, :ref:`String<class_String>` reason="" **)** |
  33. +---------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  34. | :ref:`String<class_String>` | :ref:`get_peer_address<class_WebSocketServer_method_get_peer_address>` **(** :ref:`int<class_int>` id **)** |const| |
  35. +---------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  36. | :ref:`int<class_int>` | :ref:`get_peer_port<class_WebSocketServer_method_get_peer_port>` **(** :ref:`int<class_int>` id **)** |const| |
  37. +---------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  38. | :ref:`bool<class_bool>` | :ref:`has_peer<class_WebSocketServer_method_has_peer>` **(** :ref:`int<class_int>` id **)** |const| |
  39. +---------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  40. | :ref:`bool<class_bool>` | :ref:`is_listening<class_WebSocketServer_method_is_listening>` **(** **)** |const| |
  41. +---------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  42. | :ref:`Error<enum_@GlobalScope_Error>` | :ref:`listen<class_WebSocketServer_method_listen>` **(** :ref:`int<class_int>` port, :ref:`PackedStringArray<class_PackedStringArray>` protocols=PackedStringArray(), :ref:`bool<class_bool>` gd_mp_api=false **)** |
  43. +---------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  44. | void | :ref:`stop<class_WebSocketServer_method_stop>` **(** **)** |
  45. +---------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  46. Signals
  47. -------
  48. .. _class_WebSocketServer_signal_client_close_request:
  49. - **client_close_request** **(** :ref:`int<class_int>` id, :ref:`int<class_int>` code, :ref:`String<class_String>` reason **)**
  50. Emitted when a client requests a clean close. You should keep polling until you get a :ref:`client_disconnected<class_WebSocketServer_signal_client_disconnected>` signal with the same ``id`` to achieve the clean close. See :ref:`WebSocketPeer.close<class_WebSocketPeer_method_close>` for more details.
  51. ----
  52. .. _class_WebSocketServer_signal_client_connected:
  53. - **client_connected** **(** :ref:`int<class_int>` id, :ref:`String<class_String>` protocol, :ref:`String<class_String>` resource_name **)**
  54. Emitted when a new client connects. "protocol" will be the sub-protocol agreed with the client, and "resource_name" will be the resource name of the URI the peer used.
  55. "resource_name" is a path (at the very least a single forward slash) and potentially a query string.
  56. ----
  57. .. _class_WebSocketServer_signal_client_disconnected:
  58. - **client_disconnected** **(** :ref:`int<class_int>` id, :ref:`bool<class_bool>` was_clean_close **)**
  59. Emitted when a client disconnects. ``was_clean_close`` will be ``true`` if the connection was shutdown cleanly.
  60. ----
  61. .. _class_WebSocketServer_signal_data_received:
  62. - **data_received** **(** :ref:`int<class_int>` id **)**
  63. Emitted when a new message is received.
  64. \ **Note:** This signal is *not* emitted when used as high-level multiplayer peer.
  65. Property Descriptions
  66. ---------------------
  67. .. _class_WebSocketServer_property_bind_ip:
  68. - :ref:`String<class_String>` **bind_ip**
  69. +-----------+--------------------+
  70. | *Default* | ``"*"`` |
  71. +-----------+--------------------+
  72. | *Setter* | set_bind_ip(value) |
  73. +-----------+--------------------+
  74. | *Getter* | get_bind_ip() |
  75. +-----------+--------------------+
  76. When not set to ``*`` will restrict incoming connections to the specified IP address. Setting ``bind_ip`` to ``127.0.0.1`` will cause the server to listen only to the local host.
  77. ----
  78. .. _class_WebSocketServer_property_ca_chain:
  79. - :ref:`X509Certificate<class_X509Certificate>` **ca_chain**
  80. +----------+---------------------+
  81. | *Setter* | set_ca_chain(value) |
  82. +----------+---------------------+
  83. | *Getter* | get_ca_chain() |
  84. +----------+---------------------+
  85. When using SSL (see :ref:`private_key<class_WebSocketServer_property_private_key>` and :ref:`ssl_certificate<class_WebSocketServer_property_ssl_certificate>`), you can set this to a valid :ref:`X509Certificate<class_X509Certificate>` to be provided as additional CA chain information during the SSL handshake.
  86. ----
  87. .. _class_WebSocketServer_property_handshake_timeout:
  88. - :ref:`float<class_float>` **handshake_timeout**
  89. +-----------+------------------------------+
  90. | *Default* | ``3.0`` |
  91. +-----------+------------------------------+
  92. | *Setter* | set_handshake_timeout(value) |
  93. +-----------+------------------------------+
  94. | *Getter* | get_handshake_timeout() |
  95. +-----------+------------------------------+
  96. The time in seconds before a pending client (i.e. a client that has not yet finished the HTTP handshake) is considered stale and forcefully disconnected.
  97. ----
  98. .. _class_WebSocketServer_property_private_key:
  99. - :ref:`CryptoKey<class_CryptoKey>` **private_key**
  100. +----------+------------------------+
  101. | *Setter* | set_private_key(value) |
  102. +----------+------------------------+
  103. | *Getter* | get_private_key() |
  104. +----------+------------------------+
  105. When set to a valid :ref:`CryptoKey<class_CryptoKey>` (along with :ref:`ssl_certificate<class_WebSocketServer_property_ssl_certificate>`) will cause the server to require SSL instead of regular TCP (i.e. the ``wss://`` protocol).
  106. ----
  107. .. _class_WebSocketServer_property_ssl_certificate:
  108. - :ref:`X509Certificate<class_X509Certificate>` **ssl_certificate**
  109. +----------+----------------------------+
  110. | *Setter* | set_ssl_certificate(value) |
  111. +----------+----------------------------+
  112. | *Getter* | get_ssl_certificate() |
  113. +----------+----------------------------+
  114. When set to a valid :ref:`X509Certificate<class_X509Certificate>` (along with :ref:`private_key<class_WebSocketServer_property_private_key>`) will cause the server to require SSL instead of regular TCP (i.e. the ``wss://`` protocol).
  115. Method Descriptions
  116. -------------------
  117. .. _class_WebSocketServer_method_disconnect_peer:
  118. - void **disconnect_peer** **(** :ref:`int<class_int>` id, :ref:`int<class_int>` code=1000, :ref:`String<class_String>` reason="" **)**
  119. Disconnects the peer identified by ``id`` from the server. See :ref:`WebSocketPeer.close<class_WebSocketPeer_method_close>` for more information.
  120. ----
  121. .. _class_WebSocketServer_method_get_peer_address:
  122. - :ref:`String<class_String>` **get_peer_address** **(** :ref:`int<class_int>` id **)** |const|
  123. Returns the IP address of the given peer.
  124. ----
  125. .. _class_WebSocketServer_method_get_peer_port:
  126. - :ref:`int<class_int>` **get_peer_port** **(** :ref:`int<class_int>` id **)** |const|
  127. Returns the remote port of the given peer.
  128. ----
  129. .. _class_WebSocketServer_method_has_peer:
  130. - :ref:`bool<class_bool>` **has_peer** **(** :ref:`int<class_int>` id **)** |const|
  131. Returns ``true`` if a peer with the given ID is connected.
  132. ----
  133. .. _class_WebSocketServer_method_is_listening:
  134. - :ref:`bool<class_bool>` **is_listening** **(** **)** |const|
  135. Returns ``true`` if the server is actively listening on a port.
  136. ----
  137. .. _class_WebSocketServer_method_listen:
  138. - :ref:`Error<enum_@GlobalScope_Error>` **listen** **(** :ref:`int<class_int>` port, :ref:`PackedStringArray<class_PackedStringArray>` protocols=PackedStringArray(), :ref:`bool<class_bool>` gd_mp_api=false **)**
  139. Starts listening on the given port.
  140. You can specify the desired subprotocols via the "protocols" array. If the list empty (default), no sub-protocol will be requested.
  141. If ``true`` is passed as ``gd_mp_api``, the server will behave like a multiplayer peer for the :ref:`MultiplayerAPI<class_MultiplayerAPI>`, connections from non-Godot clients will not work, and :ref:`data_received<class_WebSocketServer_signal_data_received>` will not be emitted.
  142. 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(id)`` to communicate with the peer with given ``id`` (e.g. ``get_peer(id).get_available_packet_count``).
  143. ----
  144. .. _class_WebSocketServer_method_stop:
  145. - void **stop** **(** **)**
  146. Stops the server and clear its state.
  147. .. |virtual| replace:: :abbr:`virtual (This method should typically be overridden by the user to have any effect.)`
  148. .. |const| replace:: :abbr:`const (This method has no side effects. It doesn't modify any of the instance's member variables.)`
  149. .. |vararg| replace:: :abbr:`vararg (This method accepts any number of arguments after the ones described here.)`
  150. .. |constructor| replace:: :abbr:`constructor (This method is used to construct a type.)`
  151. .. |static| replace:: :abbr:`static (This method doesn't need an instance to be called, so it can be called directly using the class name.)`
  152. .. |operator| replace:: :abbr:`operator (This method describes a valid operator to use with this type as left-hand operand.)`