class_websocketserver.rst 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. .. _class_WebSocketServer_signal_client_connected:
  36. - **client_connected** **(** :ref:`int<class_int>` id, :ref:`String<class_String>` protocol **)**
  37. Emitted when a new client connects. "protocol" will be the sub-protocol agreed with the client.
  38. .. _class_WebSocketServer_signal_client_disconnected:
  39. - **client_disconnected** **(** :ref:`int<class_int>` id, :ref:`bool<class_bool>` was_clean_close **)**
  40. Emitted when a client disconnects. ``was_clean_close`` will be ``true`` if the connection was shutdown cleanly.
  41. .. _class_WebSocketServer_signal_data_received:
  42. - **data_received** **(** :ref:`int<class_int>` id **)**
  43. Emitted when a new message is received.
  44. **Note:** This signal is *not* emitted when used as high-level multiplayer peer.
  45. Description
  46. -----------
  47. This class implements a WebSocket server that can also support the high-level multiplayer API.
  48. 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.
  49. **Note:** This class will not work in HTML5 exports due to browser restrictions.
  50. Method Descriptions
  51. -------------------
  52. .. _class_WebSocketServer_method_disconnect_peer:
  53. - void **disconnect_peer** **(** :ref:`int<class_int>` id, :ref:`int<class_int>` code=1000, :ref:`String<class_String>` reason="" **)**
  54. Disconnects the peer identified by ``id`` from the server. See :ref:`WebSocketPeer.close<class_WebSocketPeer_method_close>` for more information.
  55. .. _class_WebSocketServer_method_get_peer_address:
  56. - :ref:`String<class_String>` **get_peer_address** **(** :ref:`int<class_int>` id **)** const
  57. Returns the IP address of the given peer.
  58. .. _class_WebSocketServer_method_get_peer_port:
  59. - :ref:`int<class_int>` **get_peer_port** **(** :ref:`int<class_int>` id **)** const
  60. Returns the remote port of the given peer.
  61. .. _class_WebSocketServer_method_has_peer:
  62. - :ref:`bool<class_bool>` **has_peer** **(** :ref:`int<class_int>` id **)** const
  63. Returns ``true`` if a peer with the given ID is connected.
  64. .. _class_WebSocketServer_method_is_listening:
  65. - :ref:`bool<class_bool>` **is_listening** **(** **)** const
  66. Returns ``true`` if the server is actively listening on a port.
  67. .. _class_WebSocketServer_method_listen:
  68. - :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 **)**
  69. Starts listening on the given port.
  70. You can specify the desired subprotocols via the "protocols" array. If the list empty (default), no sub-protocol will be requested.
  71. 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.
  72. 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``).
  73. .. _class_WebSocketServer_method_stop:
  74. - void **stop** **(** **)**
  75. Stops the server and clear its state.