Skip to content
Open
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 @@ -1146,7 +1146,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 @@ -1174,6 +1174,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 1190 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#L1184-L1190

Added lines #L1184 - L1190 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

Encrypted WebGL clients with UseWebSockets=false receive advice to switch to WebSockets even though WebSockets are already active. In WebGL player builds, CreateDriver selects WebSocketNetworkInterface even when m_UseWebSockets is false. LogClientConnectionFailureMessage instead checks the serialized flag, so it incorrectly suggests that DTLS is involved. Determine whether WebSockets are effectively enabled, including the WebGL fallback. Use that result in both WebSocket checks in LogClientConnectionFailureMessage.

🤖 Helpful? 👍/👎

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

You get a warning in the console when we end up using WebSockets even if UseWebSockets is false. It's not something that's supposed to happen on a correctly configured transport so I don't think it's worth handling here.

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

Check warning on line 1198 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#L1196-L1198

Added lines #L1196 - L1198 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 1205 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

Added line #L1205 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 1208 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#L1207-L1208

Added lines #L1207 - L1208 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 @@ -50,7 +51,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