diff options
Diffstat (limited to 'doc/classes/UDPServer.xml')
-rw-r--r-- | doc/classes/UDPServer.xml | 54 |
1 files changed, 28 insertions, 26 deletions
diff --git a/doc/classes/UDPServer.xml b/doc/classes/UDPServer.xml index c3a3a49a80..8151ecb625 100644 --- a/doc/classes/UDPServer.xml +++ b/doc/classes/UDPServer.xml @@ -9,7 +9,8 @@ Below a small example of how it can be used: [codeblocks] [gdscript] - class_name Server + # server_node.gd + class_name ServerNode extends Node var server := UDPServer.new() @@ -34,35 +35,35 @@ pass # Do something with the connected peers. [/gdscript] [csharp] + // ServerNode.cs using Godot; - using System; using System.Collections.Generic; - public class Server : Node + public partial class ServerNode : Node { - public UDPServer Server = new UDPServer(); - public List<PacketPeerUDP> Peers = new List<PacketPeerUDP>(); + private UdpServer _server = new UdpServer(); + private List<PacketPeerUdp> _peers = new List<PacketPeerUdp>(); public override void _Ready() { - Server.Listen(4242); + _server.Listen(4242); } - public override void _Process(float delta) + public override void _Process(double delta) { - Server.Poll(); // Important! - if (Server.IsConnectionAvailable()) + _server.Poll(); // Important! + if (_server.IsConnectionAvailable()) { - PacketPeerUDP peer = Server.TakeConnection(); + PacketPeerUdp peer = _server.TakeConnection(); byte[] packet = peer.GetPacket(); - GD.Print($"Accepted Peer: {peer.GetPacketIp()}:{peer.GetPacketPort()}"); - GD.Print($"Received Data: {packet.GetStringFromUTF8()}"); + GD.Print($"Accepted Peer: {peer.GetPacketIP()}:{peer.GetPacketPort()}"); + GD.Print($"Received Data: {packet.GetStringFromUtf8()}"); // Reply so it knows we received the message. peer.PutPacket(packet); // Keep a reference so we can keep contacting the remote peer. - Peers.Add(peer); + _peers.Add(peer); } - foreach (var peer in Peers) + foreach (var peer in _peers) { // Do something with the peers. } @@ -72,7 +73,8 @@ [/codeblocks] [codeblocks] [gdscript] - class_name Client + # client_node.gd + class_name ClientNode extends Node var udp := PacketPeerUDP.new() @@ -90,30 +92,30 @@ connected = true [/gdscript] [csharp] + // ClientNode.cs using Godot; - using System; - public class Client : Node + public partial class ClientNode : Node { - public PacketPeerUDP Udp = new PacketPeerUDP(); - public bool Connected = false; + private PacketPeerUdp _udp = new PacketPeerUdp(); + private bool _connected = false; public override void _Ready() { - Udp.ConnectToHost("127.0.0.1", 4242); + _udp.ConnectToHost("127.0.0.1", 4242); } - public override void _Process(float delta) + public override void _Process(double delta) { - if (!Connected) + if (!_connected) { // Try to contact server - Udp.PutPacket("The Answer Is..42!".ToUTF8()); + _udp.PutPacket("The Answer Is..42!".ToUtf8()); } - if (Udp.GetAvailablePacketCount() > 0) + if (_udp.GetAvailablePacketCount() > 0) { - GD.Print($"Connected: {Udp.GetPacket().GetStringFromUTF8()}"); - Connected = true; + GD.Print($"Connected: {_udp.GetPacket().GetStringFromUtf8()}"); + _connected = true; } } } |