Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

Check For Internet Connection
Started by CrunchyApps Apr 02 2013 05:35 AM

* * * * * 6 votes
24 replies to this topic
check internet connection internet connection corona
[TOPIC CONTROLS]
This topic has been archived. This means that you cannot reply to this topic.
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#1

CrunchyApps

[GLOBAL: userInfoPane.html]
CrunchyApps
  • Enthusiast

  • 37 posts
  • Corona SDK

Hello! Is there a way to check for an active internet connection? I want to do the check when the app launches.

I created a scene in storyboard and used the code I found here: 

http://developer.coronalabs.com/code/check-internet-connection

 

scene1.lua

local http = require("socket.http")
local ltn12 = require("ltn12")


if http.request( "http://www.google.com" ) == nil then


        local function onCloseApp( event )
        if "clicked" == event.action then
                os.exit()
        end
        end


        native.showAlert( "Alert", "An internet connection is required to use this application.", { "Exit" }, onCloseApp )
else
    storyboard.gotoScene( "scene2", "crossFade", 75  )
end

It works pretty well but slows the application. There is a huge delay between scenes and Revmob fullscreen ad no longer shows.

Is there a better way to check for an active internet connection?

 

Thank you!

 



[TOPIC: post.html]
#2

aukStudios

[GLOBAL: userInfoPane.html]
aukStudios
  • Corona Geek

  • 1,003 posts
  • Corona SDK

Hi there,

 

On iOS, you can use the network.setStatusListener() API, which will automatically monitor the network status.  Check it out here: http://docs.coronalabs.com/api/library/network/setStatusListener.html.

 

On Android, I do something similar to what you do -- a quick socket test to see if the internet is accessible or not.

 

Hope this helps!

 

- Andrew



[TOPIC: post.html]
#3

CrunchyApps

[GLOBAL: userInfoPane.html]
CrunchyApps
  • Enthusiast

  • 37 posts
  • Corona SDK

Hello aukStudios and thanks for the quick response.

The problem is actually on Android; on a slow device this test might freeze the app for a couple of seconds or even crash it.



[TOPIC: post.html]
#4

aukStudios

[GLOBAL: userInfoPane.html]
aukStudios
  • Corona Geek

  • 1,003 posts
  • Corona SDK

Hi there,

 

Try using something like the following instead.  You can set the timeout of the socket connection, so that it won't hold up your app indefinitely.

 

You can wrap this code into a function which will return true/false whether the internet connection is available per this method of testing it.

 

local socket = require("socket")
            
local test = socket.tcp()
test:settimeout(1000)                   -- Set timeout to 1 second
            
local testResult = test:connect("www.google.com", 80)        -- Note that the test does not work if we put http:// in front
 
if not(testResult == nil) then
    print("Internet access is available")
else
    print("Internet access is not available")
end
            
test:close()
test = nil



[TOPIC: post.html]
#5

CrunchyApps

[GLOBAL: userInfoPane.html]
CrunchyApps
  • Enthusiast

  • 37 posts
  • Corona SDK

Andrew,

 

Thank you for the awesome post. It works very fast and doesn't slow down the app.

I combined your code with the first one and tested it on my device.

 

