- Home /
UNet: How to know when a client fails to connect
Hi, I'm trying to make a custom GUI for the login scene for a game that uses the Networking API
One thing I'm trying to achieve is to display a "Connecting" message when I call StartClient() and if the connection attempt times out, I want to display a "Could not connect to the server" message.
But I have no idea how to do this. I see there are a lot of callbacks available, but I tried OnClientError, OnServerError, OnClientDisconnect, OnClientNotReady but apparently none of these callbacks are called here.
I'm pretty sure this should be possible because the NetworkManager's default HUD does something like that, but I have no idea how, and the documentation is no help.
edit: here is the code of my NetworkManager
using UnityEngine;
using System.Collections;
using UnityEngine.Networking;
public class CustomNetworkManager : NetworkManager {
void OnClientError(NetworkConnection conn, int errorCode) {
print ("ClientError");
print (conn);
print (errorCode);
}
void OnStopClient() {
print ("StopClient");
}
void OnClientDisconnect(NetworkConnection conn) {
print ("ClientDisconnect");
print (conn);
}
void OnClientNotReady(NetworkConnection conn) {
print ("ClientNotReady");
print (conn);
}
}
edit 2: I also tried to get the NetworkClient when I call StartClient and to register a handler for the Error MsgType... to no avail
public void StartClient() {
SetServerIp ();
NetworkClient client = NetworkManager.singleton.StartClient ();
client.RegisterHandler (MsgType.Error, ConnectionError);
}
public void ConnectionError(NetworkMessage netMsg) {
print ("connection error");
print (netMsg);
}
Answer by kroth · Nov 03, 2016 at 07:27 PM
Register a handler for the message type MsgType.Disconnect. This is the message handler called when the client fails to connect to a server.
Thanks a lot, this worked like a charm.
so the solution is as follows
client.RegisterHandler ($$anonymous$$sgType.Disconnect, ConnectionError);
Answer by Cynikal · Nov 03, 2016 at 03:07 PM
void OnFailedToConnect(NetworkConnectionError error) {
Debug.Log("Could not connect to server: " + error);
}
hm... searching in the doc, this is part of the Network class. So how can I hook into this callback from my Network$$anonymous$$anager? I don't even know how to get a reference to the Network
Actually it looks like the Network class is not even part of the Networking namespace, so I believe it's not part of the Networking API (unless I'm misunderstanding something)
I think this is for the old $$anonymous$$asterServer...
Your answer
Follow this Question
Related Questions
Where should I override the Network Manager Callbacks? 0 Answers
Why not using Start exlusively in NetworkBehaviours? 0 Answers
Unity networking tutorial? 6 Answers
Unity TCP async functions 0 Answers
Cmd on a runtime created object 0 Answers