Skip to content

Commit 506368b

Browse files
committed
replacing polling for lobby updates with lobby events
1 parent bcac45b commit 506368b

File tree

4 files changed

+82
-47
lines changed

4 files changed

+82
-47
lines changed

Assets/Scripts/UnityServices/Lobbies/LobbyAPIInterface.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,5 +133,10 @@ public async void SendHeartbeatPing(string lobbyId)
133133
{
134134
await LobbyService.Instance.SendHeartbeatPingAsync(lobbyId);
135135
}
136+
137+
public async Task<ILobbyEvents> SubscribeToLobby(string lobbyId, LobbyEventCallbacks eventCallbacks)
138+
{
139+
return await LobbyService.Instance.SubscribeToLobbyEventsAsync(lobbyId, eventCallbacks);
140+
}
136141
}
137142
}

Assets/Scripts/UnityServices/Lobbies/LobbyServiceFacade.cs

Lines changed: 65 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ public class LobbyServiceFacade : IDisposable, IStartable
3737

3838
public Lobby CurrentUnityLobby { get; private set; }
3939

40+
LobbyEventCallbacks m_LobbyEventCallbacks;
41+
ILobbyEvents m_LobbyEvents;
42+
4043
bool m_IsTracking = false;
4144

4245
public void Start()
@@ -74,12 +77,10 @@ public void SetRemoteLobby(Lobby lobby)
7477

7578
public void BeginTracking()
7679
{
80+
SubscribeToJoinedLobby();
7781
if (!m_IsTracking)
7882
{
7983
m_IsTracking = true;
80-
// 2s update cadence is arbitrary and is here to demonstrate the fact that this update can be rather infrequent
81-
// the actual rate limits are tracked via the RateLimitCooldown objects defined above
82-
m_UpdateRunner.Subscribe(UpdateLobby, 2f);
8384
m_JoinedLobbyContentHeartbeat.BeginTracking();
8485
}
8586
}
@@ -109,59 +110,16 @@ public Task EndTracking()
109110
m_LocalLobby?.Reset(m_LocalUser);
110111
}
111112

113+
m_LobbyEvents?.UnsubscribeAsync();
112114
if (m_IsTracking)
113115
{
114-
m_UpdateRunner.Unsubscribe(UpdateLobby);
115116
m_IsTracking = false;
116117
m_HeartbeatTime = 0;
117118
m_JoinedLobbyContentHeartbeat.EndTracking();
118119
}
119-
120120
return task;
121121
}
122122

123-
async void UpdateLobby(float unused)
124-
{
125-
if (!m_RateLimitQuery.CanCall)
126-
{
127-
return;
128-
}
129-
130-
try
131-
{
132-
var lobby = await m_LobbyApiInterface.GetLobby(m_LocalLobby.LobbyID);
133-
134-
CurrentUnityLobby = lobby;
135-
m_LocalLobby.ApplyRemoteData(lobby);
136-
137-
// as client, check if host is still in lobby
138-
if (!m_LocalUser.IsHost)
139-
{
140-
foreach (var lobbyUser in m_LocalLobby.LobbyUsers)
141-
{
142-
if (lobbyUser.Value.IsHost)
143-
{
144-
return;
145-
}
146-
}
147-
m_UnityServiceErrorMessagePub.Publish(new UnityServiceErrorMessage("Host left the lobby", "Disconnecting.", UnityServiceErrorMessage.Service.Lobby));
148-
await EndTracking();
149-
// no need to disconnect Netcode, it should already be handled by Netcode's callback to disconnect
150-
}
151-
}
152-
catch (LobbyServiceException e)
153-
{
154-
if (e.Reason == LobbyExceptionReason.RateLimited)
155-
{
156-
m_RateLimitQuery.PutOnCooldown();
157-
}
158-
else if (e.Reason != LobbyExceptionReason.LobbyNotFound && !m_LocalUser.IsHost) // If Lobby is not found and if we are not the host, it has already been deleted. No need to publish the error here.
159-
{
160-
PublishError(e);
161-
}
162-
}
163-
}
164-
165123
/// <summary>
166124
/// Attempt to create a new lobby and then join it.
167125
/// </summary>
@@ -264,6 +222,66 @@ async void UpdateLobby(float unused)
264222
return (false, null);
265223
}
266224

