MessagingService

显示已弃用

*此内容使用人工智能(Beta)翻译,可能包含错误。若要查看英文页面,请点按 此处

无法创建
服务
未复制

消息传递服务 允许相同经验的服务器在实时(小于 1 秒)使用主题进行通信(少于 1 秒)。主题是开发者定义的字符串(1–80个字符),服务器使用它来发送和接收消息。

交付是最大努力,但不保证。请确保设计您的体验,以避免交付失败对关键不敏感。

跨服务器消息传递 探索如何在更大的细节中通过服务器进行通信。

如果您想将临时消息发布到实时游戏服务器,您可以使用 打开云 API

限制

请注意,这些限制可能会发生变化。


<th>最大值</th>
</tr>
</thead>
<tbody>
<tr>
<td>
<b>消息的大小</b>
</td>
<td>
1kB
</td>
</tr>
<tr>
<td>
<b>每个游戏服务器发送的消息</b>
</td>
<td>
600+240\*(此游戏服务器上的玩家数量)每分钟
</td>
</tr>
<tr>
<td>
<b>每个主题收到的消息</b>
</td>
<td>
(40+80\*服务器数量)每分钟
</td>
</tr>
<tr>
<td>
<b>为整个游戏收到的消息</b>
</td>
<td>
(400+200\*服务器数量)每分钟
</td>
</tr>
<tr>
<td>
<b>每个游戏服务器允许的订阅</b>
</td>
<td>
20 + 8\*(此游戏服务器上的玩家数量)
</td>
</tr>
<tr>
<td>
<b>每个游戏服务器的订阅请求</b>
</td>
<td>
每分钟 240 个请求
</td>
</tr>
</tbody>
限制

概要

方法

属性

方法

PublishAsync

()
暂停

该函数将提供的消息发送给主题的所有订阅者,触发他们注册的回调被调用。

直到消息被后端接收为止提供。

参数

topic: string

决定消息发送到哪里。

默认值:""
message: Variant

要包含在消信息中的数据。

默认值:""

返回

()

SubscribeAsync

暂停

该函数注册一个回调,开始收听指定的主题。回调在主题收到消信息时被调用。它可以多次调用相同主题。

回调

回调以单个参数被调用,表中包含以下入口:


<th>摘要</th>
</tr>
</thead>
<tbody>
<tr>
<td>
<b>数据</b>
</td>
<td>
开发者提供了付载物
</td>
</tr>
<tr>
<td>
<b>已发送</b>
</td>
<td>
在消息发送时的秒数 Unix 时间
</td>
</tr>
</tbody>
领域

直到订阅正确注册并返回连接对象为止。

要取消订阅,请在返回的对象上调用 Disconnect() 。一旦调用,回调不应被触发。杀死包含连接的脚本也会导致底层连接取消订阅。

还请参阅MessagingService:PublishAsync(),该方法将提供的消息发送给主题的所有订阅者,触发他们注册的回调被调用。

参数

topic: string

决定在哪里收听消息。

默认值:""
callback: function

在收到消息时触发的函数。

默认值:""

返回

可用于取消订阅主题的连接。

代码示例

This example demonstrates how to use MessagingService:SubscribeAsync() to listen to a topic for cross-server chat within a game universe.

When a player joins, the example subscribes to the topic player-<player.UserId>. When a message is sent with this topic, the connected callback executes and prints Received message for <player.Name">. It also disconnects the connection when the player's ancestry changes.

In order for this to work as expected it must be placed in a server Script.

Subscribing to Cross Server Messages

local MessagingService = game:GetService("MessagingService")
local Players = game:GetService("Players")
local function onPlayerAdded(player)
--subscribe to the topic
local topic = "player-" .. player.UserId
local connection = MessagingService:SubscribeAsync(topic, function(message)
print("Received message for", player.Name, message.Data)
end)
player.AncestryChanged:Connect(function()
-- unsubscribe from the topic
connection:Disconnect()
end)
end
Players.PlayerAdded:Connect(onPlayerAdded)

活动