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/
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