Realtime 서버용 게임 클라이언트 통합 - 아마존 GameLift

기계 번역으로 제공되는 번역입니다. 제공된 번역과 원본 영어의 내용이 상충하는 경우에는 영어 버전이 우선합니다.

Realtime 서버용 게임 클라이언트 통합

이 주제에서는 Amazon GameLift 호스팅 게임 세션에 참가하고 참여할 수 있도록 게임 클라이언트를 준비하는 방법을 설명합니다.

게임 클라이언트를 준비하는 데 필요한 두 개의 작업 세트가 있습니다.

  • 기존 게임에 대한 정보를 획득하고 매치메이킹을 요청하며 새로운 게임 세션을 시작하고 플레이어를 위한 게임 세션을 예약하도록 게임 클라이언트를 설정합니다.

  • 게임 클라이언트를 활성화하여 Realtime 서버에 호스팅된 게임 세션에 참가하고 메시지를 교환합니다.

게임 세션 및 플레이어 세션 찾기 또는 생성

게임 세션을 찾거나 시작하고 FlexMatch 매치메이킹을 요청하며 게임에서 플레이어 세션을 생성하여 플레이어를 위한 공간을 예약하도록 게임 클라이언트를 설정합니다. 백엔드 서비스를 생성하고, 게임 클라이언트 실행에 의해 트리거될 때 이것을 사용하여 Amazon GameLift 서비스로 직접 요청하는 것이 가장 좋습니다. 그러면 백엔드 서비스가 관련 응답을 게임 클라이언트에 다시 릴레이합니다.

  1. AWS SDK를 게임 클라이언트에 추가하고, Amazon GameLift 클라이언트를 초기화하며, 플릿 및 대기열에서 호스팅 리소스를 사용하도록 해당 클라이언트를 구성합니다. AWS SDK는 여러 언어로 제공됩니다(Amazon GameLift SDK 사용자 지정 클라이언트 서비스의 경우 참조).

  2. GameLift 기능을 백엔드 서비스에 추가합니다. 자세한 지침은 GameLift Amazon을 게임 클라이언트에 추가FlexMatch 매치메이킹 추가를 참조하세요. 게임 세션 배치를 사용하여 새 게임 세션을 생성하는 것이 모범 사례입니다. 이 방법을 사용하면 새 게임 세션을 신속하고 지능적으로 배치하는 GameLift의 능력을 최대로 활용하는 것은 물론 플레이어 지연 시간 데이터를 사용하여 게임 지연 시간을 최소화할 수 있습니다. 적어도 백엔드 서비스에서 새 게임 세션을 요청하고 그에 따라 게임 세션 데이터를 처리할 수 있어야 합니다. 그리고 기존 게임 세션에 관한 정보를 검색하여 가져오는 기능을 추가하고, 플레이어 세션을 요청하여 기존 게임 세션의 플레이어 슬롯을 효과적으로 유지할 수 있어야 합니다.

  3. 연결 정보를 다시 게임 클라이언트에 전달합니다. 백엔드 서비스는 Amazon GameLift 서비스에 대한 요청의 응답에서 게임 세션 및 플레이어 세션 객체를 수신합니다. 이러한 객체에는 게임 클라이언트가 Realtime Server에서 실행 중인 게임 세션에 연결하는 데 필요한 정보, 특히 연결 세부 정보(IP 주소 및 포트) 및 플레이어 세션 ID가 포함됩니다.

Realtime 서버의 게임에 연결

