class_websocketserver.rst 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. :github_url: hide
  2. .. Generated automatically by doc/tools/makerst.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:`NetworkedMultiplayerPeer<class_NetworkedMultiplayerPeer>` **<** :ref:`PacketPeer<class_PacketPeer>` **<** :ref:`Reference<class_Reference>` **<** :ref:`Object<class_Object>`
  9. **Category:** Core
  10. Brief Description
  11. -----------------
  12. A WebSocket server implementation
  13. Methods
  14. -------
  15. +---------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  16. | 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="" **)** |
  17. +---------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  18. | :ref:`String<class_String>` | :ref:`get_peer_address<class_WebSocketServer_method_get_peer_address>` **(** :ref:`int<class_int>` id **)** const |
  19. +---------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  20. | :ref:`int<class_int>` | :ref:`get_peer_port<class_WebSocketServer_method_get_peer_port>` **(** :ref:`int<class_int>` id **)** const |
  21. +---------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  22. | :ref:`bool<class_bool>` | :ref:`has_peer<class_WebSocketServer_method_has_peer>` **(** :ref:`int<class_int>` id **)** const |
  23. +---------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  24. | :ref:`bool<class_bool>` | :ref:`is_listening<class_WebSocketServer_method_is_listening>` **(** **)** const |
  25. +---------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  26. | :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 **)** |
  27. +---------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  28. | void | :ref:`stop<class_WebSocketServer_method_stop>` **(** **)** |
  29. +---------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  30. Signals
  31. -------
  32. .. _class_WebSocketServer_signal_client_close_request:
  33. - **client_close_request** **(** :ref:`int<class_int>` id, :ref:`int<class_int>` code, :ref:`String<class_String>` reason **)**
  34. 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.
  35. ----
  36. .. _class_WebSocketServer_signal_client_connected:
  37. - **client_connected** **(** :ref:`int<class_int>` id, :ref:`String<class_String>` protocol **)**
  38. Emitted when a new client connects. "protocol" will be the sub-protocol agreed with the client.
  39. ----
  40. .. _class_WebSocketServer_signal_client_disconnected:
  41. - **client_disconnected** **(** :ref:`int<class_int>` id, :ref:`bool<class_bool>` was_clean_close **)**
  42. Emitted when a client disconnects. ``was_clean_close`` will be ``true`` if the connection was shutdown cleanly.
  43. ----
  44. .. _class_WebSocketServer_signal_data_received:
  45. - **data_received** **(** :ref:`int<class_int>` id **)**
  46. Emitted when a new message is received. Note: This signal is NOT emitted when used as high level multiplayer peer.
  47. Description
  48. -----------
  49. This class implements a WebSocket server that can also support the high level multiplayer API.
  50. 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.
  51. Note: This class will not work in HTML5 exports due to browser restrictions.
  52. Method Descriptions
  53. -------------------
  54. .. _class_WebSocketServer_method_disconnect_peer:
  55. - void **disconnect_peer** **(** :ref:`int<class_int>` id, :ref:`int<class_int>` code=1000, :ref:`String<class_String>` reason="" **)**
  56. Disconnects the peer identified by ``id`` from the server. See :ref:`WebSocketPeer.close<class_WebSocketPeer_method_close>` for more info.
  57. ----
  58. .. _class_WebSocketServer_method_get_peer_address:
  59. - :ref:`String<class_String>` **get_peer_address** **(** :ref:`int<class_int>` id **)** const
  60. Returns the IP address of the given peer.
  61. ----
  62. .. _class_WebSocketServer_method_get_peer_port:
  63. - :ref:`int<class_int>` **get_peer_port** **(** :ref:`int<class_int>` id **)** const
  64. Returns the remote port of the given peer.
  65. ----
  66. .. _class_WebSocketServer_method_has_peer:
  67. - :ref:`bool<class_bool>` **has_peer** **(** :ref:`int<class_int>` id **)** const
  68. Returns ``true`` if a peer with the given ID is connected.
  69. ----
  70. .. _class_WebSocketServer_method_is_listening:
  71. - :ref:`bool<class_bool>` **is_listening** **(** **)** const
  72. Returns ``true`` if the server is actively listening on a port.
  73. ----
  74. .. _class_WebSocketServer_method_listen:
  75. - :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 **)**
  76. Start listening on the given port.
  77. You can specify the desired subprotocols via the "protocols" array. If the list empty (default), "binary" will be used.
  78. 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.
  79. 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``).
  80. ----
  81. .. _class_WebSocketServer_method_stop:
  82. - void **stop** **(** **)**
  83. Stop the server and clear its state.