You should share it  as there are many developers having a hard time figuring out how to make a fast check on a internet connection ( http://developer.coronalabs.com/code/check-internet-connection), and your method is very good.

 

Sorry for my bad english.

Stefan



[TOPIC: post.html]
#6

Naomi

[GLOBAL: userInfoPane.html]
Naomi
  • Corona Geek

  • 2,303 posts
  • Corona SDK

Andrew @aukStudios, I like the timeout in your code.  I didn't have on in mine.  Thank you for sharing!

 

Naomi



[TOPIC: post.html]
#7

Naomi

[GLOBAL: userInfoPane.html]
Naomi
  • Corona Geek

  • 2,303 posts
  • Corona SDK

Hmmm... actually, updating my code caused some conflict (that is, my app started behaving oddly), so I reverted mine back to this:

local function networkConnection()
    local netConn = require('socket').connect('www.google.com', 80)
    if netConn == nil then
        return false
    end
    netConn:close()
    return true
end
 
I wonder why this revised version didn't work for me:

local function networkConnection()
    local socket = require("socket")
    local test = socket.tcp()
    test:settimeout(1000)  -- Set timeout to 1 second
    local netConn = test:connect("www.google.com", 80)
    if netConn == nil then
        return false
    end
    netConn:close()
    return true
end
 
Naomi
 
 


[TOPIC: post.html]
#8

aukStudios

[GLOBAL: userInfoPane.html]
aukStudios
  • Corona Geek

  • 1,003 posts
  • Corona SDK

Hi Naomi,

 

In the revised version, I think line 10 should be test:close() rather than netConn:close().  That might explain the odd behavior you said you were seeing.

 

- Andrew



[TOPIC: post.html]
#9

Naomi

[GLOBAL: userInfoPane.html]
Naomi
  • Corona Geek

  • 2,303 posts
  • Corona SDK

Ah, Andrew, thank you for spotting it.  Appreciate it!

 

Naomi



[TOPIC: post.html]
#10

aukStudios

[GLOBAL: userInfoPane.html]
aukStudios
  • Corona Geek

  • 1,003 posts
  • Corona SDK

No problem!



[TOPIC: post.html]
#11

remco

[GLOBAL: userInfoPane.html]
remco
  • Observer

  • 5 posts
  • Corona SDK

For anyone using the socket method: http://w3.impa.br/~diego/software/luasocket/tcp.html#settimeout

 

I had some serious problems, the settimeout uses seconds not miliseconds  :)

 

Use:

settimeout(1) -- sets timeout to 1 second
 


[TOPIC: post.html]
#12

aukStudios

[GLOBAL: userInfoPane.html]
aukStudios
  • Corona Geek

  • 1,003 posts
  • Corona SDK

That's right, it uses seconds.  In addition, I've found it helpful to use the 't' option mentioned on that page, like this:

 

settimeout(1, 't')

 

This way the timeout applies to the entire networking attempt, and not just each operation that LuaSocket performs.

 

- Andrew



[TOPIC: post.html]
#13

remco

[GLOBAL: userInfoPane.html]
remco
  • Observer

  • 5 posts
  • Corona SDK

Wont that interfere with any network.request i'm using? Because I dont want my original network.requests timeout by this option.



[TOPIC: post.html]
#14

aukStudios

[GLOBAL: userInfoPane.html]
aukStudios
  • Corona Geek

  • 1,003 posts
  • Corona SDK

Hi there,

 

I don't think it would interfere, since the timeout you set for this socket test is independent of the timeout you set for network.requests.

 

- Andrew



[TOPIC: post.html]
#15

sagarpawaskar

[GLOBAL: userInfoPane.html]
sagarpawaskar
  • Enthusiast

  • 33 posts
  • Corona SDK

do you guys block access to your game when Internet connection isn't available?



[TOPIC: post.html]
#16

aukStudios

[GLOBAL: userInfoPane.html]
aukStudios
  • Corona Geek

  • 1,003 posts
  • Corona SDK

No, I just alert the user when they take an action that requires the internet (e.g., facebook, in-app purchase) that they need to have an internet connection.

 

- Andrew



[TOPIC: post.html]
#17

-Garet-

[GLOBAL: userInfoPane.html]
-Garet-
  • Enthusiast

  • 58 posts
  • Corona SDK

I know it's a bit late, but I created a quick and easy library to check for internet connection. Maybe it'll be helpful to some people that find this topic in a search: http://igaret.com/tutorials/test-internet-connection-corona-sdk



[TOPIC: post.html]
#18

Pers

[GLOBAL: userInfoPane.html]
Pers
  • Enthusiast

  • 86 posts
  • Corona SDK

Garet, thanks for the code!

It saved me precious time :)



