本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。
Amazon GameLift ServersRealtime接口
当Amazon GameLift ServersRealtime脚本初始化时,将返回Realtime服务器接口。本主题介绍了可通过接口使用的属性和方法。了解有关编写 Realtime 脚本的更多信息,并在创建Realtime脚本中查看详细的脚本示例。
该 Realtime 接口提供对以下对象的访问:
-
会话
-
player
-
gameMessage
-
配置
Realtime 会话对象
使用这些方法来访问与服务器相关的信息和执行与服务器相关的操作。
getPlayers()
检索当前已连接到游戏会话 IDs 的玩家的同行列表。返回一组玩家对象。
语法
rtSession.getPlayers()
broadcastGroupMembership更新 ()
触发向玩家组传输更新后的组成员资格列表。指定要广播的成员资格(groupIdTo广播)和接收更新的群组(targetGroupId)。组 IDs 必须为正整数或 “-1” 表示所有组。Amazon GameLift ServersRealtime脚本示例有关用户定义组的示例,请参见 IDs。
语法
rtSession.broadcastGroupMembershipUpdate(groupIdToBroadcast, targetGroupId)
getServerId()
检索服务器的唯一对等连接 ID 标识符,这用于将消息路由到服务器。
语法
rtSession.getServerId()
getAllPlayersGroupId()
检索默认组的组 ID,该组包含当前连接到游戏会话的所有玩家。
语法
rtSession.getAllPlayersGroupId()
ProcessEnding()
触发 Realtime Server 来终止游戏服务器。必须从 Realtime 脚本调用此函数以从游戏会话中完全退出。
语法
rtSession.processEnding()
getGameSession身份证 ()
检索当前正在运行的游戏会话的唯一 ID。
语法
rtSession.getGameSessionId()
getLogger()
检索用于日志记录的接口。使用此项来记录将捕获到游戏会话日志中的语句。日志记录程序支持使用“info”、“warn”和“error”语句。例如:logger.info("<string>")
。
语法
rtSession.getLogger()
sendMessage()
发送消息,该消息使用 newTextGameMessage
或 newBinaryGameMessage
创建,通过 UDP 通道从 Realtime Server 发送到玩家接收方。使用玩家的对等连接 ID 确认接收方。
语法
rtSession.sendMessage(gameMessage, targetPlayer)
sendGroupMessage()
发送消息,该消息使用 newTextGameMessage
或 newBinaryGameMessage
创建,通过 UDP 通道从 Realtime Server 发送到玩家组中的所有玩家。组 IDs 必须为正整数或 “-1” 表示所有组。Amazon GameLift ServersRealtime脚本示例有关用户定义组的示例,请参见 IDs。
语法
rtSession.sendGroupMessage(gameMessage, targetGroup)
sendReliableMessage()
发送消息,该消息使用 newTextGameMessage
或 newBinaryGameMessage
创建,通过 TCP 通道从 Realtime Server 发送到玩家接收方。使用玩家的对等连接 ID 确认接收方。
语法
rtSession.sendReliableMessage(gameMessage, targetPlayer)
sendReliableGroup消息 ()
发送消息,该消息使用 newTextGameMessage
或 newBinaryGameMessage
创建,通过 TCP 通道从 Realtime Server 发送到玩家组中的所有玩家。组 IDs ,必须是正整数或 “-1” 才能表示所有组。Amazon GameLift ServersRealtime脚本示例有关用户定义组的示例,请参见 IDs。
语法
rtSession.sendReliableGroupMessage(gameMessage, targetGroup)
newTextGame消息 ()
使用 SendMessage 函数创建一条包含文本的新消息,该消息将从服务器发送给玩家收件人。消息格式类似于客户端 SDK 中使用的格式Realtime(请参阅RTMessage)。返回 gameMessage
对象。
语法
rtSession.newTextGameMessage(opcode, sender, payload)
newBinaryGame消息 ()
使用 SendMessage 函数创建一条包含二进制数据的新消息,该消息将从服务器发送给玩家收件人。消息格式类似于客户端 SDK 中使用的格式Realtime(请参阅RTMessage)。返回 gameMessage
对象。
语法
rtSession.newBinaryGameMessage(opcode, sender, binaryPayload)
玩家对象
访问与玩家相关的信息。
player.peerId
连接到 Realtime Server 和加入游戏会话时分配到游戏客户端的唯一 ID。
玩家。 playerSessionId
玩家会话 ID 由游戏客户端在连接到 Realtime Server 和加入游戏会话时引用。
游戏消息对象
使用这些方法可以访问 Realtime 服务器收到的消息。从游戏客户端收到的消息具有 RTMessage 结构。
getPayloadAs文本 ()
以文本形式获取游戏消息有效载荷。
语法
gameMessage.getPayloadAsText()
gameMessage.opcode
消息中包含的操作代码。
gameMessage.payload
消息中包含的有效负载。可以是文本或二进制数据。
gameMessage.sender
发送消息的游戏客户端的对等连接 ID。
gameMessage.reliable
布尔值,指示通过 TCP (true) 还是 UDP (false) 发送消息。
配置对象
配置对象可用于覆盖默认配置。
configuration.maxPlayers
可以接受的最大客户端/服务器连接数 RealTimeServers。
默认值为 32。
配置。 pingIntervalTime
服务器尝试向所有连接的客户端发送 ping 命令以验证连接是否正常的时间间隔(以毫秒为单位)。
默认值为 3000ms。