update main script
This commit is contained in:
parent
77d779dd4d
commit
2f947401da
|
@ -23,43 +23,66 @@ local ws = function (dict)
|
||||||
print(msg)
|
print(msg)
|
||||||
end
|
end
|
||||||
local server = game:GetService('HttpService')
|
local server = game:GetService('HttpService')
|
||||||
local id = server:RequestAsync({
|
local response, status = server:RequestAsync({
|
||||||
["Url"] = BaseUrl .. "api/connect/" .. JobId,
|
["Url"] = BaseUrl .. "/api/connect/" .. JobId,
|
||||||
["Method"] = "PUT",
|
["Method"] = "PUT",
|
||||||
["Body"] = dict.url
|
["Body"] = server:JSONEncode({["url"] = dict.url}),
|
||||||
}).Body
|
["Headers"] = {["Content-Type"] = "application/json"}
|
||||||
print(id)
|
})
|
||||||
print(typeof(id))
|
if response.Success then
|
||||||
local function sendMessage(msg)
|
local id = response.Body
|
||||||
wait()
|
local function sendMessage(msg)
|
||||||
server:PostAsync(BaseUrl .. "api/send/" .. JobId .."/"..id, msg)
|
wait()
|
||||||
end
|
local resp = server:RequestAsync({
|
||||||
|
["Url"] = BaseUrl .. "/api/send/" .. JobId .."/"..id,
|
||||||
local function close()
|
["Method"] = "POST",
|
||||||
server:RequestAsync({
|
["Body"] = server:JSONEncode({["data"] = msg}),
|
||||||
["Url"] = BaseUrl .. "api/close/" .. JobId .. "/" .. id,
|
["Headers"] = {["Content-Type"] = "application/json"}
|
||||||
["Method"] = "DELETE"
|
})
|
||||||
})
|
if not resp.Success then
|
||||||
end
|
error("Error Sending Message: " .. resp.StatusCode .. " " .. resp.StatusMessage)
|
||||||
|
|
||||||
local loop = coroutine.create(function()
|
|
||||||
while wait(50 / 1000) do
|
|
||||||
local msg = server:GetAsync(BaseUrl .. "api/poll/"..JobId.."/"..id)
|
|
||||||
if notExists(messages, msg) then
|
|
||||||
table.insert(messages, msg)
|
|
||||||
if notEmpty(msg) then
|
|
||||||
onMessage(msg)
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end)
|
|
||||||
coroutine.resume(loop)
|
local function close()
|
||||||
game:BindToClose(close)
|
server:RequestAsync({
|
||||||
return {
|
["Url"] = BaseUrl .. "/api/close/" .. JobId .. "/" .. id,
|
||||||
sendMessage = sendMessage,
|
["Method"] = "DELETE"
|
||||||
onMessage = onMessage,
|
})
|
||||||
close = close
|
end
|
||||||
}
|
|
||||||
|
local loop = coroutine.create(function()
|
||||||
|
while wait(0.1) do
|
||||||
|
print("poll")
|
||||||
|
local response = server:RequestAsync({
|
||||||
|
["Url"] = BaseUrl .. "/api/poll/"..JobId.."/".. id,
|
||||||
|
["Method"] = "GET"
|
||||||
|
})
|
||||||
|
|
||||||
|
if response.Success then
|
||||||
|
local msg = response.Body
|
||||||
|
-- Rest of the code
|
||||||
|
else
|
||||||
|
error("Error Polling Messages: " .. response.StatusCode .. " " .. response.StatusMessage)
|
||||||
|
end
|
||||||
|
if notExists(messages, msg) then
|
||||||
|
table.insert(messages, msg)
|
||||||
|
if notEmpty(msg) then
|
||||||
|
onMessage(msg)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end)
|
||||||
|
coroutine.resume(loop)
|
||||||
|
game:BindToClose(close)
|
||||||
|
return {
|
||||||
|
sendMessage = sendMessage,
|
||||||
|
onMessage = onMessage,
|
||||||
|
close = close
|
||||||
|
}
|
||||||
|
else
|
||||||
|
error("Error Connecting to Websocket Server: " .. response.StatusCode .. " " .. response.StatusMessage)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
return ws
|
return ws
|
||||||
|
|
Loading…
Reference in a new issue