Skip to content

Commit ee5b2b4

Browse files
committed
Added connection to relay server on reconnect when using relay
1 parent 7a45aba commit ee5b2b4

File tree

1 file changed

+29
-21
lines changed

1 file changed

+29
-21
lines changed

Assets/BossRoom/Scripts/Shared/Net/ConnectionManagement/ClientGameNetPortal.cs

Lines changed: 29 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ public class ClientGameNetPortal : MonoBehaviour
1818
public static ClientGameNetPortal Instance;
1919
private GameNetPortal m_Portal;
2020
Coroutine m_TryToReconnectCoroutine;
21+
string m_JoinCode;
22+
OnlineMode m_OnlineMode;
2123

2224
/// <summary>
2325
/// 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()
173175
NetworkManager.Singleton.Shutdown();
174176
yield return new WaitWhile(() => NetworkManager.Singleton.ShutdownInProgress); // wait until NetworkManager completes shutting down
175177
Debug.Log($"Reconnecting attempt {nbTries + 1}/3...");
176-
ConnectClient();
178+
ConnectClient(null);
177179
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
178180
nbTries++;
179181
}
@@ -200,11 +202,11 @@ private IEnumerator TryToReconnect()
200202
/// 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
201203
/// 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).
202204
/// </remarks>
203-
/// <param name="portal"> </param>
204205
/// <param name="ipaddress">the IP address of the host to connect to. (currently IPV4 only)</param>
205206
/// <param name="port">The port of the host to connect to. </param>
206207
public void StartClient(string ipaddress, int port)
207208
{
209+
m_OnlineMode = OnlineMode.IpHost;
208210
var chosenTransport = NetworkManager.Singleton.gameObject.GetComponent<TransportPicker>().IpHostTransport;
209211
NetworkManager.Singleton.NetworkConfig.NetworkTransport = chosenTransport;
210212

@@ -222,36 +224,42 @@ public void StartClient(string ipaddress, int port)
222224
throw new ArgumentOutOfRangeException(nameof(chosenTransport));
223225
}
224226

225-
ConnectClient();
227+
ConnectClient(null);
226228
}
227229

228-
public async void StartClientUnityRelayModeAsync(string joinCode, Action<string> onFailure)
230+
public void StartClientUnityRelayModeAsync(string joinCode, Action<string> onFailure)
229231
{
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;
231235
NetworkManager.Singleton.NetworkConfig.NetworkTransport = utp;
232236

233237
Debug.Log($"Setting Unity Relay client with join code {joinCode}");
234238

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+
}
240241

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)
245245
{
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;
249251

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+
}
252262

253-
private void ConnectClient()
254-
{
255263
var clientGuid = ClientPrefs.GetGuid();
256264
var payload = JsonUtility.ToJson(new ConnectionPayload()
257265
{

0 commit comments

Comments
 (0)