Make it poll less, roblox has a limit 😭

This commit is contained in:
Christopher Cookman 2024-09-04 12:01:11 -06:00
parent 923b4654d1
commit af26ff5dd5
Signed by: ChrisChrome
GPG key ID: A023A26E42C33A42

View file

@ -1,7 +1,7 @@
local Base64 = require(script.Base64)
local JobId = game:GetService('HttpService'):GenerateGUID(false)
print("jobid is " .. JobId)
local BaseUrl = "https://sock.kcadev.org"
local config = require(script.Parent.Parent:WaitForChild("Configuration"))
local BaseUrl = config["websocket-proxy"]
local atob = Base64.atob
local btoa = Base64.btoa
local messages = {}
@ -19,10 +19,14 @@ local function notEmpty(s)
end
local ws = function (dict)
local socket = {}
local closed = false
local onMessage = dict.onMessage or function(msg)
print(msg)
end
local onError = dict.onError or function(err)
print(err)
end
local server = game:GetService('HttpService')
local response, status = server:RequestAsync({
["Url"] = BaseUrl .. "/api/connect/" .. JobId,
@ -41,16 +45,16 @@ local ws = function (dict)
["Headers"] = {["Content-Type"] = "application/json"}
})
if not resp.Success then
error("Error Sending Message: " .. resp.StatusCode .. " " .. resp.StatusMessage)
onError({message = resp.StatusMessage, code = resp.StatusCode})
return false
else
return true
end
end
local loop = task.spawn(function()
while wait(0.1) do
print("poll")
while wait(.5) do
local response = server:RequestAsync({
["Url"] = BaseUrl .. "/api/poll/"..JobId.."/".. id,
["Method"] = "GET"
@ -61,11 +65,12 @@ local ws = function (dict)
if notExists(messages, msg) then
table.insert(messages, msg)
if notEmpty(msg) then
onMessage(msg)
onMessage(socket, msg)
end
end
else
error("Error Polling Messages: " .. response.StatusCode .. " " .. response.StatusMessage)
onError({message = response.StatusMessage, code = response.StatusCode})
break
end
end
end)
@ -81,13 +86,15 @@ local ws = function (dict)
end
game:BindToClose(close)
return {
socket = {
sendMessage = sendMessage,
onMessage = onMessage,
close = close
}
return socket
else
error("Error Connecting to Websocket Server: " .. response.StatusCode .. " " .. response.StatusMessage)
onError({message = response.StatusMessage, code = response.StatusCode})
return false
end
end