I’m developing a game that requires Internet access to show ads and interacting with Google Play services, of course the build.settins file sets “android.permission.INTERNET” in the usesPermissions table for Android.
However, if the device has no internet access and ugly warning is displayed as soon as the game tries to init Google Play services, indicating there’s no Internet access.
I’m wondering if it’s possible, somehow, to detect no internet connection is available to skip executing methods requiring internet access.
local socket = require("socket") local http = require("socket.http") local function testNetworkConnection() print("testing connection") if http.request( "http://google.com/" ) == nil then print("cant connect to google") return false end print("got a connection") return true end
If you want to specify a timeout you can use the following snippet I come up with based on several posts on this forum.
local function is\_internet\_connection\_available() 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 test:close() return true end
@coronasdk771 – I think settimeout function takes the parameter in “seconds” ( not nanoseconds) – so, the timeout line in your code should be : test:settimeout(1) – for 1 second
What’s an appropriate amount of time to set as the timeout value? I’m assuming the overhead here is similar to a ping, and on 3G/4G/WiFi connection this will return in under a second. But what about slower connections, like in-flight wifi, airport wifi, and developing countries?
I guess one way to answer this is “as long as you can reasonably ask your users to wait” but I’m curious if anyone has any stats on how long this can take. Thanks!
local socket = require("socket") local http = require("socket.http") local function testNetworkConnection() print("testing connection") if http.request( "http://google.com/" ) == nil then print("cant connect to google") return false end print("got a connection") return true end
If you want to specify a timeout you can use the following snippet I come up with based on several posts on this forum.
local function is\_internet\_connection\_available() 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 test:close() return true end
@coronasdk771 – I think settimeout function takes the parameter in “seconds” ( not nanoseconds) – so, the timeout line in your code should be : test:settimeout(1) – for 1 second
What’s an appropriate amount of time to set as the timeout value? I’m assuming the overhead here is similar to a ping, and on 3G/4G/WiFi connection this will return in under a second. But what about slower connections, like in-flight wifi, airport wifi, and developing countries?
I guess one way to answer this is “as long as you can reasonably ask your users to wait” but I’m curious if anyone has any stats on how long this can take. Thanks!