Skip to content

Commit 1046647

Browse files
jeffreyrainy0xFA11
andauthored
chore: expose websockets (#2201)
* chore: websockets. Minimalist UI to use WebSockets * fix: adjusting #ifdef on transport version to allow building non-webgl platforms with UTP 2.0 * fix: addressing build issue where WebGL platform requires different name for UTP symbols * fix: emitting a warning and switching to WebSockets if uers don't, on WebGL platform * Addressing PR review comments * Addressing PR review comments * Update com.unity.netcode.gameobjects/CHANGELOG.md Co-authored-by: Fatih Mar <mfatihmar@gmail.com> Co-authored-by: Fatih Mar <mfatihmar@gmail.com>
1 parent f34ded4 commit 1046647

2 files changed

Lines changed: 32 additions & 0 deletions

File tree

com.unity.netcode.gameobjects/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ Additional documentation and release notes are available at [Multiplayer Documen
99

1010
## [Unreleased]
1111

12+
### Added
13+
14+
- 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)
15+
1216
### Changed
1317

1418
- 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)

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

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,18 @@ private enum State
150150
[SerializeField]
151151
private ProtocolType m_ProtocolType;
152152

153+
#if UTP_TRANSPORT_2_0_ABOVE
154+
[Tooltip("Whether or not to use WebSockets as Network Interface")]
155+
[SerializeField]
156+
private bool m_UseWebSockets = false;
157+
158+
public bool UseWebSockets
159+
{
160+
set => m_UseWebSockets = value;
161+
get => m_UseWebSockets;
162+
}
163+
#endif
164+
153165
[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.")]
154166
[SerializeField]
155167
private int m_MaxPacketQueueSize = InitialMaxPacketQueueSize;
@@ -1365,7 +1377,23 @@ public void CreateDriver(UnityTransport transport, out NetworkDriver driver,
13651377
#endif
13661378
heartbeatTimeoutMS: transport.m_HeartbeatTimeoutMS);
13671379

1380+
#if UTP_TRANSPORT_2_0_ABOVE
1381+
if (m_UseWebSockets)
1382+
{
1383+
driver = NetworkDriver.Create(new WebSocketNetworkInterface(), m_NetworkSettings);
1384+
}
1385+
else
1386+
{
1387+
#if UNITY_WEBGL
1388+
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.");
1389+
driver = NetworkDriver.Create(new WebSocketNetworkInterface(), m_NetworkSettings);
1390+
#else
1391+
driver = NetworkDriver.Create(new UDPNetworkInterface(), m_NetworkSettings);
1392+
#endif
1393+
}
1394+
#else
13681395
driver = NetworkDriver.Create(m_NetworkSettings);
1396+
#endif
13691397

13701398
#if MULTIPLAYER_TOOLS_1_0_0_PRE_7
13711399
#if UTP_TRANSPORT_2_0_ABOVE

0 commit comments

Comments
 (0)