- Home /
How do I handle connection errors in a user friendly way?
I'm trying to create a game where players come together in a lobby before the actual gameplay and for that I have made my own Host Game and Join Game buttons. If a host is running, you can enter the IP address and the port and the client will connect fine. This is done through my custom NetworkLobbyManager:
using UnityEngine;
using System.Collections;
using UnityEngine.Networking;
public class NetworkLobbyManagerCustom : NetworkLobbyManager {
public void setupHost () {
NetworkManager.singleton.StartHost ();
Debug.Log ("Host started");
}
public void unloadHost() {
NetworkManager.singleton.StopHost ();
Debug.Log ("Host stopped");
}
public void setupClient (string ip, int port) {
NetworkManager.singleton.networkPort = port;
NetworkManager.singleton.networkAddress = ip;
Debug.Log ("Connecting...");
NetworkManager.singleton.StartClient ();
}
public void unloadClient() {
NetworkManager.singleton.StopClient ();
}
}
If you enter some nonsense into the IP box the Console will print "DNS resolution failed" and "UNet Client Error Connect Error: 11" error messages as expected. If you enter a correct IP address, but the server isn't running, the scene just restarts.
I'm not quite satisfied with this - I need to be able to tell the user what he did wrong. I've been looking through the documentation to see what I could find and it seems like the OnFailedToConnect() function is what I'm looking for.
Just to try it out, I wanted to copy the following function from the documentation:
void OnFailedToConnect(NetworkConnectionError error) {
Debug.Log("Could not connect to server: " + error);
}
but I had no idea where to put it. I tried putting it in the script for my NetworkLobbyManager which, as you can see above, has a call functions such as StartClient() to succesfully connect to a server - so it made sense to me that I would put the OnFailedToConnect() function there too.
Now when I enter the wrong data or when my server isn't running, StartClient() gets called as per usual and the game will try to connect to the server, but once it fails, OnFailedToConnect() doesn't seem to be called. At least not in the NetworkLobbyManager.
Am I putting OnFailedToConnect() in the wrong place? Or am I supposed to use a different function altogether to handle connection failures and errors? Hell, am I going about this the right way?
Thanks in advance for your help.