agones.dev/agones@v1.53.0/examples/unity-simple/Assets/Scripts/UdpEchoClient.cs (about)

     1  // Copyright 2019 Google LLC
     2  // All Rights Reserved.
     3  //
     4  // Licensed under the Apache License, Version 2.0 (the "License");
     5  // you may not use this file except in compliance with the License.
     6  // You may obtain a copy of the License at
     7  //
     8  //     http://www.apache.org/licenses/LICENSE-2.0
     9  //
    10  // Unless required by applicable law or agreed to in writing, software
    11  // distributed under the License is distributed on an "AS IS" BASIS,
    12  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  // See the License for the specific language governing permissions and
    14  // limitations under the License.
    15  
    16  using System.Net;
    17  using System.Net.Sockets;
    18  using System.Text;
    19  using UnityEngine;
    20  using UnityEngine.UI;
    21  
    22  namespace AgonesExample
    23  {
    24      public class UdpEchoClient : MonoBehaviour
    25      {
    26          [SerializeField]
    27          private InputField sendTextField;
    28          [SerializeField]
    29          private Text receivedText;
    30          [SerializeField]
    31          private InputField serverAddressField;
    32          [SerializeField]
    33          private InputField serverPortField;
    34  
    35          private UdpClient client;
    36          public string ServerAddress { get; private set; } = "127.0.0.1";
    37          public int ServerPort { get; private set; } = 7777;
    38  
    39          void Start()
    40          {
    41              serverAddressField.text = ServerAddress;
    42              serverPortField.text = ServerPort.ToString();
    43  
    44              client = new UdpClient(ServerAddress, ServerPort);
    45          }
    46  
    47          void Update()
    48          {
    49              if (client.Available > 0)
    50              {
    51                  IPEndPoint remote = null;
    52                  byte[] rbytes = client.Receive(ref remote);
    53                  string received = Encoding.UTF8.GetString(rbytes);
    54  
    55                  Debug.Log($"Client - Recv {received}");
    56  
    57                  receivedText.text = received;
    58              }
    59          }
    60  
    61          // Invoke by "Change Server" Button.
    62          public void ChangeServer()
    63          {
    64              if (IPAddress.TryParse(serverAddressField.text, out IPAddress ip))
    65              {
    66                  ServerAddress = ip.ToString();
    67              }
    68              if (int.TryParse(serverPortField.text, out int port))
    69              {
    70                  ServerPort = port;
    71              }
    72  
    73              client = new UdpClient(ServerAddress, ServerPort);
    74  
    75              Debug.Log($"Client - ChangeServer {ServerAddress}:{ServerPort}");
    76          }
    77  
    78          // Invoke by "Send" Button.
    79          public void SendTextToServer()
    80          {
    81              if (string.IsNullOrWhiteSpace(sendTextField.text))
    82              {
    83                  return;
    84              }
    85  
    86              Debug.Log($"Client - SendText {sendTextField.text}");
    87  
    88              byte[] bytes = Encoding.UTF8.GetBytes(sendTextField.text);
    89              client.Send(bytes, bytes.Length);
    90  
    91              sendTextField.text = "";
    92          }
    93      }
    94  }