Sys.get_connectivity, sys.connectivity_host only works on mobile (DEF-1899)


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.

Could I get another example with using these two?


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 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")


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?


sys.*_connectivity* API currently only returns valid data on mobile! This should probably be specified in the docs, will add an issue to update this. :thumbsup:


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
		print("Net online!")
		sys.net_connection = 1

function check_internet_connection(self)
	http.request("", "GET", http_result)

function update(self, dt)
	if (sys.net_connection == 0) then
		print("No internet connection!")
		print("Internet connection active!")


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.


The reason is that it’s not functional currently on desktop as mentioned higher in thread.

Isn’t it updated by sys.get_connectivity normally though?

Good idea about coroutine. Thank you, I’ll make an example of that.


No, sys.get_connectivity returns a value that can be one of three constants. Look at britzls code above.


Whoops. Thank you for pointing that out.


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.


What about Android?


On Android we use ConnectivityManager


Thank you!

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.


Hmm, yeah, it is unfortunate. There’s a getaddrinfo_a which is asynchronous, but it’s not used by LuaSocket and I’m not sure if it’s portable.