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
This commit is contained in:
2021-10-13 14:01:20 -05:00
parent 53daceac89
commit dd613b4044

View File

@@ -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) {