@@ -18,6 +18,8 @@ public class ClientGameNetPortal : MonoBehaviour
18
18
public static ClientGameNetPortal Instance ;
19
19
private GameNetPortal m_Portal ;
20
20
Coroutine m_TryToReconnectCoroutine ;
21
+ string m_JoinCode ;
22
+ OnlineMode m_OnlineMode ;
21
23
22
24
/// <summary>
23
25
/// If a disconnect occurred this will be populated with any contextual information that was available to explain why.
@@ -173,7 +175,7 @@ private IEnumerator TryToReconnect()
173
175
NetworkManager . Singleton . Shutdown ( ) ;
174
176
yield return new WaitWhile ( ( ) => NetworkManager . Singleton . ShutdownInProgress ) ; // wait until NetworkManager completes shutting down
175
177
Debug . Log ( $ "Reconnecting attempt { nbTries + 1 } /3...") ;
176
- ConnectClient ( ) ;
178
+ ConnectClient ( null ) ;
177
179
yield return new WaitForSeconds ( 1.1f * k_TimeoutDuration ) ; // wait a bit longer than the timeout duration to make sure we have enough time to stop this coroutine if successful
178
180
nbTries ++ ;
179
181
}
@@ -200,11 +202,11 @@ private IEnumerator TryToReconnect()
200
202
/// This method must be static because, when it is invoked, the client still doesn't know it's a client yet, and in particular, GameNetPortal hasn't
201
203
/// yet initialized its client and server GNP-Logic objects yet (which it does in OnNetworkSpawn, based on the role that the current player is performing).
202
204
/// </remarks>
203
- /// <param name="portal"> </param>
204
205
/// <param name="ipaddress">the IP address of the host to connect to. (currently IPV4 only)</param>
205
206
/// <param name="port">The port of the host to connect to. </param>
206
207
public void StartClient ( string ipaddress , int port )
207
208
{
209
+ m_OnlineMode = OnlineMode . IpHost ;
208
210
var chosenTransport = NetworkManager . Singleton . gameObject . GetComponent < TransportPicker > ( ) . IpHostTransport ;
209
211
NetworkManager . Singleton . NetworkConfig . NetworkTransport = chosenTransport ;
210
212
@@ -222,36 +224,42 @@ public void StartClient(string ipaddress, int port)
222
224
throw new ArgumentOutOfRangeException ( nameof ( chosenTransport ) ) ;
223
225
}
224
226
225
- ConnectClient ( ) ;
227
+ ConnectClient ( null ) ;
226
228
}
227
229
228
- public async void StartClientUnityRelayModeAsync ( string joinCode , Action < string > onFailure )
230
+ public void StartClientUnityRelayModeAsync ( string joinCode , Action < string > onFailure )
229
231
{
230
- var utp = ( UnityTransport ) NetworkManager . Singleton . gameObject . GetComponent < TransportPicker > ( ) . UnityRelayTransport ;
232
+ m_OnlineMode = OnlineMode . UnityRelay ;
233
+ m_JoinCode = joinCode ;
234
+ var utp = NetworkManager . Singleton . gameObject . GetComponent < TransportPicker > ( ) . UnityRelayTransport ;
231
235
NetworkManager . Singleton . NetworkConfig . NetworkTransport = utp ;
232
236
233
237
Debug . Log ( $ "Setting Unity Relay client with join code { joinCode } ") ;
234
238
235
- try
236
- {
237
- var clientRelayUtilityTask = UnityRelayUtilities . JoinRelayServerFromJoinCode ( joinCode ) ;
238
- await clientRelayUtilityTask ;
239
- var ( ipv4Address , port , allocationIdBytes , connectionData , hostConnectionData , key ) = clientRelayUtilityTask . Result ;
239
+ ConnectClient ( onFailure ) ;
240
+ }
240
241
241
- m_LobbyServiceFacade . UpdatePlayerRelayInfoAsync ( allocationIdBytes . ToString ( ) , joinCode , null , null ) ;
242
- utp . SetRelayServerData ( ipv4Address , port , allocationIdBytes , key , connectionData , hostConnectionData ) ;
243
- }
244
- catch ( Exception e )
242
+ async void ConnectClient ( Action < string > onFailure )
243
+ {
244
+ if ( m_OnlineMode == OnlineMode . UnityRelay )
245
245
{
246
- onFailure ? . Invoke ( e . Message ) ;
247
- return ; //not re-throwing, but still not allowing to connect
248
- }
246
+ try
247
+ {
248
+ var clientRelayUtilityTask = UnityRelayUtilities . JoinRelayServerFromJoinCode ( m_JoinCode ) ;
249
+ await clientRelayUtilityTask ;
250
+ var ( ipv4Address , port , allocationIdBytes , connectionData , hostConnectionData , key ) = clientRelayUtilityTask . Result ;
249
251
250
- ConnectClient ( ) ;
251
- }
252
+ m_LobbyServiceFacade . UpdatePlayerRelayInfoAsync ( allocationIdBytes . ToString ( ) , m_JoinCode , null , null ) ;
253
+ var utp = ( UnityTransport ) NetworkManager . Singleton . NetworkConfig . NetworkTransport ;
254
+ utp . SetRelayServerData ( ipv4Address , port , allocationIdBytes , key , connectionData , hostConnectionData ) ;
255
+ }
256
+ catch ( Exception e )
257
+ {
258
+ onFailure ? . Invoke ( e . Message ) ;
259
+ return ; //not re-throwing, but still not allowing to connect
260
+ }
261
+ }
252
262
253
- private void ConnectClient ( )
254
- {
255
263
var clientGuid = ClientPrefs . GetGuid ( ) ;
256
264
var payload = JsonUtility . ToJson ( new ConnectionPayload ( )
257
265
{
0 commit comments