class_websocketserver.rst 8.2 KB

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