Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1144,7 +1144,7 @@
// means a clean disconnect that doesn't require logging.
if (!m_Driver.Listening && m_ServerClientId == default)
{
Debug.LogError("Failed to connect to server.");
LogClientConnectionFailureMessage();
}

// Set the disconnect event error code
Expand Down Expand Up @@ -1172,6 +1172,40 @@
return false;
}

private void LogClientConnectionFailureMessage()
{
if (OnCurrentTopology() == NetworkTopologyTypes.ClientServer)
{
var reasons = new List<string> { "Server/host is not running." };

if (m_ProtocolType == ProtocolType.RelayUnityTransport && !m_UseWebSockets)
{
reasons.Add("Relay port might be blocked on your network. Try using WebSockets.");
if (m_RelayServerData.IsSecure != 0)
{
reasons.Add("DTLS might be blocked on your network. Try using UDP or WebSockets.");
}
}

Check warning on line 1188 in com.unity.netcode.gameobjects/Runtime/Transports/UTP/UnityTransport.cs

View check run for this annotation

Codecov GitHub.com / codecov/patch

com.unity.netcode.gameobjects/Runtime/Transports/UTP/UnityTransport.cs#L1182-L1188

Added lines #L1182 - L1188 were not covered by tests
else if (m_ProtocolType != ProtocolType.RelayUnityTransport)
{
reasons.Add($"Server/host might not be reachable. Try \"ping {ConnectionData.Address}\" in a terminal.");
reasons.Add("The firewall on the server/host might be blocking the connection attempts.");
if (m_UseEncryption && !m_UseWebSockets)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2

In browser builds with UseWebSockets false and UseEncryption true, failed direct connections incorrectly report DTLS as a possible cause. CreateDriver forces WebSocketNetworkInterface under UNITY_WEBGL && !UNITY_EDITOR, even when UseWebSockets is false. The connection therefore uses TLS over WebSockets, and recommending WebSockets sends users toward a protocol already in use. The Relay diagnostic has the same flag mismatch. Base these conditions on the effective protocol, including the WebGL fallback.

🤖 Helpful? 👍/👎

{
reasons.Add("DTLS might be blocked on your network. Try using WebSockets.");
}

Check warning on line 1196 in com.unity.netcode.gameobjects/Runtime/Transports/UTP/UnityTransport.cs

View check run for this annotation

Codecov GitHub.com / codecov/patch

com.unity.netcode.gameobjects/Runtime/Transports/UTP/UnityTransport.cs#L1194-L1196

Added lines #L1194 - L1196 were not covered by tests
}

const string prefix = "\n\t- ";
Debug.LogError("Failed to connect to server. Possible reasons:" + prefix + string.Join(prefix, reasons));
}
else
{

Check warning on line 1203 in com.unity.netcode.gameobjects/Runtime/Transports/UTP/UnityTransport.cs

View check run for this annotation

Codecov GitHub.com / codecov/patch

com.unity.netcode.gameobjects/Runtime/Transports/UTP/UnityTransport.cs#L1203

Added line #L1203 was not covered by tests
// If using DA the SDK should have logged an actual useful error.
Debug.LogError("Failed to connect to server.");
}

Check warning on line 1206 in com.unity.netcode.gameobjects/Runtime/Transports/UTP/UnityTransport.cs

View check run for this annotation

Codecov GitHub.com / codecov/patch

com.unity.netcode.gameobjects/Runtime/Transports/UTP/UnityTransport.cs#L1205-L1206

Added lines #L1205 - L1206 were not covered by tests
}

/// <summary>
/// Handles accepting new connections and processing transport events.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections;
using System.Text.RegularExpressions;
using NUnit.Framework;
using Unity.Netcode.TestHelpers.Runtime;
using Unity.Netcode.Transports.UTP;
Expand Down Expand Up @@ -48,7 +49,7 @@ public IEnumerator ClientFailsToConnect()
m_ClientNetworkManager.StartClient();

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

Expand Down
Loading