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