mirror of
https://github.com/godotengine/godot-docs.git
synced 2025-12-31 17:49:03 +03:00
145 lines
8.3 KiB
ReStructuredText
145 lines
8.3 KiB
ReStructuredText
:github_url: hide
|
|
|
|
.. Generated automatically by doc/tools/makerst.py in Godot's source tree.
|
|
.. DO NOT EDIT THIS FILE, but the WebSocketServer.xml source instead.
|
|
.. The source is found in doc/classes or modules/<name>/doc_classes.
|
|
|
|
.. _class_WebSocketServer:
|
|
|
|
WebSocketServer
|
|
===============
|
|
|
|
**Inherits:** :ref:`WebSocketMultiplayerPeer<class_WebSocketMultiplayerPeer>` **<** :ref:`NetworkedMultiplayerPeer<class_NetworkedMultiplayerPeer>` **<** :ref:`PacketPeer<class_PacketPeer>` **<** :ref:`Reference<class_Reference>` **<** :ref:`Object<class_Object>`
|
|
|
|
**Category:** Core
|
|
|
|
Brief Description
|
|
-----------------
|
|
|
|
A WebSocket server implementation
|
|
|
|
Methods
|
|
-------
|
|
|
|
+---------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
|
|
| 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="" **)** |
|
|
+---------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
|
|
| :ref:`String<class_String>` | :ref:`get_peer_address<class_WebSocketServer_method_get_peer_address>` **(** :ref:`int<class_int>` id **)** const |
|
|
+---------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
|
|
| :ref:`int<class_int>` | :ref:`get_peer_port<class_WebSocketServer_method_get_peer_port>` **(** :ref:`int<class_int>` id **)** const |
|
|
+---------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
|
|
| :ref:`bool<class_bool>` | :ref:`has_peer<class_WebSocketServer_method_has_peer>` **(** :ref:`int<class_int>` id **)** const |
|
|
+---------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
|
|
| :ref:`bool<class_bool>` | :ref:`is_listening<class_WebSocketServer_method_is_listening>` **(** **)** const |
|
|
+---------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
|
|
| :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 **)** |
|
|
+---------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
|
|
| void | :ref:`stop<class_WebSocketServer_method_stop>` **(** **)** |
|
|
+---------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
|
|
|
|
Signals
|
|
-------
|
|
|
|
.. _class_WebSocketServer_signal_client_close_request:
|
|
|
|
- **client_close_request** **(** :ref:`int<class_int>` id, :ref:`int<class_int>` code, :ref:`String<class_String>` reason **)**
|
|
|
|
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.
|
|
|
|
----
|
|
|
|
.. _class_WebSocketServer_signal_client_connected:
|
|
|
|
- **client_connected** **(** :ref:`int<class_int>` id, :ref:`String<class_String>` protocol **)**
|
|
|
|
Emitted when a new client connects. "protocol" will be the sub-protocol agreed with the client.
|
|
|
|
----
|
|
|
|
.. _class_WebSocketServer_signal_client_disconnected:
|
|
|
|
- **client_disconnected** **(** :ref:`int<class_int>` id, :ref:`bool<class_bool>` was_clean_close **)**
|
|
|
|
Emitted when a client disconnects. ``was_clean_close`` will be ``true`` if the connection was shutdown cleanly.
|
|
|
|
----
|
|
|
|
.. _class_WebSocketServer_signal_data_received:
|
|
|
|
- **data_received** **(** :ref:`int<class_int>` id **)**
|
|
|
|
Emitted when a new message is received. Note: This signal is NOT emitted when used as high level multiplayer peer.
|
|
|
|
Description
|
|
-----------
|
|
|
|
This class implements a WebSocket server that can also support the high level multiplayer API.
|
|
|
|
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.
|
|
|
|
Note: This class will not work in HTML5 exports due to browser restrictions.
|
|
|
|
Method Descriptions
|
|
-------------------
|
|
|
|
.. _class_WebSocketServer_method_disconnect_peer:
|
|
|
|
- void **disconnect_peer** **(** :ref:`int<class_int>` id, :ref:`int<class_int>` code=1000, :ref:`String<class_String>` reason="" **)**
|
|
|
|
Disconnects the peer identified by ``id`` from the server. See :ref:`WebSocketPeer.close<class_WebSocketPeer_method_close>` for more info.
|
|
|
|
----
|
|
|
|
.. _class_WebSocketServer_method_get_peer_address:
|
|
|
|
- :ref:`String<class_String>` **get_peer_address** **(** :ref:`int<class_int>` id **)** const
|
|
|
|
Returns the IP address of the given peer.
|
|
|
|
----
|
|
|
|
.. _class_WebSocketServer_method_get_peer_port:
|
|
|
|
- :ref:`int<class_int>` **get_peer_port** **(** :ref:`int<class_int>` id **)** const
|
|
|
|
Returns the remote port of the given peer.
|
|
|
|
----
|
|
|
|
.. _class_WebSocketServer_method_has_peer:
|
|
|
|
- :ref:`bool<class_bool>` **has_peer** **(** :ref:`int<class_int>` id **)** const
|
|
|
|
Returns ``true`` if a peer with the given ID is connected.
|
|
|
|
----
|
|
|
|
.. _class_WebSocketServer_method_is_listening:
|
|
|
|
- :ref:`bool<class_bool>` **is_listening** **(** **)** const
|
|
|
|
Returns ``true`` if the server is actively listening on a port.
|
|
|
|
----
|
|
|
|
.. _class_WebSocketServer_method_listen:
|
|
|
|
- :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 **)**
|
|
|
|
Start listening on the given port.
|
|
|
|
You can specify the desired subprotocols via the "protocols" array. If the list empty (default), "binary" will be used.
|
|
|
|
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.
|
|
|
|
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``).
|
|
|
|
----
|
|
|
|
.. _class_WebSocketServer_method_stop:
|
|
|
|
- void **stop** **(** **)**
|
|
|
|
Stop the server and clear its state.
|
|
|