Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LuaSocket to test presence of internet connection

I'm trying to test the presence of an internet connection in Corona SDK, using LuaSocket library.

I found this solution:

function test()           
    local connection = socket.tcp()
    connection:settimeout(1000)
    local result = connection:connect("www.google.com", 80)
    connection:close()
    if (result) then return true end
    return false
end

But it have a problem: if there is a bad/unstable connection, the program is blocked until the socket is running (for various seconds).

So I tried like this:

    connection:settimeout(1000, 't')

But it's very inaccurate (it's returning false where there is a little network lag). There is a better way?

Maybe making the socket not blocking?

UPDATE 2: I tried this code, but I can't really understand if it makes sense...

local socket = require("socket") 
function test(callback, timeout)
    if timeout == nil then timeout = 1000 end
    local connection = socket.tcp()
    connection:settimeout(0)
    connection:connect("www.google.com", 80)
    local t
    t = timer.performWithDelay( 10, function()
        local r = socket.select({connection}, nil, 0)
        if r[1] or timeout == 0 then
            connection:close()
            timer.cancel( t )
            callback(timeout > 0)
        end
        timeout = timeout - 10
    end , 0)
end

It always returns "no connection"

like image 935
ProGM Avatar asked Oct 16 '25 03:10

ProGM


2 Answers

Finally I found a way to make it work an all devices. Thank you to hades2510:

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

https://gist.github.com/ProGM/9786014

like image 184
ProGM Avatar answered Oct 18 '25 23:10

ProGM


You can take a look over over network.setStatusListener.

You cannot use IP addresses with it but it seems like you don't care about that.

like image 35
Radu Diță Avatar answered Oct 18 '25 23:10

Radu Diță