Skip to content

Commit da0eb0a

Browse files
feat: Improve error message on connection failure (#4207)
1 parent 9f88aea commit da0eb0a

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
@@ -1146,7 +1146,7 @@ private bool ProcessEvent()
11461146
// means a clean disconnect that doesn't require logging.
11471147
if (!m_Driver.Listening && m_ServerClientId == default)
11481148
{
1149-
Debug.LogError("Failed to connect to server.");
1149+
LogClientConnectionFailureMessage();
11501150
}
11511151

11521152
// Set the disconnect event error code
@@ -1174,6 +1174,40 @@ private bool ProcessEvent()
11741174
return false;
11751175
}
11761176

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

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

0 commit comments

Comments
 (0)