Advertising request with Internet off

I’m using appodeal. The problem is this if the user starts the game with the active conection and then turns it off. When I need publicity I do something like:

if( appodeal.isLoaded("interstitial") ) then appodeal.show( "interstitial" ) startAds = true else startAds = false end 

This imposes a “startAds” equal to the true but in reality having the connition turned off the publicity will not be shown.

This is a problem because I perform several operations if startAds = true.

I could conquer the connection from time to time but this causes some slowdowns.

Is there another fluid way?

To verify the connection I do:

function M.checksInternetConnection() local socket = require("socket") local test = socket.tcp() test:settimeout(1000) -- Set timeout to 1 second local testResult = test:connect("www.google.com", 80) local thereIs if not(testResult == nil) then print("Internet access is available") thereIs = true else print("Internet access is not available") thereIs = false end test:close() test = nil return thereIs end

Never check internet connection with socket.tcp
This function has a big problem : if the connection lag, then your app will freeze for 20 seconds or more, and sometime forever, just making the phone crash.

How do I check the connection then?

socket is fine for testing the network , however…

test:settimeout(1000) -- Set timeout to 1 second

…sets the timeout to 1000 seconds, which seems excessive.

you can also use network.getConnectionStatus() (undocumented) as a first pass just to see if you have a network at all, if so then use socket.tcp (with a reasonable timeout) as a second pass to test if you have internet access.

on iOS you could set up a network status listener, but doesn’t work on android

My more info plugin checks network connections
https://marketplace.coronalabs.com/plugin/more-info

Even with a 1s timeout, if your internet connection lag, the timeout will not work and the app will freeze for 20s.
Have a look on the forum, it’s a known bug.
The best way is to have your own server and own page returning “ok” check if the page is reachable with a standard internet access.
I spend so much time on the différents forum founding this is the only safe solution.

@laty1 Then what code is recommended? 

thank you @davebollinger But I would like a good solution both on iOS and android

are you sure you set the timeout prior to connect?

fe, this form doesn’t allow it:

local socket = require("socket") local client, err = socket:connect("www.google.com",80) client:settimeout(1,"t") -- too late, no effect

but this form does:

local socket = require("socket") local master = socket.tcp() master:settimeout(1,"t") local client, err = master:connect("www.google.com",80) -- will use master's timeout

if that’s the source of your trouble, then it’s not a bug, it’s just different usage.

unfortunately, this code sucks and freeze your app if there is lag with your internet connection (black screen during at least 20s) :

local function isInternetOK() local socket = require("socket") local test\_connection = socket.tcp() local isNetworkAvailable = false test\_connection:settimeout(1,'b') -- blocking time test\_connection:settimeout(1,'t') -- total time local testResult = test\_connection:connect("www.google.com",80) if not(testResult == nil) then isNetworkAvailable = true else isNetworkAvailable = false end test\_connection:close() test\_connection = nil return isNetworkAvailable end

have a look here for instruction :

http://w3.impa.br/~diego/software/luasocket/tcp.html#settimeout

and here for known troubles :

https://forums.coronalabs.com/topic/33356-check-for-internet-connection/
 

@triogical Then what you use to test the connection?

on a quick glance it looks ok, so perhaps it’s the dns lookup that’s stalling you.  (that is, it might not be the actual connect that’s stalling, but the address lookup implied by that connect) if so, then give it an actual ip instead:

local one,err = master:connect("8.8.8.8", 53) -- google's dns

if even that fails, then it’s not the dns lookup either, so use settimeout(0) to go non-blocking, then connect (which should timeout immediately and appear to fail) then follow-up with socket.select() to poll for a writable connection.

No other dev has trouble by testing internet connection with socket when there is lag (in building, in subway,…) ?? It works well in every situation with all your users, for everyone ?

No, everyone has exactly the same trouble - if you have a bad connection and try to use it then your app will suffer in some way.  (obviously different apps will be affected in different ways, depending on how the network is used)

The OP asked how to determine if the user had shut off their connection.  That is easily and quickly done with either a network status listener on iOS or network.getConnectionStatus() on Android.  (and Scott Harrison’s plugin probably also reports the same level of “raw” network availability)  That’s “Phase 1”, and might be enough (depending on how the app uses that connection).

“Phase 2” of that question, asking “how GOOD or USEFUL is that connection”, is entirely different and really requires an actual attempt to USE the network it to measure its success.  Ideally you’d prefer this to be a non-blocking (aka asynchronous) request.  You can do that with just plain old network.request().  You can also do it with socket, but it will require settimeout(0) and socket.select() as I described above to make it all non-blocking.  But you shouldn’t even attempt any of these type methods if the Phase 1 test failed.

A lot of people have asked me on this. I am getting ready to push out an update that will determine if the phone network or wifi can connect to google.com in real time( not with functions)

Edit: as for the flakiness, more info handles that as well

When you check internet connection and the answer is ok, then you could send a network.request but if the connection is too slow and make your app lag then it could be a good thing to make a timer.performwithdelay(5 seconds) and then send a network.cancel for your previous request. Instead of keeping a 30s lagging connection.
is it a good idea ? And maybe the only one to avoid lag no?

I think if the app is freeze, even the timer will not work … but better to wait for more answers

Never check internet connection with socket.tcp
This function has a big problem : if the connection lag, then your app will freeze for 20 seconds or more, and sometime forever, just making the phone crash.

How do I check the connection then?

socket is fine for testing the network , however…

test:settimeout(1000) -- Set timeout to 1 second

…sets the timeout to 1000 seconds, which seems excessive.

you can also use network.getConnectionStatus() (undocumented) as a first pass just to see if you have a network at all, if so then use socket.tcp (with a reasonable timeout) as a second pass to test if you have internet access.

on iOS you could set up a network status listener, but doesn’t work on android

My more info plugin checks network connections
https://marketplace.coronalabs.com/plugin/more-info