[TOPIC: post.html]
#19

Pers

[GLOBAL: userInfoPane.html]
Pers
  • Enthusiast

  • 86 posts
  • Corona SDK

FYI, using checking the internet connection with socket.tcp will work great ONLY if you have either no internet connection or a good internet connection.

 

If you have a flaky internet connection (such as a slow connection or a wifi signal whose router has disconnected from the internet) your app will freeze for several seconds. Sometimes up to 30 seconds.

 

Jorge



[TOPIC: post.html]
#20

oromanetti

[GLOBAL: userInfoPane.html]
oromanetti
  • Contributor

  • 234 posts
  • Corona SDK

Hi Guys,

Did you find a solution to avoid freezing app?

I noticed that with the code above, Xcode console shows this message each second :

 

WiFi:[419334385.238658]: 
Too frequent(3.034801 secs) rssi event from driver
 
As if checking for internet connexion periodically
Can't we check only one time and then stop the loop?
 

Thanks for your help

Best,

 

Olivier



[TOPIC: post.html]
#21

oromanetti

[GLOBAL: userInfoPane.html]
oromanetti
  • Contributor

  • 234 posts
  • Corona SDK

Hi Guys,

Did you find a solution to avoid freezing app?

I noticed that with the code above, Xcode console shows this message each second :

 

WiFi:[419334385.238658]: 
Too frequent(3.034801 secs) rssi event from driver
 
As if checking for internet connexion periodically
Can't we check only one time and then stop the loop?
 

Thanks for your help

Best,

 

Olivier



[TOPIC: post.html]
#22

aukStudios

[GLOBAL: userInfoPane.html]
aukStudios
  • Corona Geek

  • 1,003 posts
  • Corona SDK

Hi there,

 

I don't think you should place this code within a loop (such as a repeating timer.performWithDelay), especially at a frequency of one second.  If the socket takes more than one second to open and close, you'll be opening more and more sockets to the same host, which is bad.

 

Instead, in my experience it's worked well simply to wrap it in a function and call it to test network availability before critical user actions (such as attempting an IAP, attempting to share on social media, etc.), and alerting them if they don't have a network connection.

 

- Andrew



[TOPIC: post.html]
#23

oromanetti

[GLOBAL: userInfoPane.html]
oromanetti
  • Contributor

  • 234 posts
  • Corona SDK

Hi Andrew

Thanks for your answer

Sorry I didn't explain well.

I didn't put this code within a loop.

Like you say I simply wrap it in a function and call it to test network availability before critical user actions.

But I noticed that with this call, Xcode console shows this message each second :

 

WiFi:[419334385.238658]: 
Too frequent(3.034801 secs) rssi event from driver
 
Do you have this message too in your Xcode Organizer Console?
Thanks
 
Olivier


[TOPIC: post.html]
#24

aukStudios

[GLOBAL: userInfoPane.html]
aukStudios
  • Corona Geek

  • 1,003 posts
  • Corona SDK

Nope, I don't see that message in my Console log.

 

- Andrew



[TOPIC: post.html]
#25

davebollinger

[GLOBAL: userInfoPane.html]
davebollinger
  • Corona Geek

  • 1,358 posts
  • Corona SDK

fwiw, hth: network.getConnectionStatus() can be used as a first-pass fail-fast test
it's undocumented, but you probably have it, try: print(type(network.getConnectionStatus))

given that network.setStatusListener() doesn't seem to work on Android, and is flaky on iOS, you can use getConnectionStatus() to "decide" if it's even worth doing a more exhaustive (and potentially blocking) network request (via network api or socket lib or etc).

if the returnvalue.isConnected == false, then no point continuing. if true, you still need a more exhaustive second pass (cuz "is connected" does NOT mean "have useful internet") but second pass will now be less likely to block

BUT, as it's undocumented, if you choose to use it, I guess you'd have to test it with each release, as I suppose there's no guarantee it wouldn't just vanish some day


[topic_controls]
 
[/topic_controls]