为实时服务器集成游戏客户端 - 亚马逊 GameLift
Amazon Web Services 文档中描述的 Amazon Web Services 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅 中国的 Amazon Web Services 服务入门 (PDF)

本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。

为实时服务器集成游戏客户端

本主题介绍如何让您的游戏客户端做好准备,以便能够加入和参与亚马逊GameLift托管的游戏会话。

准备游戏客户端需要两组任务:

  • 设置您的游戏客户端以获取有关现有游戏的信息、请求对战、启动新游戏会话以及为玩家预留游戏会话位置。

  • 允许您的游戏客户端加入在实时服务器上托管的游戏会话并交换消息。

查找或创建游戏会话和玩家会话

设置您的游戏客户端以查找或启动游戏会话、请求 FlexMatch 对战以及通过创建玩家会话为游戏中的玩家预留空间。最佳做法是,创建后端服务,并在游戏客户端操作触发时使用它向亚马逊GameLift服务发出直接请求。然后,后端服务将相关响应中继回游戏客户端。

  1. 将 Amazon SDK 添加到您的游戏客户端,初始化 Amazon GameLift 客户端,然后将其配置为使用队列和队列中的主机资源。该Amazon软件开发工具包有多种语言版本;请参阅亚马逊 GameLift SDK。用于自定义客户端服务

  2. 为您的后端服务添加GameLift功能。有关更详细的说明,请参阅将亚马逊GameLift添加到您的游戏客户端添加FlexMatch配对。最佳做法是使用游戏会话放置功能来创建新的游戏会话。此方法使您可以充分利用快速智能地放置新游戏会话的能力,并使用玩家延迟数据来最大限度地减少游戏延迟。GameLift至少,您的后端服务必须能够请求新的游戏会话并处理游戏会话数据作为响应。您还可能需要添加功能,以搜索并获取现有游戏会话的信息和请求玩家会话,这会有效地保留现有游戏会话中的玩家位置。

  3. 将连接信息传回游戏客户端。后端服务接收游戏会话和玩家会话对象,以响应对 Amazon GameLift 服务的请求。这些对象包含游戏客户端直接连接到运行在 Realtime Server 上的游戏会话所需的信息,尤其是连接详细信息(IP 地址和端口)以及玩家会话 ID。

在实时服务器上连接到游戏

使您的游戏客户端能够直接连接实时服务器上的托管游戏会话,并与服务器和其他玩家交换消息。

  1. 获取 Realtime Client SDK,进行构建,然后将其添加到您的游戏客户端项目中。有关软件开发工具包要求的更多信息以及如何生成客户端库的说明,请参阅自述文件。

  2. 使用指定要使用的客户端/服务器连接类型的客户端配置调用 Client()

  3. 将以下功能添加到您的游戏客户端。有关更多信息,请参阅实时服务器客户端 API (C#) 参考

  4. 根据需要设置客户端回调的事件处理程序。请参阅实时服务器客户端 API (C#) 参考:异步回调

游戏客户端示例

基本实时客户端 (C#)

此示例说明了游戏客户端与实时客户端 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 Amazon 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); } } }