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

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

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

本主题介绍如何准备您的游戏客户端以便能够加入并参与 托管的游戏会话。

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

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

  • 使您的游戏客户端能够加入服务器上托管的游戏会话并交换消息。

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

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

  1. 将 AWS 软件开发工具包添加到您的客户端服务项目,初始化 AWS 客户端,并将其配置为使用您的实例集和/或队列中的托管资源。该 AWS提供多种语言;请参阅的 开发工具包。

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

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

在实时服务器上连接游戏

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

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

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

    注意

    如果要使用 TLS 证书连接到在安全实例集上运行的实时服务器,则必须指定安全的连接类型。

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

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

与启用了 TLS 证书生成的 Realtime 实例集一起使用时,将使用 TLS 证书自动对服务器进行身份验证。对正在传输的 TCP 和 UDP 通信进行加密,以提供传输层安全性。TCP 流量使用 TLS 1.2 进行加密,而 UDP 流量使用 DTLS 1.2 进行加密。

游戏客户端示例

基本 Realtime 客户端 (C #)

此示例说明与客户端软件开发工具包 (C#) 的基本游戏客户端集成。如下所示,该示例初始化客户端对象,设置事件处理程序并实施客户端回调,连接到服务器,发送消息,然后断开连接。

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); } } }