class_udpserver.rst 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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 UDPServer.xml source instead.
  4. .. The source is found in doc/classes or modules/<name>/doc_classes.
  5. .. _class_UDPServer:
  6. UDPServer
  7. =========
  8. **Inherits:** :ref:`Reference<class_Reference>` **<** :ref:`Object<class_Object>`
  9. Helper class to implement a UDP server.
  10. Description
  11. -----------
  12. A simple server that opens a UDP socket and returns connected :ref:`PacketPeerUDP<class_PacketPeerUDP>` upon receiving new packets. See also :ref:`PacketPeerUDP.connect_to_host<class_PacketPeerUDP_method_connect_to_host>`.
  13. Below a small example of how it can be used:
  14. ::
  15. # server.gd
  16. extends Node
  17. var server := UDPServer.new()
  18. var peers = []
  19. func _ready():
  20. server.listen(4242)
  21. func _process(delta):
  22. if server.is_connection_available():
  23. var peer : PacketPeerUDP = server.take_connection()
  24. var pkt = peer.get_packet()
  25. print("Accepted peer: %s:%s" % [peer.get_packet_ip(), peer.get_packet_port()])
  26. print("Received data: %s" % [pkt.get_string_from_utf8()])
  27. # Reply so it knows we received the message.
  28. peer.put_packet(pkt)
  29. # Keep a reference so we can keep contacting the remote peer.
  30. peers.append(peer)
  31. for i in range(0, peers.size()):
  32. pass # Do something with the connected peers.
  33. ::
  34. # client.gd
  35. extends Node
  36. var udp := PacketPeerUDP.new()
  37. var connected = false
  38. func _ready():
  39. udp.connect_to_host("127.0.0.1", 4242)
  40. func _process(delta):
  41. if !connected:
  42. # Try to contact server
  43. udp.put_packet("The answer is... 42!".to_utf8())
  44. if udp.get_available_packet_count() > 0:
  45. print("Connected: %s" % udp.get_packet().get_string_from_utf8())
  46. connected = true
  47. Methods
  48. -------
  49. +-------------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------+
  50. | :ref:`bool<class_bool>` | :ref:`is_connection_available<class_UDPServer_method_is_connection_available>` **(** **)** const |
  51. +-------------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------+
  52. | :ref:`bool<class_bool>` | :ref:`is_listening<class_UDPServer_method_is_listening>` **(** **)** const |
  53. +-------------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------+
  54. | :ref:`Error<enum_@GlobalScope_Error>` | :ref:`listen<class_UDPServer_method_listen>` **(** :ref:`int<class_int>` port, :ref:`String<class_String>` bind_address="*" **)** |
  55. +-------------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------+
  56. | void | :ref:`stop<class_UDPServer_method_stop>` **(** **)** |
  57. +-------------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------+
  58. | :ref:`PacketPeerUDP<class_PacketPeerUDP>` | :ref:`take_connection<class_UDPServer_method_take_connection>` **(** **)** |
  59. +-------------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------+
  60. Method Descriptions
  61. -------------------
  62. .. _class_UDPServer_method_is_connection_available:
  63. - :ref:`bool<class_bool>` **is_connection_available** **(** **)** const
  64. Returns ``true`` if a packet with a new address/port combination is received on the socket.
  65. ----
  66. .. _class_UDPServer_method_is_listening:
  67. - :ref:`bool<class_bool>` **is_listening** **(** **)** const
  68. Returns ``true`` if the socket is open and listening on a port.
  69. ----
  70. .. _class_UDPServer_method_listen:
  71. - :ref:`Error<enum_@GlobalScope_Error>` **listen** **(** :ref:`int<class_int>` port, :ref:`String<class_String>` bind_address="*" **)**
  72. Starts the server by opening a UDP socket listening on the given port. You can optionally specify a ``bind_address`` to only listen for packets sent to that address. See also :ref:`PacketPeerUDP.listen<class_PacketPeerUDP_method_listen>`.
  73. ----
  74. .. _class_UDPServer_method_stop:
  75. - void **stop** **(** **)**
  76. Stops the server, closing the UDP socket if open. Will not disconnect any connected :ref:`PacketPeerUDP<class_PacketPeerUDP>`.
  77. ----
  78. .. _class_UDPServer_method_take_connection:
  79. - :ref:`PacketPeerUDP<class_PacketPeerUDP>` **take_connection** **(** **)**
  80. Returns a :ref:`PacketPeerUDP<class_PacketPeerUDP>` connected to the address/port combination of the first packet in queue. Will return ``null`` if no packet is in queue. See also :ref:`PacketPeerUDP.connect_to_host<class_PacketPeerUDP_method_connect_to_host>`.