Last active
April 5, 2021 07:01
-
-
Save ProGM/9786014 to your computer and use it in GitHub Desktop.
A simple connection checker for Corona SDK
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--------------------------------------- | |
-- Test connection to the internet | |
--------------------------------------- | |
local socket = require("socket") | |
local connection = {} | |
local function manual_test(callback, timeout) | |
if timeout == nil then timeout = 1000 end | |
local connection = assert(socket.tcp()) | |
connection:settimeout(0) | |
local result = connection:connect("www.google.com", 80) | |
local t | |
t = timer.performWithDelay( 10, function() | |
local r, w, e = socket.select(nil, {connection}, 0) | |
if w[1] or timeout == 0 then | |
connection:close() | |
timer.cancel( t ) | |
callback(timeout > 0) | |
end | |
timeout = timeout - 10 | |
end , 0) | |
end | |
local isReachable = nil | |
function connection.test(callback) | |
if network.canDetectNetworkStatusChanges then | |
if isReachable == nil then | |
local function networkListener(event) | |
isReachable = event.isReachable | |
callback(isReachable) | |
end | |
network.setStatusListener( "www.google.com", networkListener ) | |
else | |
callback(isReachable) | |
end | |
else | |
manual_test(callback) | |
end | |
end | |
return connection |
Hi, how do I run this piece of code ?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
i used below link and solved my problem :
https://forums.coronalabs.com/topic/33356-check-for-internet-connection/