using System ;
using System.Threading.Tasks ;
using Unity.Services.Relay.Allocations ;
namespace LobbyRelaySample.Relay
namespace LobbyRelaySample.relay
{
/// <summary>
/// Wrapper for all the interaction with the Relay API.
/// <summary>
/// A Relay Allocation represents a "server" for a new host.
/// </summary>
public static void AllocateAsync ( int maxConnections , Action < Allocation > onComplete )
public static async void AllocateAsync ( int maxConnections , Action < Allocation > onComplete )
CreateAllocationRequest createAllocationRequest = new CreateAllocationRequest ( new AllocationRequest ( maxConnections ) ) ;
var task = RelayService . AllocationsApiClient . CreateAllocationAsync ( createAllocationRequest ) ;
AsyncRequest . DoRequest ( task , OnResponse ) ;
try
{
Allocation allocation = await Relay . Instance . CreateAllocationAsync ( maxConnections ) ;
void OnResponse ( Response < AllocateResponseBody > response )
onComplete . Invoke ( allocation ) ;
}
catch ( RelayServiceException ex )
if ( response = = null )
Debug . LogError ( "Relay returned a null Allocation. This might occur if the Relay service has an outage, if your cloud project ID isn't linked, or if your Relay package version is outdated." ) ;
else if ( response . Status > = 2 0 0 & & response . Status < 3 0 0 )
onComplete ? . Invoke ( response . Result . Data . Allocation ) ;
else
Debug . LogError ( $"Allocation returned a non Success code: {response.Status}" ) ;
} ;
Debug . LogError ( ex . Message + "\n" + ex . StackTrace ) ;
throw ;
}
}
/// <summary>
public static void GetJoinCodeAsync ( Guid hostAllocationId , Action < string > onComplete )
public static async void GetJoinCodeAsync ( Guid hostAllocationId , Action < string > onComplete )
GetJoinCodeAsync ( hostAllocationId , a = >
try
{
string joinCode = await Relay . Instance . GetJoinCodeAsync ( hostAllocationId ) ;
onComplete . Invoke ( joinCode ) ;
}
catch ( RelayServiceException ex )
if ( a . Status > = 2 0 0 & & a . Status < 3 0 0 )
onComplete . Invoke ( a . Result . Data . JoinCode ) ;
else
{
Debug . LogError ( $"Join Code Get 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 ) ;
AsyncRequest . DoRequest ( task , onComplete ) ;
Debug . LogError ( ex . Message + "\n" + ex . StackTrace ) ;
throw ;
}
public static void JoinAsync ( string joinCode , Action < JoinAllocation > onComplete )
public static async void JoinAsync ( string joinCode , Action < JoinAllocation > onComplete )
JoinAsync ( joinCode , a = >
try
if ( a . Status > = 2 0 0 & & a . Status < 3 0 0 )
onComplete . Invoke ( a . Result . Data . Allocation ) ;
else
{
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 ) ;
AsyncRequest . DoRequest ( task , onComplete ) ;
JoinAllocation joinAllocation = await Relay . Instance . JoinAllocationAsync ( joinCode ) ;
onComplete . Invoke ( joinAllocation ) ;
}
catch ( RelayServiceException ex )
{
Debug . LogError ( ex . Message + "\n" + ex . StackTrace ) ;
throw ;
}
}
}
}