From c295ec4a451cf15b2545a56207fb83f2df4d613c Mon Sep 17 00:00:00 2001 From: IconPippi Date: Sat, 29 Apr 2023 23:14:56 +0200 Subject: [PATCH] Basic table serialization/deserialization --- lib/MockMessagingService/init.lua | 2 +- plugin/init.server.lua | 7 +++++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/lib/MockMessagingService/init.lua b/lib/MockMessagingService/init.lua index f0140da..1280ca8 100644 --- a/lib/MockMessagingService/init.lua +++ b/lib/MockMessagingService/init.lua @@ -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" diff --git a/plugin/init.server.lua b/plugin/init.server.lua index 64f6855..4f5cd65 100644 --- a/plugin/init.server.lua +++ b/plugin/init.server.lua @@ -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") @@ -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 @@ -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") @@ -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