@@ -37,6 +37,9 @@ public class LobbyServiceFacade : IDisposable, IStartable
37
37
38
38
public Lobby CurrentUnityLobby { get ; private set ; }
39
39
40
+ LobbyEventCallbacks m_LobbyEventCallbacks ;
41
+ ILobbyEvents m_LobbyEvents ;
42
+
40
43
bool m_IsTracking = false ;
41
44
42
45
public void Start ( )
@@ -74,12 +77,10 @@ public void SetRemoteLobby(Lobby lobby)
74
77
75
78
public void BeginTracking ( )
76
79
{
80
+ SubscribeToJoinedLobby ( ) ;
77
81
if ( ! m_IsTracking )
78
82
{
79
83
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 ) ;
83
84
m_JoinedLobbyContentHeartbeat . BeginTracking ( ) ;
84
85
}
85
86
}
@@ -109,59 +110,16 @@ public Task EndTracking()
109
110
m_LocalLobby ? . Reset ( m_LocalUser ) ;
110
111
}
111
112
113
+ m_LobbyEvents ? . UnsubscribeAsync ( ) ;
112
114
if ( m_IsTracking )
113
115
{
114
- m_UpdateRunner . Unsubscribe ( UpdateLobby ) ;
115
116
m_IsTracking = false ;
116
117
m_HeartbeatTime = 0 ;
117
118
m_JoinedLobbyContentHeartbeat . EndTracking ( ) ;
118
119
}
119
-
120
120
return task ;
121
121
}
122
122
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
-
165
123
/// <summary>
166
124
/// Attempt to create a new lobby and then join it.
167
125
/// </summary>
@@ -264,6 +222,66 @@ async void UpdateLobby(float unused)
264
222
return ( false , null ) ;
265
223
}
266
224
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
+
267
285
/// <summary>
268
286
/// Used for getting the list of all active lobbies, without needing full info for each.
269
287
/// </summary>
0 commit comments