|
|
|
|
|
|
using System; |
|
|
|
using Unity.Services.Relay; |
|
|
|
using Unity.Services.Relay.Allocations; |
|
|
|
using RelayService = Unity.Services.Relay.Relay; |
|
|
|
|
|
|
|
namespace LobbyRelaySample.Relay |
|
|
|
{ |
|
|
|
|
|
|
/// </summary>
|
|
|
|
public static void AllocateAsync(int maxConnections, Action<Allocation> onComplete) |
|
|
|
{ |
|
|
|
CreateAllocationRequest createAllocationRequest = new CreateAllocationRequest(new AllocationRequest(maxConnections)); |
|
|
|
var task = RelayService.AllocationsApiClient.CreateAllocationAsync(createAllocationRequest); |
|
|
|
var task = RelayService.Instance.CreateAllocationAsync(maxConnections); |
|
|
|
void OnResponse(Response<AllocateResponseBody> response) |
|
|
|
void OnResponse(Allocation response) |
|
|
|
else if (response.Status >= 200 && response.Status < 300) |
|
|
|
onComplete?.Invoke(response.Result.Data.Allocation); |
|
|
|
Debug.LogError($"Allocation returned a non Success code: {response.Status}"); |
|
|
|
onComplete?.Invoke(response); |
|
|
|
}; |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
/// </summary>
|
|
|
|
public static void GetJoinCodeAsync(Guid hostAllocationId, Action<string> onComplete) |
|
|
|
{ |
|
|
|
GetJoinCodeAsync(hostAllocationId, a => |
|
|
|
var task = RelayService.Instance.GetJoinCodeAsync(hostAllocationId); |
|
|
|
AsyncRequestRelay.Instance.DoRequest(task, OnResponse); |
|
|
|
|
|
|
|
void OnResponse(string response) |
|
|
|
if (a.Status >= 200 && a.Status < 300) |
|
|
|
onComplete.Invoke(a.Result.Data.JoinCode); |
|
|
|
if (response == null) |
|
|
|
Debug.LogError("Could not retrieve a Relay join code."); |
|
|
|
{ |
|
|
|
Debug.LogError($"Relay GetJoinCodeAsync returned a non-success code: {a.Status}"); |
|
|
|
} |
|
|
|
}); |
|
|
|
} |
|
|
|
private static void GetJoinCodeAsync(Guid hostAllocationId, Action<Response<JoinCodeResponseBody>> onComplete) |
|
|
|
{ |
|
|
|
CreateJoincodeRequest joinCodeRequest = new CreateJoincodeRequest(new JoinCodeRequest(hostAllocationId)); |
|
|
|
var task = RelayService.AllocationsApiClient.CreateJoincodeAsync(joinCodeRequest); |
|
|
|
AsyncRequestRelay.Instance.DoRequest(task, onComplete); |
|
|
|
onComplete?.Invoke(response); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
|
{ |
|
|
|
JoinAsync(joinCode, a => |
|
|
|
var task = RelayService.Instance.JoinAllocationAsync(joinCode); |
|
|
|
AsyncRequestRelay.Instance.DoRequest(task, OnResponse); |
|
|
|
|
|
|
|
void OnResponse(JoinAllocation response) |
|
|
|
if (a.Status >= 200 && a.Status < 300) |
|
|
|
onComplete.Invoke(a.Result.Data.Allocation); |
|
|
|
if (response == null) |
|
|
|
Debug.LogError("Could not join async with Relay join code " + joinCode); |
|
|
|
{ |
|
|
|
Debug.LogError($"Join Call returned a non Success code: {a.Status}"); |
|
|
|
} |
|
|
|
}); |
|
|
|
} |
|
|
|
|
|
|
|
public static void JoinAsync(string joinCode, Action<Response<JoinResponseBody>> onComplete) |
|
|
|
{ |
|
|
|
JoinRelayRequest joinRequest = new JoinRelayRequest(new JoinRequest(joinCode)); |
|
|
|
var task = RelayService.AllocationsApiClient.JoinRelayAsync(joinRequest); |
|
|
|
AsyncRequestRelay.Instance.DoRequest(task, onComplete); |
|
|
|
onComplete?.Invoke(response); |
|
|
|
}; |
|
|
|
} |
|
|
|
} |
|
|
|
} |