-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c04681d
commit 5c7a132
Showing
1 changed file
with
64 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
export type MessagingClient={ | ||
Publish: (self: MessagingClient, topic:string, message: any) -> (), | ||
Subscribe: (self: MessagingClient, topic:string, callback: (message: any) -> ()) -> () | ||
} | ||
|
||
--[[ | ||
-- _ _ ____ _ ____ _____ | ||
-- | | | / ___| / \ / ___| ____| | ||
-- | | | \___ \ / _ \| | _| _| | ||
-- | |_| |___) / ___ \ |_| | |___ | ||
-- \___/|____/_/ \_\____|_____| | ||
-- | ||
------------------------------------------------------------- | ||
Publish: | ||
local MessagingClient=require(path.MessagingClient) | ||
local TOPIC="Example" | ||
MessagingClient.Publish(TOPIC, "Example") | ||
------------------------------------------------------------- | ||
Subscribe: | ||
local MessagingClient=require(path.MessagingClient) | ||
local TOPIC="Example" | ||
MessagingClient.Subscribe(TOPIC, function() | ||
end) | ||
]] | ||
|
||
local MessagingService = game:GetService("MessagingService") | ||
|
||
local MessagingClient = {} | ||
|
||
function MessagingClient:Publish(topic, message) | ||
local success, err = pcall(function() | ||
MessagingService:PublishAsync(topic, message) | ||
end) | ||
|
||
if not success then | ||
warn("Failed to publish message to topic ["..topic.."]: "..err) | ||
end | ||
end | ||
|
||
|
||
function MessagingClient:Subscribe(topic, callback) | ||
local success, err = pcall(function() | ||
MessagingService:SubscribeAsync(topic, function(message) | ||
callback(message.Data) | ||
end) | ||
end) | ||
|
||
if not success then | ||
warn("Failed to subscribe to topic ["..topic .."]: "..err) | ||
end | ||
end | ||
|
||
return MessagingClient |