diff --git a/com.unity.netcode.gameobjects/CHANGELOG.md b/com.unity.netcode.gameobjects/CHANGELOG.md index e4ba64a1f0..a1019456ef 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 + +- 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 - 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 d5de9abfd9..49d81e50c3 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; @@ -1365,7 +1377,23 @@ public void CreateDriver(UnityTransport transport, out NetworkDriver driver, #endif heartbeatTimeoutMS: transport.m_HeartbeatTimeoutMS); +#if UTP_TRANSPORT_2_0_ABOVE + if (m_UseWebSockets) + { + driver = NetworkDriver.Create(new WebSocketNetworkInterface(), m_NetworkSettings); + } + else + { +#if UNITY_WEBGL + 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); +#endif + } +#else driver = NetworkDriver.Create(m_NetworkSettings); +#endif #if MULTIPLAYER_TOOLS_1_0_0_PRE_7 #if UTP_TRANSPORT_2_0_ABOVE