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


#1

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?


#2

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

#3

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?


#4

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:


#5

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

#6

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.


#7

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.


#8

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


#9

Whoops. Thank you for pointing that out.


#10

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.


#11

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.


#12

What about Android?


#13

On Android we use ConnectivityManager


#14

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.


#15

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.