Skip to content

Commit

Permalink
Basic table serialization/deserialization
Browse files Browse the repository at this point in the history
  • Loading branch information
IconPippi committed Apr 29, 2023
1 parent 7c4a1f3 commit c295ec4
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/MockMessagingService/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ function MockMessagingService:PublishAsync(topic: string, message)
end

function MockMessagingService:SubscribeAsync(topic: string, callback): RBXScriptConnection
local callbackEvent = Instance.new("BindableEvent")
local callbackEvent: BindableEvent = Instance.new("BindableEvent")
callbackEvent.Parent = script
callbackEvent.Name = topic .. "_CallbackEvent"

Expand Down
7 changes: 7 additions & 0 deletions plugin/init.server.lua
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ end
toggleButton.Click:Connect(toggleButtonClicked)

-- ################## Data exchange logic ##################
local Serializer = game:GetService("HttpService")
local MMS = game.ServerScriptService:WaitForChild("MessagingService"):WaitForChild("MockMessagingService")

local publishRequest: BindableEvent = MMS:WaitForChild("PublishRequest")
Expand All @@ -62,6 +63,7 @@ plugin:SetSetting("_serverCount", _serverCount() + 1)
--[[ Constraints:
[Limit] [Maximum]
Topic length 80 characters
Size of message 1kB
Messages sent per game server 150 + 60 * (number of players in this game server) per minute
Messages received per topic (10 + 20 * number of servers) per minute
Expand Down Expand Up @@ -98,6 +100,10 @@ publishRequest.Event:Connect(function (topic: string, message)
error("Topic length exceeds 80 characters")
end

if typeof(message) ~= "string" then
message = Serializer:JSONEncode(message)
end

-- check if message size exceeds 1kB
if string.len(tostring(message)) > 1024 then
error("Message size exceeds 1kB")
Expand Down Expand Up @@ -151,6 +157,7 @@ game:GetService("RunService").Heartbeat:Connect(function()
end

local data = plugin:GetSetting(topic)
pcall(function() data = Serializer:JSONDecode(data) end)
if data then
-- we want to clear the setting field even if there's
-- no one subscribed to this topic ready to accept the data
Expand Down

0 comments on commit c295ec4

Please sign in to comment.