게임 클라이언트를 활성화하여 Realtime 서버에 호스팅 게임 세션과 직접 연결하고 서버 및 다른 플레이어와 메시지를 교환합니다.

  1. Realtime Client SDK를 가져오고 빌드하며 게임 클라이언트 프로젝트에 추가합니다. SDK 요구 사항에 대한 자세한 내용과 클라이언트 라이브러리를 빌드하는 방법에 대한 지침은 README 파일을 참조하십시오.

  2. 사용할 클라이언트/서버 연결 유형을 지정하는 클라이언트 구성을 사용하여 Client()를 호출합니다.

    참고

    TLS 인증서를 사용하여 보안 플릿에서 실행되는 Realtime 서버에 연결하는 경우 보안 연결 유형을 지정해야 합니다.

  3. 다음 기능을 게임 클라이언트에 추가합니다. 자세한 내용은 Realtime Servers Client API(C#) 참조 섹션을 참조하세요.

  4. 필요에 따라 클라이언트 콜백에 대한 이벤트 핸들러를 설정합니다. Realtime Servers Client API(C#) 참조: 비동기 콜백 섹션을 참조하세요.

TLS 인증서 생성이 활성화된 Realtime 플릿으로 작업하는 경우 서버는 TLS 인증서를 사용하여 자동으로 인증됩니다. TCP 및 UDP 트래픽은 전송 중에 암호화되어 전송 계층 보안을 제공합니다. TCP 트래픽은 TLS 1.2를 사용하여 암호화되고, UDP 트래픽은 DTLS 1.2를 사용하여 암호화됩니다.

게임 클라이언트 예제

Basic Realtime Client(C#)

이 예제에서는 Realtime Client SDK(C#)와 기본 게임 클라이언트 통합을 보여 줍니다. 그림과 같이, 이 예제에서는 Realtime 클라이언트 객체를 초기화하고 이벤트 핸들러를 설정한 다음 클라이언트 측 콜백을 구현하고, Realtime 서버에 연결하고 메시지를 전송한 다음 연결을 해제합니다.

using System; using System.Text; using Aws.GameLift.Realtime; using Aws.GameLift.Realtime.Event; using Aws.GameLift.Realtime.Types; namespace Example { /** * An example client that wraps the GameLift Realtime client SDK * * You can redirect logging from the SDK by setting up the LogHandler as such: * ClientLogger.LogHandler = (x) => Console.WriteLine(x); * */ class RealTimeClient { public Aws.GameLift.Realtime.Client Client { get; private set; } // An opcode defined by client and your server script that represents a custom message type private const int MY_TEST_OP_CODE = 10; /// Initialize a client for GameLift Realtime and connect to a player session. /// <param name="endpoint">The DNS name that is assigned to Realtime server</param> /// <param name="remoteTcpPort">A TCP port for the Realtime server</param> /// <param name="listeningUdpPort">A local port for listening to UDP traffic</param> /// <param name="connectionType">Type of connection to establish between client and the Realtime server</param> /// <param name="playerSessionId">The player session ID that is assigned to the game client for a game session </param> /// <param name="connectionPayload">Developer-defined data to be used during client connection, such as for player authentication</param> public RealTimeClient(string endpoint, int remoteTcpPort, int listeningUdpPort, ConnectionType connectionType, string playerSessionId, byte[] connectionPayload) { // Create a client configuration to specify a secure or unsecure connection type // Best practice is to set up a secure connection using the connection type RT_OVER_WSS_DTLS_TLS12. ClientConfiguration clientConfiguration = new ClientConfiguration() { // C# notation to set the field ConnectionType in the new instance of ClientConfiguration ConnectionType = connectionType }; // Create a Realtime client with the client configuration Client = new Client(clientConfiguration); // Initialize event handlers for the Realtime client Client.ConnectionOpen += OnOpenEvent; Client.ConnectionClose += OnCloseEvent; Client.GroupMembershipUpdated += OnGroupMembershipUpdate; Client.DataReceived += OnDataReceived; // Create a connection token to authenticate the client with the Realtime server // Player session IDs can be retrieved using AWS SDK for GameLift ConnectionToken connectionToken = new ConnectionToken(playerSessionId, connectionPayload); // Initiate a connection with the Realtime server with the given connection information Client.Connect(endpoint, remoteTcpPort, listeningUdpPort, connectionToken); } public void Disconnect() { if (Client.Connected) { Client.Disconnect(); } } public bool IsConnected() { return Client.Connected; } /// <summary> /// Example of sending to a custom message to the server. /// /// Server could be replaced by known peer Id etc. /// </summary> /// <param name="intent">Choice of delivery intent i.e. Reliable, Fast etc. </param> /// <param name="payload">Custom payload to send with message</param> public void SendMessage(DeliveryIntent intent, string payload) { Client.SendMessage(Client.NewMessage(MY_TEST_OP_CODE) .WithDeliveryIntent(intent) .WithTargetPlayer(Constants.PLAYER_ID_SERVER) .WithPayload(StringToBytes(payload))); } /** * Handle connection open events */ public void OnOpenEvent(object sender, EventArgs e) { } /** * Handle connection close events */ public void OnCloseEvent(object sender, EventArgs e) { } /** * Handle Group membership update events */ public void OnGroupMembershipUpdate(object sender, GroupMembershipEventArgs e) { } /** * Handle data received from the Realtime server */ public virtual void OnDataReceived(object sender, DataReceivedEventArgs e) { switch (e.OpCode) { // handle message based on OpCode default: break; } } /** * Helper method to simplify task of sending/receiving payloads. */ public static byte[] StringToBytes(string str) { return Encoding.UTF8.GetBytes(str); } /** * Helper method to simplify task of sending/receiving payloads. */ public static string BytesToString(byte[] bytes) { return Encoding.UTF8.GetString(bytes); } } }