Skip to content

Commit 183cbc8

Browse files
feat: Improve error message on connection failure [2.X] (#4208)
feat: Improve error message on connection failure
1 parent 07c326e commit 183cbc8

2 files changed

Lines changed: 37 additions & 2 deletions

File tree

‎com.unity.netcode.gameobjects/Runtime/Transports/UTP/UnityTransport.cs‎

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1144,7 +1144,7 @@ private bool ProcessEvent()
11441144
// means a clean disconnect that doesn't require logging.
11451145
if (!m_Driver.Listening && m_ServerClientId == default)
11461146
{
1147-
Debug.LogError("Failed to connect to server.");
1147+
LogClientConnectionFailureMessage();
11481148
}
11491149

11501150
// Set the disconnect event error code
@@ -1172,6 +1172,40 @@ private bool ProcessEvent()
11721172
return false;
11731173
}
11741174

1175+
private void LogClientConnectionFailureMessage()
1176+
{
1177+
if (OnCurrentTopology() == NetworkTopologyTypes.ClientServer)
1178+
{
1179+
var reasons = new List<string> { "Server/host is not running." };
1180+
1181+
if (m_ProtocolType == ProtocolType.RelayUnityTransport && !m_UseWebSockets)
1182+
{
1183+
reasons.Add("Relay port might be blocked on your network. Try using WebSockets.");
1184+
if (m_RelayServerData.IsSecure != 0)
1185+
{
1186+
reasons.Add("DTLS might be blocked on your network. Try using UDP or WebSockets.");
1187+
}
1188+
}
1189+
else if (m_ProtocolType != ProtocolType.RelayUnityTransport)
1190+
{
1191+
reasons.Add($"Server/host might not be reachable. Try \"ping {ConnectionData.Address}\" in a terminal.");
1192+
reasons.Add("The firewall on the server/host might be blocking the connection attempts.");
1193+
if (m_UseEncryption && !m_UseWebSockets)
1194+
{
1195+
reasons.Add("DTLS might be blocked on your network. Try using WebSockets.");
1196+
}
1197+
}
1198+
1199+
const string prefix = "\n\t- ";
1200+
Debug.LogError("Failed to connect to server. Possible reasons:" + prefix + string.Join(prefix, reasons));
1201+
}
1202+
else
1203+
{
1204+
// If using DA the SDK should have logged an actual useful error.
1205+
Debug.LogError("Failed to connect to server.");
1206+
}
1207+
}
1208+
11751209
/// <summary>
11761210
/// Handles accepting new connections and processing transport events.
11771211
/// </summary>

‎com.unity.netcode.gameobjects/Tests/Runtime/Connection/ClientOnlyConnectionTests.cs‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System.Collections;
2+
using System.Text.RegularExpressions;
23
using NUnit.Framework;
34
using Unity.Netcode.TestHelpers.Runtime;
45
using Unity.Netcode.Transports.UTP;
@@ -48,7 +49,7 @@ public IEnumerator ClientFailsToConnect()
4849
m_ClientNetworkManager.StartClient();
4950

5051
// Unity Transport throws an error when it times out
51-
LogAssert.Expect(LogType.Error, "Failed to connect to server.");
52+
LogAssert.Expect(LogType.Error, new Regex("^Failed to connect to server\\."));
5253
yield return NetcodeIntegrationTest.WaitForConditionOrTimeOut(() => m_WasDisconnected, m_TimeoutHelper);
5354
Assert.False(m_TimeoutHelper.TimedOut, "Timed out waiting for client to timeout waiting to connect!");
5455

0 commit comments

Comments
 (0)