傳送服務 允許相同經驗的伺服器在實時(少於 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
這個功能將提供的訊息傳送給主題的所有訂閱者,觸發他們註冊的回呼被執行。
直到訊息被後端接收為止。
參數
決定訊息傳送到哪裡。
包含在訊息中的資料。
返回
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(),該方法將提供的訊息發送給主題的所有訂閱者,觸發他們註冊的回呼被執行。
參數
返回
可用於取消主題訂閱的連線。
範例程式碼
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.
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)