Page 1 of 1
[SOLVED] How to get timeout in Lua https
Posted: Tue Sep 17, 2024 10:11 pm
by Gunroar:Cannon()
slime wrote: ↑Fri Sep 06, 2024 7:55 pm
love 12 provides
lua-https. love 11 doesn't provide it itself but you can get or make a standalone build of it.
I can't seem to set the timeout for lua-https, so that it will stop stalling/hanging the game after one second and not ~10 seconds like it currently does.
Setting https.TIMEOUT/http.TIMEOUT doesn't work, neither does setting it in the options argument. I've browsed and looked around and besides these I can't find it.
Re: How to get timeout in Lua https
Posted: Wed Sep 18, 2024 2:36 am
by slime
What made you assume there is a timeout API?
If you want to prevent stalls, you'd need to use a thread since any https request, even successful ones that happen quickly, will block the thread they're called from until the request is done. That being said, what sort of situation are you creating where you're regularly expecting a timeout?
Re: How to get timeout in Lua https
Posted: Wed Sep 18, 2024 9:57 am
by Gunroar:Cannon()
I thought there were timeouts because I saw some stuff when I browsed it. I know it's not the same lib, but there were many different pages like
this.
I'm doing a thing where I check and submit scores to Lootlocker after a game. If there is no connection at all it's fine because it just loads and skips immediately, but if there is a bad connection, or one without data, the display I put up that says "Checking online scores"
* will show for a long time (i.e. the game will hang for a really long time) even though I know at that point it shouldn't take more than a few seconds.
Also I thought, being a newb in most things, that all https/http libs had a way to include timeout. Like how they all have a way to include body/data.
So the only way to introduce timeouts is with threads?
* I put a display that greys over the whole game and shows that text so players will atleast know why the game is hanging.
Re: How to get timeout in Lua https
Posted: Wed Sep 18, 2024 10:56 am
by slime
Gunroar:Cannon() wrote: ↑Wed Sep 18, 2024 9:57 am
I thought there were timeouts because I saw some stuff when I browsed it. I know it's not the same lib, but there were many different pages like
this.
Yeah, that's for a totally different library with different APIs.
Re: How to get timeout in Lua https
Posted: Fri Sep 20, 2024 2:45 pm
by Gunroar:Cannon()
slime wrote: ↑Wed Sep 18, 2024 10:56 am
Yeah, that's for a totally different library with different APIs.
I implemented it...
Code: Select all
local _https = require("https")
local https = {
request = function(url, body, timeout)
local threadCode = string.format([[
local url, body = ...
local _https = require("https")
local code, response, b, c = _https.request(url, body)
love.thread.getChannel("code"):push(code)
love.thread.getChannel("response"):push(response)
]])
thr = love.thread.newThread(threadCode)
thr:start(url, body)
local code = love.thread.getChannel("code"):pop()
if code then
return code, love.thread.getChannel("response"):pop()
end
love.timer.sleep(timeout or body.timeout or 2)
local code = love.thread.getChannel("code"):pop()
if code then
return code, love.thread.getChannel("response"):pop()
else
return 0, "timed out"
end
end
}
Nothing too fancy, just a basic mock up (it works though).