유니티(Unity) Relay Join Code에 대해서 (#Relay 5편)

Join Code는 게임세션에 플레이어들이 참가할 수 있게 해주는 코드입니다
플레이어들은 로비, Chat, 그 외 서비스를 통해 코드를 공유할 수 있습니다
 
Join Code는 대소문자를 구분하지 않아 단순하고 짧습니다
 
호스트 플레이어는 게임 세션을 생성한 후 Allocationos 서비스에서 친구에게 보낼 참고 코드를 요청하고, 친구들은 참가코드를 사용해서 호스트 플레이어가 있는 게임 세션에 참가할 수 있습니다
 

Join Code를 만들어내는 방법에는 Relay SDK 또는 Relay API를 이용해서 만들 수 있습니다

1) NGO를 사용한다면 transport 확인하고 NGO를 host 플레이어로 시작하면 됩니다

더보기
IEnumerator Example_ConfigureTransportAndStartNgoAsHost()
{
    var serverRelayUtilityTask = AllocateRelayServerAndGetJoinCode(m_MaxConnections);
    while (!serverRelayUtilityTask.IsCompleted)
    {
        yield return null;
    }
    if (serverRelayUtilityTask.IsFaulted)
    {
        Debug.LogError("Exception thrown when attempting to start Relay Server. Server not started. Exception: " + serverRelayUtilityTask.Exception.Message);
        yield break;
    }

    var relayServerData = serverRelayUtilityTask.Result;

    // Display the joinCode to the user.

    NetworkManager.Singleton.GetComponent<UnityTransport>().SetRelayServerData(relayServerData);
    NetworkManager.Singleton.StartHost();
    yield return null;
}

2) UTP를 사용한다면 allocation과 request join code를 만들면 됩니다

더보기
public async void OnAllocate()
{
    	Debug.Log("Host - Creating an allocation. Upon success, I have 10 seconds to BIND to the Relay server that I've allocated.");

    	// Determine region to use (user-selected or auto-select/QoS)
    	string region = GetRegionOrQosDefault();
    	Debug.Log($"The chosen region is: {region ?? autoSelectRegionName}");

    	// Set max connections. Can be up to 100, but note the more players connected, the higher the bandwidth/latency impact.
    	int maxConnections = 4;

    	// Important: After the allocation is created, you have ten seconds to BIND, else the allocation times out.
    	hostAllocation = await RelayService.Instance.CreateAllocationAsync(maxConnections, region);
    	Debug.Log($"Host Allocation ID: {hostAllocation.AllocationId}, region: {hostAllocation.Region}");

    	// Initialize NetworkConnection list for the server (Host).
    	// This list object manages the NetworkConnections which represent connected players.
    	serverConnections = new NativeList<NetworkConnection>(maxConnections, Allocator.Persistent);
}
public async void OnJoinCode()
{
    	Debug.Log("Host - Getting a join code for my allocation. I would share that join code with the other players so they can join my session.");

    	try
    	{
        	joinCode = await RelayService.Instance.GetJoinCodeAsync(hostAllocation.AllocationId);
        	Debug.Log("Host - Got join code: " + joinCode);
    	}
    	catch (RelayServiceException ex)
    	{
        	Debug.LogError(ex.Message + "\n" + ex.StackTrace);
    	}
}

 

Join Code를 이용해서 게임에 참가하는 방법도 Relay SDK 또는 Relay API를 이용해서 참여할 수 있습니다

1) NGO를 사용하는 경우에는 Join an Allocation(NGO)방식을 사용하면 됩니다

더보기
public static async Task<RelayServerData> JoinRelayServerFromJoinCode(string joinCode)
{
    JoinAllocation allocation;
    try
    {
        allocation = await RelayService.Instance.JoinAllocationAsync(joinCode);
    }
    catch
    {
        Debug.LogError("Relay create join code request failed");
        throw;
    }

    Debug.Log($"client: {allocation.ConnectionData[0]} {allocation.ConnectionData[1]}");
    Debug.Log($"host: {allocation.HostConnectionData[0]} {allocation.HostConnectionData[1]}");
    Debug.Log($"client: {allocation.AllocationId}");

    return new RelayServerData(allocation, "dtls");
}

2) UTP를 사용하는 경우에는 Join an Allocation(UTP)방식 사용하면 됩니다

더보기
public async void OnJoin()
{
    	// Input join code in the respective input field first.
    	if (String.IsNullOrEmpty(JoinCodeInput.text))
    	{
        	Debug.LogError("Please input a join code.");
        	return;
    	}

    	Debug.Log("Player - Joining host allocation using join code. Upon success, I have 10 seconds to BIND to the Relay server that I've allocated.");

    	try
    	{
        	playerAllocation = await RelayService.Instance.JoinAllocationAsync(JoinCodeInput.text);
        	Debug.Log("Player Allocation ID: " + playerAllocation.AllocationId);
    	}
    	catch (RelayServiceException ex)
    	{
        	Debug.LogError(ex.Message + "\n" + ex.StackTrace);
    	}
}