What’s a better way to do below? As the status isn’t being updated within the same loop. Or just have a periodic check that updates the status?
Actually I realize when doing normal HTTP requests just check for response.status == 0 and if that is the case then attempt again after a few moments maybe, then if it still isn’t working inform user Internet connection appears to be down, then boot them back to a login screen if necessary.
For anyone searching in the future, you can test for Internet connection on desktop with this kind of method. Although don’t constantly check on update (especially not for domains you don’t own such as google’s!), I set update rate to 1 just for testing . Only test when you need to. For Desktop targets use something like this, for mobile use sys.get_connectivity, because you will want to check for cellular network or not too. If you do use a test like this pick a domain or link you own with a very small amount of text, such as an S3 page.
function http_result(self, id, response)
if (response.status == 0) then
print("Net offline!")
sys.net_connection = 0
else
print("Net online!")
sys.net_connection = 1
end
end
function check_internet_connection(self)
http.request("http://www.google.com", "GET", http_result)
end
function update(self, dt)
check_internet_connection()
if (sys.net_connection == 0) then
print("No internet connection!")
else
print("Internet connection active!")
end
end