The code below currently makes more secure message service requests to other servers, by giving each message request a callback, and attempting 5 times to retrieve the data.
Utilmately I want to know if this really would work cuz, to me I’m worried about some random api thing that may break this system. Thank you in advance and have fun reading my code.
local MessageService = game:GetService("MessagingService")
local Knit = require(ReplicatedStorage.Packages.Knit)
local SharedFunctions = require(ReplicatedStorage.Shared.Functions)
local MessageCodeLength = 16
local GlobalMessageService = Knit.CreateService {
Name = "GlobalMessageService",
Client = {}
}
local function SendMessageRequest(RequestKey, CallbackKey, Paras)
local Code = SharedFunctions.GetCode(MessageCodeLength)
local SendData = {
Code = Code,
RequestedData = "waiting",
Paras = Paras
}
local TimeForConnection = 4
local CallbackConnection = MessageService:SubscribeAsync(CallbackKey, function(CallbackData)
local RealCallbackData = CallbackData.Data
if RealCallbackData.Code == SendData.Code then
SendData = RealCallbackData
end
end)
local Ticks = 1000
repeat
task.wait(TimeForConnection/Ticks)
Ticks -= 1
until SendData.RequestData == "waiting" or Ticks == 0
--Disconnect old stuff
CallbackConnection:Disconnect()
if Ticks == 0 then
return false
else
return SendData.RequestedData
end
end
function GlobalMessageService:SendSafeMessageRequest(RequestKey, CallbackKey, Paras, Attempts)
local CurrentAttempts = 5
repeat
local ReturnData = SendMessageRequest(RequestKey, CallbackKey, Paras)
if ReturnData then
return ReturnData
end
task.wait(1)
until CurrentAttempts == 0
return "After "..tostring(Attempts).." global message service was unsuccessful in reciving data"
end
function GlobalMessageService:KnitStart()
end
return GlobalMessageService ```
also here is the receiving side of the messages, this only happens on one server btw (I have a whole system in place for that.)
```MessageService:SubscribeAsync(PositionRequestKey, function(CallbackData)
print("Recieved Data 793 Position request")
local RealData = CallbackData.Data
local Paras = RealData.Paras
local SearchUserId = Paras[1]
--Convert Search id to string as that's the type player data holds userid as
SearchUserId = tostring(SearchUserId)
if not SearchUserId then
warn("no search userId provided for position request")
end
print("Search Id is:"..tostring(SearchUserId))
--Ensure the leaderboard is loaded
if not EnsureLoaded() then
return
end
local ReturnData = {}
for Position, PlayerData in SortedLBTable do
print(PlayerData.UserId, "SortedLBTable")
if PlayerData.UserId == tostring(SearchUserId) then
print("same id")
ReturnData = {
Paras = Paras,
RequestedData = Position,
Code = RealData.Code,
}
MessageService:PublishAsync(PositionCallbackKey, ReturnData)
return
end
end
print("sending back rn, Position request, error occured tho")
ReturnData = {
Paras = Paras,
RequestedData = "error; Failed to find Player Position",
Code = RealData.Code,
}
MessageService:PublishAsync(PositionCallbackKey, ReturnData)
end)
MessageService:SubscribeAsync(RangeRequestKey, function(RequestData)
print("Recieved Data 793 Range request")
--Ensure the leaderboard is loaded
if not EnsureLoaded() then
warn("leaderboard isn't loaded")
return
end
local RealRequestData = RequestData.Data
--print("RANGE REQUESTED")
local Parameters = RealRequestData.Paras
print("Range Request Parameters")
SharedFunctions.PrintTable(Parameters)
local from = Parameters[1]
local to = Parameters[2]
--print("start")
--PrintTable(RealRequestData)
--PrintTable(SortedLBTable)
local ReturnTable = {}
-- print(to, from, "range", "dif:", to-from)
for i = 0, (to-from) do
local key = i + from
--- print(i, key, SortedLBTable[key], " Primary Server Print")
ReturnTable[key] = SortedLBTable[key]
end
-- print("return table 748 :")
-- PrintTable(ReturnTable)
local ReturnData = {
Paras = Parameters,
Code = RealRequestData.Code,
RequestedData = ReturnTable,
}
--- print("RANGE REQUESTED OVER")
print("sending back rn, range request")
MessageService:PublishAsync(RangeCallbackKey, ReturnData)
end)```