為即時伺服器整合遊戲用戶端 - Amazon GameLift

本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。

為即時伺服器整合遊戲用戶端

本主題說明如何準備您的遊戲用戶端,以便能夠加入並參與 Amazon GameLift 託管的遊戲工作階段。

要準備您的遊戲用戶端需要兩組任務:

  • 設定您的遊戲用戶端,以取得有關現有遊戲的資訊、請求配對、啟動新的遊戲工作階段,以及為玩家預留遊戲工作階段位置。

  • 使您的遊戲客戶端能夠加入託管在實時服務器上的遊戲會話並交換消息。

查找或創建遊戲會話和玩家會話

設定遊戲用戶端,以尋找或啟動遊戲工作階段、請求 FlexMatch 配對,以及透過建立玩家工作階段,來為遊戲中的玩家預留空間。最佳實務是建立後端服務,並在遊戲用戶端動作觸發時,使用該GameLift服務向 Amazon 服務發出直接請求。後端服務接著會將相關回應轉送回遊戲用戶端。

  1. 將 AWS SDK 新增至您的遊戲用戶端、初始化 Amazon 用GameLift戶端,然後將其設定為使用叢集和佇列中的託管資源。此開發AWS套件提供多種語言版本;請參閱 Amazon 開GameLift發套對於遊戲客戶端服務件。

  2. 將GameLift功能添加到您的後端服務。如需更詳細的指示,請參閱添加 Amazon GameLift 到您的遊戲客戶端新增FlexMatch配對。最佳實務是使用遊戲工作階段置放來建立新的遊戲工作階段。此方法可讓您充分利用快速智慧地放置新遊戲工作階段的能力,以及使用玩家延遲資料將遊戲延遲降至最低。GameLift您的後端服務至少必須能夠要求新的遊戲工作階段,並處理遊戲工作階段資料以作回應。您可能也會想要新增功能,來搜尋和取得現有遊戲工作階段的資訊,並請求玩家工作階段,有效地在現有的遊戲工作階段中預留玩家位置。

  3. 將連線資訊傳回遊戲用戶端。後端服務會接收遊戲工作階段和玩家工作階段物件,以回應 Amazon GameLift 服務的請求。這些物件包含資訊,特別是遊戲用戶端連線到在 Realtime 伺服器上所執行遊戲工作階段所需的連線詳細資訊 (IP 地址和連接埠) 以及玩家工作階段 ID。

連接到實時服務器上的遊戲

讓您的遊戲用戶端直接與即時伺服器上的代管遊戲工作階段連線,並與伺服器和其他玩家交換訊息。

  1. 獲取實時客戶端 SDK,構建它,並將其添加到您的遊戲客戶端項目中。如需 SDK 需求以及如何建置用戶端程式庫的詳細資訊,請參閱 README 檔案。

  2. 使用指定用戶端/伺服器連線類型的用戶端組態來呼叫 Client()

    注意

    如果您連線到在使用 TLS 憑證之安全機群上執行的 Realtime 伺服器,您必須指定安全連線類型。

  3. 將以下功能新增到您的遊戲用戶端。如需詳細資訊,請參閱即時伺服器用戶端 API (C#) 參考

  4. 視需要為用戶端回呼設定事件處理器。請參閱 實時服務器客戶端 API(C#)參考:異步回調

使用已啟用 TLS 憑證產生的 Realtime 機群時,系統會自動使用 TLS 憑證來驗證伺服器。TCP 和 UDP 流量會在傳送中加密,以提供傳輸層安全性。TCP 流量會使用 TLS 1.2 加密,而 UDP 流量則使用 DTLS 1.2 加密。

遊戲用戶端範例

基本實時客戶端(C#)

此範例說明了與即時用戶端 SDK (C#) 的基本遊戲用戶端整合。如圖所示,該示例初始化 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); } } }