Skip to content

Commit 82dc3b0

Browse files
Merge develop-2.0.0 into chore/remove-spawn-state-exceptions-network-object
2 parents 06858a8 + 6c2db82 commit 82dc3b0

File tree

4 files changed

+72
-1
lines changed

4 files changed

+72
-1
lines changed

com.unity.netcode.gameobjects/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ Additional documentation and release notes are available at [Multiplayer Documen
1010

1111
### Added
1212

13+
- Added a `WebSocketPath` field to `UnityTransport.ConnectionData` (which also shows up in the inspector if "Use WebSockets" is checked) that controls the path clients will connect to and servers/hosts will listen on when using WebSockets. (#3901)
1314
- `NetworkTransport.EarlyUpdate` and `NetworkTransport.PostLateUpdate` are now public. For the vast majority of users, there's really no point in ever calling those methods directly (the `NetworkManager` handles it). It's only useful if wrapping transports outside of NGO. (#3890)
1415

1516
### Changed

com.unity.netcode.gameobjects/Editor/HiddenScriptEditor.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ public class UnityTransportEditor : HiddenScriptEditor
4242

4343
private SerializedProperty m_ServerAddressProperty;
4444
private SerializedProperty m_ServerPortProperty;
45+
private SerializedProperty m_WebSocketPathProperty;
4546

4647
private const string k_LoopbackIpv4 = "127.0.0.1";
4748
private const string k_LoopbackIpv6 = "::1";
@@ -62,6 +63,7 @@ private void Initialize()
6263

6364
m_ServerAddressProperty = connectionDataProperty.FindPropertyRelative(nameof(UnityTransport.ConnectionAddressData.Address));
6465
m_ServerPortProperty = connectionDataProperty.FindPropertyRelative(nameof(UnityTransport.ConnectionAddressData.Port));
66+
m_WebSocketPathProperty = connectionDataProperty.FindPropertyRelative(nameof(UnityTransport.ConnectionAddressData.WebSocketPath));
6567
}
6668

6769
/// <summary>
@@ -79,6 +81,11 @@ public override void OnInspectorGUI()
7981
EditorGUILayout.PropertyField(m_ServerAddressProperty);
8082
EditorGUILayout.PropertyField(m_ServerPortProperty);
8183

84+
if (m_UnityTransport.UseWebSockets)
85+
{
86+
EditorGUILayout.PropertyField(m_WebSocketPathProperty);
87+
}
88+
8289
serializedObject.ApplyModifiedProperties();
8390

8491
EditorGUILayout.HelpBox("It's recommended to leave remote connections disabled for local testing to avoid exposing ports on your device.", MessageType.Info);

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

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ public enum ProtocolType
6868
// frame at 60 FPS. This will be a large over-estimation in any realistic scenario.
6969
private const int k_MaxReliableThroughput = (NetworkParameterConstants.MTU * 64 * 60) / 1000; // bytes per millisecond
7070

71-
private static ConnectionAddressData s_DefaultConnectionAddressData = new ConnectionAddressData { Address = "127.0.0.1", Port = 7777, ServerListenAddress = string.Empty };
71+
private static ConnectionAddressData s_DefaultConnectionAddressData = new ConnectionAddressData { Address = "127.0.0.1", Port = 7777, WebSocketPath = "/", ServerListenAddress = string.Empty };
7272

7373
#pragma warning disable IDE1006 // Naming Styles
7474
/// <summary>
@@ -228,6 +228,13 @@ public struct ConnectionAddressData
228228
[SerializeField]
229229
public ushort Port;
230230

231+
/// <summary>
232+
/// Path of the URL when using WebSockets.
233+
/// </summary>
234+
[Tooltip("Path to connect to or listen on when using WebSockets. Defaults to \"/\" if not set.")]
235+
[SerializeField]
236+
public string WebSocketPath;
237+
231238
/// <summary>
232239
/// IP address the server will listen on. If not provided, will use localhost.
233240
/// </summary>
@@ -513,6 +520,12 @@ public NetworkSettings GetDefaultNetworkSettings()
513520
}
514521
}
515522

523+
// Set up the WebSocket path if configured and if using WebSockets.
524+
if (m_UseWebSockets && m_ProtocolType != ProtocolType.RelayUnityTransport && !string.IsNullOrWhiteSpace(ConnectionData.WebSocketPath))
525+
{
526+
settings.WithWebSocketParameters(path: ConnectionData.WebSocketPath);
527+
}
528+
516529
#if UNITY_MP_TOOLS_NETSIM_IMPLEMENTATION_ENABLED
517530
// Latency, jitter and packet loss will be set by the network simulator in the tools
518531
// package. We just need to initialize the settings since otherwise these features will

com.unity.netcode.gameobjects/Tests/Runtime/Transports/UnityTransportConnectionTests.cs

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,56 @@ public IEnumerator ConnectSingleClient_IPAddress()
7979
yield return null;
8080
}
8181

82+
// Check connection with a single WebSocket client (IP address).
83+
[UnityTest]
84+
public IEnumerator ConnectSingleClient_WebSocket_IPAddress()
85+
{
86+
InitializeTransport(out m_Server, out m_ServerEvents);
87+
InitializeTransport(out m_Clients[0], out m_ClientsEvents[0]);
88+
89+
m_Server.UseWebSockets = true;
90+
m_Clients[0].UseWebSockets = true;
91+
92+
m_Clients[0].SetConnectionData("127.0.0.1", 7777);
93+
94+
m_Server.StartServer();
95+
m_Clients[0].StartClient();
96+
97+
yield return WaitForNetworkEvent(NetworkEvent.Connect, m_ClientsEvents[0]);
98+
99+
// Check we've received Connect event on server too.
100+
Assert.AreEqual(1, m_ServerEvents.Count);
101+
Assert.AreEqual(NetworkEvent.Connect, m_ServerEvents[0].Type);
102+
103+
yield return null;
104+
}
105+
106+
// Check connection with a single WebSocket client (IP address and path).
107+
[UnityTest]
108+
public IEnumerator ConnectSingleClient_WebSocket_IPAddressAndPath()
109+
{
110+
InitializeTransport(out m_Server, out m_ServerEvents);
111+
InitializeTransport(out m_Clients[0], out m_ClientsEvents[0]);
112+
113+
m_Server.UseWebSockets = true;
114+
m_Clients[0].UseWebSockets = true;
115+
116+
m_Clients[0].SetConnectionData("127.0.0.1", 7777);
117+
m_Clients[0].ConnectionData.WebSocketPath = "/test";
118+
m_Server.ConnectionData.WebSocketPath = "/test";
119+
120+
m_Server.StartServer();
121+
m_Clients[0].StartClient();
122+
123+
yield return WaitForNetworkEvent(NetworkEvent.Connect, m_ClientsEvents[0]);
124+
125+
// Check we've received Connect event on server too.
126+
Assert.AreEqual(1, m_ServerEvents.Count);
127+
Assert.AreEqual(NetworkEvent.Connect, m_ServerEvents[0].Type);
128+
129+
yield return null;
130+
}
131+
82132
#if HOSTNAME_RESOLUTION_AVAILABLE
83133
// Check connection with a single client (hostname).
84134
[UnityTest]

0 commit comments

Comments
 (0)