From 8c341e7cd1cb56dc8850190141564d4662f5d49c Mon Sep 17 00:00:00 2001 From: Jeffrey Rainy Date: Thu, 1 Sep 2022 13:29:05 -0400 Subject: [PATCH 1/7] chore: websockets. Minimalist UI to use WebSockets --- .../Runtime/Transports/UTP/UnityTransport.cs | 23 ++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/com.unity.netcode.gameobjects/Runtime/Transports/UTP/UnityTransport.cs b/com.unity.netcode.gameobjects/Runtime/Transports/UTP/UnityTransport.cs index b46720ea66..4d81880eb9 100644 --- a/com.unity.netcode.gameobjects/Runtime/Transports/UTP/UnityTransport.cs +++ b/com.unity.netcode.gameobjects/Runtime/Transports/UTP/UnityTransport.cs @@ -150,6 +150,18 @@ private enum State [SerializeField] private ProtocolType m_ProtocolType; +#if UTP_TRANSPORT_2_0_ABOVE + [Tooltip("Whether or not to use WebSockets as Network Interface")] + [SerializeField] + private bool m_UseWebSockets = false; + + public bool UseWebSockets + { + set => m_UseWebSockets = value; + get => m_UseWebSockets; + } +#endif + [Tooltip("The maximum amount of packets that can be in the internal send/receive queues. Basically this is how many packets can be sent/received in a single update/frame.")] [SerializeField] private int m_MaxPacketQueueSize = InitialMaxPacketQueueSize; @@ -1362,7 +1374,16 @@ public void CreateDriver(UnityTransport transport, out NetworkDriver driver, #endif heartbeatTimeoutMS: transport.m_HeartbeatTimeoutMS); - driver = NetworkDriver.Create(m_NetworkSettings); +#if UTP_TRANSPORT_2_0_ABOVE + if (m_UseWebSockets) + { + driver = NetworkDriver.Create(new WebSocketNetworkInterface(), m_NetworkSettings); + } + else +#endif + { + driver = NetworkDriver.Create(new BaselibNetworkInterface(), m_NetworkSettings); + } #if MULTIPLAYER_TOOLS_1_0_0_PRE_7 #if UTP_TRANSPORT_2_0_ABOVE From 43e09850af0dea05aa56c5b7abd63af88e0d5f65 Mon Sep 17 00:00:00 2001 From: Jeffrey Rainy Date: Tue, 13 Sep 2022 16:10:52 -0400 Subject: [PATCH 2/7] fix: adjusting #ifdef on transport version to allow building non-webgl platforms with UTP 2.0 --- .../Runtime/Transports/UTP/UnityTransport.cs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/com.unity.netcode.gameobjects/Runtime/Transports/UTP/UnityTransport.cs b/com.unity.netcode.gameobjects/Runtime/Transports/UTP/UnityTransport.cs index fa302d04d4..00288aa0a4 100644 --- a/com.unity.netcode.gameobjects/Runtime/Transports/UTP/UnityTransport.cs +++ b/com.unity.netcode.gameobjects/Runtime/Transports/UTP/UnityTransport.cs @@ -1383,10 +1383,12 @@ public void CreateDriver(UnityTransport transport, out NetworkDriver driver, driver = NetworkDriver.Create(new WebSocketNetworkInterface(), m_NetworkSettings); } else -#endif { - driver = NetworkDriver.Create(new BaselibNetworkInterface(), m_NetworkSettings); + driver = NetworkDriver.Create(new UDPNetworkInterface(), m_NetworkSettings); } +#else + driver = NetworkDriver.Create(m_NetworkSettings); +#endif #if MULTIPLAYER_TOOLS_1_0_0_PRE_7 #if UTP_TRANSPORT_2_0_ABOVE From 54e9c1a5b4fd51d7865b352ad52205617200b592 Mon Sep 17 00:00:00 2001 From: Jeffrey Rainy Date: Tue, 13 Sep 2022 16:48:43 -0400 Subject: [PATCH 3/7] fix: addressing build issue where WebGL platform requires different name for UTP symbols --- .../Runtime/Transports/UTP/UnityTransport.cs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/com.unity.netcode.gameobjects/Runtime/Transports/UTP/UnityTransport.cs b/com.unity.netcode.gameobjects/Runtime/Transports/UTP/UnityTransport.cs index b23e3f55d4..ffc57cb6ac 100644 --- a/com.unity.netcode.gameobjects/Runtime/Transports/UTP/UnityTransport.cs +++ b/com.unity.netcode.gameobjects/Runtime/Transports/UTP/UnityTransport.cs @@ -1384,7 +1384,11 @@ public void CreateDriver(UnityTransport transport, out NetworkDriver driver, } else { +#if UNITY_WEBGL + driver = NetworkDriver.Create(new BaselibNetworkInterface(), m_NetworkSettings); +#else driver = NetworkDriver.Create(new UDPNetworkInterface(), m_NetworkSettings); +#endif } #else driver = NetworkDriver.Create(m_NetworkSettings); From e03d4ad31209c53525e318dab9a08bb1168d2fc4 Mon Sep 17 00:00:00 2001 From: Jeffrey Rainy Date: Tue, 13 Sep 2022 17:20:00 -0400 Subject: [PATCH 4/7] fix: emitting a warning and switching to WebSockets if uers don't, on WebGL platform --- .../Runtime/Transports/UTP/UnityTransport.cs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/com.unity.netcode.gameobjects/Runtime/Transports/UTP/UnityTransport.cs b/com.unity.netcode.gameobjects/Runtime/Transports/UTP/UnityTransport.cs index ffc57cb6ac..a62a56726d 100644 --- a/com.unity.netcode.gameobjects/Runtime/Transports/UTP/UnityTransport.cs +++ b/com.unity.netcode.gameobjects/Runtime/Transports/UTP/UnityTransport.cs @@ -1385,7 +1385,8 @@ public void CreateDriver(UnityTransport transport, out NetworkDriver driver, else { #if UNITY_WEBGL - driver = NetworkDriver.Create(new BaselibNetworkInterface(), m_NetworkSettings); + Debug.LogWarning("WebSockets were used even though they're not selected in NetworkManager. You should adjust the setting, to silence this warning."); + driver = NetworkDriver.Create(new WebSocketNetworkInterface(), m_NetworkSettings); #else driver = NetworkDriver.Create(new UDPNetworkInterface(), m_NetworkSettings); #endif From 0c6928e3b724f4529ea6e9cc9943bb55db9d6122 Mon Sep 17 00:00:00 2001 From: Jeffrey Rainy Date: Tue, 13 Sep 2022 17:52:25 -0400 Subject: [PATCH 5/7] Addressing PR review comments --- com.unity.netcode.gameobjects/CHANGELOG.md | 4 ++++ .../Runtime/Transports/UTP/UnityTransport.cs | 2 +- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/com.unity.netcode.gameobjects/CHANGELOG.md b/com.unity.netcode.gameobjects/CHANGELOG.md index e4ba64a1f0..e34095830a 100644 --- a/com.unity.netcode.gameobjects/CHANGELOG.md +++ b/com.unity.netcode.gameobjects/CHANGELOG.md @@ -9,6 +9,10 @@ Additional documentation and release notes are available at [Multiplayer Documen ## [Unreleased] +### Added + +- WebSocket supports. When using UTP 2.0, a property `UseWebSockets` in the `UtpTransport` component of the `NetworkManager` allows picking WebSockets for communication. When building for WebGL, this selection happens automatically. + ### Changed - The debug simulator in `UnityTransport` is now non-deterministic. Its random number generator used to be seeded with a constant value, leading to the same pattern of packet drops, delays, and jitter in every run. (#2196) diff --git a/com.unity.netcode.gameobjects/Runtime/Transports/UTP/UnityTransport.cs b/com.unity.netcode.gameobjects/Runtime/Transports/UTP/UnityTransport.cs index a62a56726d..49d81e50c3 100644 --- a/com.unity.netcode.gameobjects/Runtime/Transports/UTP/UnityTransport.cs +++ b/com.unity.netcode.gameobjects/Runtime/Transports/UTP/UnityTransport.cs @@ -1385,7 +1385,7 @@ public void CreateDriver(UnityTransport transport, out NetworkDriver driver, else { #if UNITY_WEBGL - Debug.LogWarning("WebSockets were used even though they're not selected in NetworkManager. You should adjust the setting, to silence this warning."); + Debug.LogWarning($"WebSockets were used even though they're not selected in NetworkManager. You should check {nameof(UseWebSockets)}', on the Unity Transport component, to silence this warning."); driver = NetworkDriver.Create(new WebSocketNetworkInterface(), m_NetworkSettings); #else driver = NetworkDriver.Create(new UDPNetworkInterface(), m_NetworkSettings); From 108a7475b59beceb50bf5e4e35ac5c0d95005e56 Mon Sep 17 00:00:00 2001 From: Jeffrey Rainy Date: Tue, 13 Sep 2022 18:02:54 -0400 Subject: [PATCH 6/7] Addressing PR review comments --- com.unity.netcode.gameobjects/CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/com.unity.netcode.gameobjects/CHANGELOG.md b/com.unity.netcode.gameobjects/CHANGELOG.md index e34095830a..ac31f8e07b 100644 --- a/com.unity.netcode.gameobjects/CHANGELOG.md +++ b/com.unity.netcode.gameobjects/CHANGELOG.md @@ -11,7 +11,7 @@ Additional documentation and release notes are available at [Multiplayer Documen ### Added -- WebSocket supports. When using UTP 2.0, a property `UseWebSockets` in the `UtpTransport` component of the `NetworkManager` allows picking WebSockets for communication. When building for WebGL, this selection happens automatically. +- WebSocket supports. When using UTP 2.0, a property `UseWebSockets` in the `UnityTransport` component of the `NetworkManager` allows picking WebSockets for communication. When building for WebGL, this selection happens automatically. ### Changed From 6a30626b6e852f199615f6b6ee2fb46ad25d6c5e Mon Sep 17 00:00:00 2001 From: Jeffrey Rainy Date: Tue, 13 Sep 2022 21:39:22 -0400 Subject: [PATCH 7/7] Update com.unity.netcode.gameobjects/CHANGELOG.md Co-authored-by: Fatih Mar --- com.unity.netcode.gameobjects/CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/com.unity.netcode.gameobjects/CHANGELOG.md b/com.unity.netcode.gameobjects/CHANGELOG.md index ac31f8e07b..a1019456ef 100644 --- a/com.unity.netcode.gameobjects/CHANGELOG.md +++ b/com.unity.netcode.gameobjects/CHANGELOG.md @@ -11,7 +11,7 @@ Additional documentation and release notes are available at [Multiplayer Documen ### Added -- WebSocket supports. When using UTP 2.0, a property `UseWebSockets` in the `UnityTransport` component of the `NetworkManager` allows picking WebSockets for communication. When building for WebGL, this selection happens automatically. +- Added WebSocket support when using UTP 2.0 with `UseWebSockets` property in the `UnityTransport` component of the `NetworkManager` allowing to pick WebSockets for communication. When building for WebGL, this selection happens automatically. (#2201) ### Changed