I’m testing these two. Is sys.connectivity_host for sys.get_connectivity? There are no examples for sys.connectivity_host and no other references than what’s in the ref so I’m unsure.
I’m testing sys.get_connectivity() and it seems to never set sys.NETWORK_CONNECTED to anything other than 1. Even if I completely disable my network connection. Tested on Mac OS X.
sys.get_connectivity() can be used to figure out if the client has internet access and if so what type of access. sys.set_connectivity_host() allows you to specify which webpage to use when trying to figure out if the client actually has internet access or not. In the case of a King game we set it to our game servers. For other games maybe www.google.com or some CDN url could work.
local connection_state = sys.get_connectivity()
if connection_state == sys.NETWORK_DISCONNECTED then
print("I'm not connected")
elseif connection_state == sys.NETWORK_CONNECTED_CELLULAR then
print("I'm connected using a cellular carrier")
elseif connection_state == sys.NETWORK_CONNECTED then
print("I'm connected using a wifi or wired connection")
end
I have tested that and it always says I’m connected “I’m connected using a wifi or wired connection” with or without an actual Internet connection active.
Tested on Windows now too. Disabled network completely, no Internet connection at all, says I’m connected. Broken?
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
You shouldn’t use sys.NETWORK_CONNECTED as a variable, it is a constant. Also I don’t really understand why you try to do it yourself when there is an api for getting connectivity?
But to answer your question (I think) :
To get the result from a http request in a way that can be used as if it is synchronous you need to use a coroutine.
How sys.get_connectivity() works inside? I have an adress (wss://), that works good on mobile with this method, and doesn’t work on dekstop using http.request() for checking (why i need this).
Would be great to have the same method on dekstop.
On iOS we use SCNetworkReachabilitySetCallback() to listen for changes. On OSX, it’s always returning “connected”. Looking at the docs it seems it should be supported on OSX too?
I have no good idea how to do it on Win/Linux though.
Looks like on Android method sys.set_connectivity_host() doesn’t matter and don’t use for connectivity check.
That mean in my situation I can’t check host availability, if I have internet connection but host unavailable for some reason - I’ll receive ANR with getaddrinfo() =(
Ok, I’ll leave only internet connectivity checking, instead of attempts to check the host.