[UNET] Display error on failure to connect

The documentation states the following:
“If the connection fails, a MsgType.Error message will be generated”
https://docs.unity3d.com/550/Documentation/ScriptReference/Networking.NetworkClient.Connect.html

I’m using NetworkClient.Connect, and am listening for a MsgType.Error, and receive no calls to the listening delegate. I’m trying to connect without even running a server in order to test this functionality. I looked for MsgType.SYSTEM_ERROR as indicated by the documentation, but it doesn’t seem to be available in 5.5.x.

I can’t receive a message when an attempt to connect to a server fails. Does anyone have a solution to this?

Yeah, the example in the documentation for NetworkClient.Connect is actually out of date, the code that it shows is actually wrong because the MsgType enum and NetworkMessageDelegate class have bothed changed since it was written. The idea is still the same though.

Ultimately, a failure to connect will currently trigger MsgType.Disconnect. In some cases it does seem to trigger MsgType.Error, however my experimenting has shown that there are inconsistencies with this. So, to be able to do something when you are disconnected due to a timeout, use MsgType.Disconnect.

Note however that if on the server side, you call NetworkConnection.Disconnect (to disconnect one of the peers on the server side), there does not appear to be any message sent to the client that you can hook onto. This seems to be a bug, which hopefully will be fixed soon. For now though, I am checking whether NetworkClient.isConnected changes from true to false to determine an unexpected disconnect.

It’s all a bit tricky at the moment, but it is slowly improving! If you need further info, let me know.