class_websocketserver.rst 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  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/3.6/doc/tools/make_rst.py.
  5. .. XML source: https://github.com/godotengine/godot/tree/3.6/modules/websocket/doc_classes/WebSocketServer.xml.
  6. .. _class_WebSocketServer:
  7. WebSocketServer
  8. ===============
  9. **Inherits:** :ref:`WebSocketMultiplayerPeer<class_WebSocketMultiplayerPeer>` **<** :ref:`NetworkedMultiplayerPeer<class_NetworkedMultiplayerPeer>` **<** :ref:`PacketPeer<class_PacketPeer>` **<** :ref:`Reference<class_Reference>` **<** :ref:`Object<class_Object>`
  10. A WebSocket server implementation.
  11. .. rst-class:: classref-introduction-group
  12. Description
  13. -----------
  14. This class implements a WebSocket server that can also support the high-level multiplayer API.
  15. After starting the server (:ref:`listen<class_WebSocketServer_method_listen>`), you will need to :ref:`NetworkedMultiplayerPeer.poll<class_NetworkedMultiplayerPeer_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.
  16. \ **Note:** Not available in HTML5 exports.
  17. .. rst-class:: classref-reftable-group
  18. Properties
  19. ----------
  20. .. table::
  21. :widths: auto
  22. +-----------------------------------------------+----------------------------------------------------------------------------+---------+
  23. | :ref:`String<class_String>` | :ref:`bind_ip<class_WebSocketServer_property_bind_ip>` | ``"*"`` |
  24. +-----------------------------------------------+----------------------------------------------------------------------------+---------+
  25. | :ref:`X509Certificate<class_X509Certificate>` | :ref:`ca_chain<class_WebSocketServer_property_ca_chain>` | |
  26. +-----------------------------------------------+----------------------------------------------------------------------------+---------+
  27. | :ref:`float<class_float>` | :ref:`handshake_timeout<class_WebSocketServer_property_handshake_timeout>` | ``3.0`` |
  28. +-----------------------------------------------+----------------------------------------------------------------------------+---------+
  29. | :ref:`CryptoKey<class_CryptoKey>` | :ref:`private_key<class_WebSocketServer_property_private_key>` | |
  30. +-----------------------------------------------+----------------------------------------------------------------------------+---------+
  31. | :ref:`X509Certificate<class_X509Certificate>` | :ref:`ssl_certificate<class_WebSocketServer_property_ssl_certificate>` | |
  32. +-----------------------------------------------+----------------------------------------------------------------------------+---------+
  33. .. rst-class:: classref-reftable-group
  34. Methods
  35. -------
  36. .. table::
  37. :widths: auto
  38. +---------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  39. | 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="" **)** |
  40. +---------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  41. | :ref:`String<class_String>` | :ref:`get_peer_address<class_WebSocketServer_method_get_peer_address>` **(** :ref:`int<class_int>` id **)** |const| |
  42. +---------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  43. | :ref:`int<class_int>` | :ref:`get_peer_port<class_WebSocketServer_method_get_peer_port>` **(** :ref:`int<class_int>` id **)** |const| |
  44. +---------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  45. | :ref:`bool<class_bool>` | :ref:`has_peer<class_WebSocketServer_method_has_peer>` **(** :ref:`int<class_int>` id **)** |const| |
  46. +---------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  47. | :ref:`bool<class_bool>` | :ref:`is_listening<class_WebSocketServer_method_is_listening>` **(** **)** |const| |
  48. +---------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  49. | :ref:`Error<enum_@GlobalScope_Error>` | :ref:`listen<class_WebSocketServer_method_listen>` **(** :ref:`int<class_int>` port, :ref:`PoolStringArray<class_PoolStringArray>` protocols=PoolStringArray( ), :ref:`bool<class_bool>` gd_mp_api=false **)** |
  50. +---------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  51. | void | :ref:`set_extra_headers<class_WebSocketServer_method_set_extra_headers>` **(** :ref:`PoolStringArray<class_PoolStringArray>` headers=PoolStringArray( ) **)** |
  52. +---------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  53. | void | :ref:`stop<class_WebSocketServer_method_stop>` **(** **)** |
  54. +---------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  55. .. rst-class:: classref-section-separator
  56. ----
  57. .. rst-class:: classref-descriptions-group
  58. Signals
  59. -------
  60. .. _class_WebSocketServer_signal_client_close_request:
  61. .. rst-class:: classref-signal
  62. **client_close_request** **(** :ref:`int<class_int>` id, :ref:`int<class_int>` code, :ref:`String<class_String>` reason **)**
  63. 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.
  64. .. rst-class:: classref-item-separator
  65. ----
  66. .. _class_WebSocketServer_signal_client_connected:
  67. .. rst-class:: classref-signal
  68. **client_connected** **(** :ref:`int<class_int>` id, :ref:`String<class_String>` protocol **)**
  69. Emitted when a new client connects. "protocol" will be the sub-protocol agreed with the client.
  70. .. rst-class:: classref-item-separator
  71. ----
  72. .. _class_WebSocketServer_signal_client_disconnected:
  73. .. rst-class:: classref-signal
  74. **client_disconnected** **(** :ref:`int<class_int>` id, :ref:`bool<class_bool>` was_clean_close **)**
  75. Emitted when a client disconnects. ``was_clean_close`` will be ``true`` if the connection was shutdown cleanly.
  76. .. rst-class:: classref-item-separator
  77. ----
  78. .. _class_WebSocketServer_signal_data_received:
  79. .. rst-class:: classref-signal
  80. **data_received** **(** :ref:`int<class_int>` id **)**
  81. Emitted when a new message is received.
  82. \ **Note:** This signal is *not* emitted when used as high-level multiplayer peer.
  83. .. rst-class:: classref-section-separator
  84. ----
  85. .. rst-class:: classref-descriptions-group
  86. Property Descriptions
  87. ---------------------
  88. .. _class_WebSocketServer_property_bind_ip:
  89. .. rst-class:: classref-property
  90. :ref:`String<class_String>` **bind_ip** = ``"*"``
  91. .. rst-class:: classref-property-setget
  92. - void **set_bind_ip** **(** :ref:`String<class_String>` value **)**
  93. - :ref:`String<class_String>` **get_bind_ip** **(** **)**
  94. 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.
  95. .. rst-class:: classref-item-separator
  96. ----
  97. .. _class_WebSocketServer_property_ca_chain:
  98. .. rst-class:: classref-property
  99. :ref:`X509Certificate<class_X509Certificate>` **ca_chain**
  100. .. rst-class:: classref-property-setget
  101. - void **set_ca_chain** **(** :ref:`X509Certificate<class_X509Certificate>` value **)**
  102. - :ref:`X509Certificate<class_X509Certificate>` **get_ca_chain** **(** **)**
  103. 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.
  104. .. rst-class:: classref-item-separator
  105. ----
  106. .. _class_WebSocketServer_property_handshake_timeout:
  107. .. rst-class:: classref-property
  108. :ref:`float<class_float>` **handshake_timeout** = ``3.0``
  109. .. rst-class:: classref-property-setget
  110. - void **set_handshake_timeout** **(** :ref:`float<class_float>` value **)**
  111. - :ref:`float<class_float>` **get_handshake_timeout** **(** **)**
  112. 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.
  113. .. rst-class:: classref-item-separator
  114. ----
  115. .. _class_WebSocketServer_property_private_key:
  116. .. rst-class:: classref-property
  117. :ref:`CryptoKey<class_CryptoKey>` **private_key**
  118. .. rst-class:: classref-property-setget
  119. - void **set_private_key** **(** :ref:`CryptoKey<class_CryptoKey>` value **)**
  120. - :ref:`CryptoKey<class_CryptoKey>` **get_private_key** **(** **)**
  121. 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).
  122. .. rst-class:: classref-item-separator
  123. ----
  124. .. _class_WebSocketServer_property_ssl_certificate:
  125. .. rst-class:: classref-property
  126. :ref:`X509Certificate<class_X509Certificate>` **ssl_certificate**
  127. .. rst-class:: classref-property-setget
  128. - void **set_ssl_certificate** **(** :ref:`X509Certificate<class_X509Certificate>` value **)**
  129. - :ref:`X509Certificate<class_X509Certificate>` **get_ssl_certificate** **(** **)**
  130. 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).
  131. .. rst-class:: classref-section-separator
  132. ----
  133. .. rst-class:: classref-descriptions-group
  134. Method Descriptions
  135. -------------------
  136. .. _class_WebSocketServer_method_disconnect_peer:
  137. .. rst-class:: classref-method
  138. void **disconnect_peer** **(** :ref:`int<class_int>` id, :ref:`int<class_int>` code=1000, :ref:`String<class_String>` reason="" **)**
  139. Disconnects the peer identified by ``id`` from the server. See :ref:`WebSocketPeer.close<class_WebSocketPeer_method_close>` for more information.
  140. .. rst-class:: classref-item-separator
  141. ----
  142. .. _class_WebSocketServer_method_get_peer_address:
  143. .. rst-class:: classref-method
  144. :ref:`String<class_String>` **get_peer_address** **(** :ref:`int<class_int>` id **)** |const|
  145. Returns the IP address of the given peer.
  146. .. rst-class:: classref-item-separator
  147. ----
  148. .. _class_WebSocketServer_method_get_peer_port:
  149. .. rst-class:: classref-method
  150. :ref:`int<class_int>` **get_peer_port** **(** :ref:`int<class_int>` id **)** |const|
  151. Returns the remote port of the given peer.
  152. .. rst-class:: classref-item-separator
  153. ----
  154. .. _class_WebSocketServer_method_has_peer:
  155. .. rst-class:: classref-method
  156. :ref:`bool<class_bool>` **has_peer** **(** :ref:`int<class_int>` id **)** |const|
  157. Returns ``true`` if a peer with the given ID is connected.
  158. .. rst-class:: classref-item-separator
  159. ----
  160. .. _class_WebSocketServer_method_is_listening:
  161. .. rst-class:: classref-method
  162. :ref:`bool<class_bool>` **is_listening** **(** **)** |const|
  163. Returns ``true`` if the server is actively listening on a port.
  164. .. rst-class:: classref-item-separator
  165. ----
  166. .. _class_WebSocketServer_method_listen:
  167. .. rst-class:: classref-method
  168. :ref:`Error<enum_@GlobalScope_Error>` **listen** **(** :ref:`int<class_int>` port, :ref:`PoolStringArray<class_PoolStringArray>` protocols=PoolStringArray( ), :ref:`bool<class_bool>` gd_mp_api=false **)**
  169. Starts listening on the given port.
  170. You can specify the desired subprotocols via the "protocols" array. If the list empty (default), no sub-protocol will be requested.
  171. If ``true`` is passed as ``gd_mp_api``, the server will behave like a network 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.
  172. 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``).
  173. .. rst-class:: classref-item-separator
  174. ----
  175. .. _class_WebSocketServer_method_set_extra_headers:
  176. .. rst-class:: classref-method
  177. void **set_extra_headers** **(** :ref:`PoolStringArray<class_PoolStringArray>` headers=PoolStringArray( ) **)**
  178. Sets additional headers to be sent to clients during the HTTP handshake.
  179. .. rst-class:: classref-item-separator
  180. ----
  181. .. _class_WebSocketServer_method_stop:
  182. .. rst-class:: classref-method
  183. void **stop** **(** **)**
  184. Stops the server and clear its state.
  185. .. |virtual| replace:: :abbr:`virtual (This method should typically be overridden by the user to have any effect.)`
  186. .. |const| replace:: :abbr:`const (This method has no side effects. It doesn't modify any of the instance's member variables.)`
  187. .. |vararg| replace:: :abbr:`vararg (This method accepts any number of arguments after the ones described here.)`
  188. .. |static| replace:: :abbr:`static (This method doesn't need an instance to be called, so it can be called directly using the class name.)`