225+
async void OnLobbyChanges(ILobbyChanges changes)
226+
{
227+
Debug.Log("Lobby updated");
228+
changes.ApplyToLobby(CurrentUnityLobby);
229+
m_LocalLobby.ApplyRemoteData(CurrentUnityLobby);
230+
231+
// as client, check if host is still in lobby
232+
if (!m_LocalUser.IsHost)
233+
{
234+
foreach (var lobbyUser in m_LocalLobby.LobbyUsers)
235+
{
236+
if (lobbyUser.Value.IsHost)
237+
{
238+
return;
239+
}
240+
}
241+
242+
m_UnityServiceErrorMessagePub.Publish(new UnityServiceErrorMessage("Host left the lobby", "Disconnecting.", UnityServiceErrorMessage.Service.Lobby));
243+
await EndTracking();
244+
245+
// no need to disconnect Netcode, it should already be handled by Netcode's callback to disconnect
246+
}
247+
}
248+
249+
void OnKickedFromLobby()
250+
{
251+
throw new NotImplementedException();
252+
}
253+
254+
void OnLobbyEventConnectionStateChanged(LobbyEventConnectionState lobbyEventConnectionState)
255+
{
256+
switch (lobbyEventConnectionState)
257+
{
258+
case LobbyEventConnectionState.Unknown:
259+
break;
260+
case LobbyEventConnectionState.Unsubscribed:
261+
break;
262+
case LobbyEventConnectionState.Subscribing:
263+
break;
264+
case LobbyEventConnectionState.Subscribed:
265+
break;
266+
case LobbyEventConnectionState.Unsynced:
267+
break;
268+
case LobbyEventConnectionState.Error:
269+
break;
270+
default:
271+
throw new ArgumentOutOfRangeException(nameof(lobbyEventConnectionState), lobbyEventConnectionState, null);
272+
}
273+
}
274+
275+
async void SubscribeToJoinedLobby()
276+
{
277+
var lobbyEventCallbacks = new LobbyEventCallbacks();
278+
lobbyEventCallbacks.LobbyChanged += OnLobbyChanges;
279+
lobbyEventCallbacks.KickedFromLobby += OnKickedFromLobby;
280+
lobbyEventCallbacks.LobbyEventConnectionStateChanged += OnLobbyEventConnectionStateChanged;
281+
m_LobbyEvents = await m_LobbyApiInterface.SubscribeToLobby(m_LocalLobby.LobbyID, m_LobbyEventCallbacks);
282+
m_JoinedLobbyContentHeartbeat.BeginTracking();
283+
}
284+
267285
/// <summary>
268286
/// Used for getting the list of all active lobbies, without needing full info for each.
269287
/// </summary>

Packages/manifest.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
"com.unity.services.authentication": "2.3.1",
1818
"com.unity.services.lobby": "1.0.3",
1919
"com.unity.services.relay": "1.0.3",
20+
"com.unity.services.wire": "1.0.0",
2021
"com.unity.test-framework": "1.1.31",
2122
"com.unity.textmeshpro": "3.0.6",
2223
"com.unity.timeline": "1.6.4",

Packages/packages-lock.json

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,17 @@
286286
},
287287
"url": "https://packages.unity.com"
288288
},
289+
"com.unity.services.wire": {
290+
"version": "1.0.0",
291+
"depth": 0,
292+
"source": "registry",
293+
"dependencies": {
294+
"com.unity.services.core": "1.2.0",
295+
"com.unity.nuget.newtonsoft-json": "3.0.1",
296+
"com.unity.services.authentication": "1.0.0-pre.37"
297+
},
298+
"url": "https://packages.unity.com"
299+
},
289300
"com.unity.settings-manager": {
290301
"version": "1.0.3",
291302
"depth": 1,

0 commit comments

Comments
 (0)