From dd613b40442bf5c9d9b5ecdffa3d5e73a523d52c Mon Sep 17 00:00:00 2001 From: Julia Butenhoff Date: Wed, 13 Oct 2021 14:01:20 -0500 Subject: [PATCH] Catching of Client Connection Timeout Error - Now connecting as a client will throw an error after a set amount of time if it didn't connect to a server --- Assets/Scripts/Network/ConnectButtons.cs | 30 ++++++++++++++++++++---- 1 file changed, 26 insertions(+), 4 deletions(-) diff --git a/Assets/Scripts/Network/ConnectButtons.cs b/Assets/Scripts/Network/ConnectButtons.cs index 9ff0429..5aaab52 100644 --- a/Assets/Scripts/Network/ConnectButtons.cs +++ b/Assets/Scripts/Network/ConnectButtons.cs @@ -8,6 +8,10 @@ using UnityEngine.UI; public class ConnectButtons : MonoBehaviour { + // The IP Used to connect to the dedicated server + const string DEDICATED_SERVER_IP = "127.0.0.1"; + const int DEDICATED_SERVER_PORT = 7777; + private UNetTransport transport; public GameObject ErrorPanel; @@ -27,8 +31,18 @@ public class ConnectButtons : MonoBehaviour { // Connect to the dedicated server public void ConnectDedicatedServer () { - // TODO: Connect to the dedicated server - ThrowError("Connect to Dedicated Server Not Implemented Yet!"); + // Set the connection address to be equal to the ip address of the dedicated server + transport.ConnectAddress = DEDICATED_SERVER_IP; + transport.ConnectPort = DEDICATED_SERVER_PORT; + + // Set the password to connect with + NetworkManager.Singleton.NetworkConfig.ConnectionData = System.Text.Encoding.ASCII.GetBytes("kingsrace"); + + // Start the client on the address + NetworkManager.Singleton.StartClient(); + + // Run a coroutine to check if the client connects to the server + StartCoroutine(checkIsConnectedClient()); } // Host a private server @@ -40,6 +54,7 @@ public class ConnectButtons : MonoBehaviour { // Connect to a private server via IP Address public void ConnectToPrivateServer() { string ipAddress = ipAddressField.text; + int port = 7777; // Check if the IP Address is valid if (!ValidateIPv4(ipAddress)) { @@ -51,6 +66,7 @@ public class ConnectButtons : MonoBehaviour { // Set the connection address to be equal to the ip address entered into the input field transport.ConnectAddress = ipAddress; + transport.ConnectPort = port; // Set the password to connect with NetworkManager.Singleton.NetworkConfig.ConnectionData = System.Text.Encoding.ASCII.GetBytes("kingsrace"); @@ -58,13 +74,19 @@ public class ConnectButtons : MonoBehaviour { // Start the client on the address NetworkManager.Singleton.StartClient(); - // TODO: Have to throw an error if can't connect to that IP + // Run a coroutine to check if the client connects to the server + StartCoroutine(checkIsConnectedClient()); } IEnumerator checkIsConnectedClient() { yield return new WaitForSecondsRealtime(connectionTimeoutTime); - + if (!NetworkManager.Singleton.IsConnectedClient) { + // Failed to connect to the server + NetworkManager.Singleton.StopClient(); + + ThrowError("Could not connect to server!\nReason: Connection Timed Out"); + } } private void ThrowError(string errorMsg) {