Test to know if a server is online, without using sockets

Hi,

I would like to share this algorithm and if it is possible to receive your feedback (thanks in advance).

I’ve tried it and it seems to work fine.

Regards,

Solca

-- -- Test to know if a server is online, without using sockets -- local AccessPoint = "https://subdomain.domain.com:port" -- Server URL (includes protocol and port) local timeOutTest = 3 -- seconds for timeout in network.request local online2 local function testPlatform( ) local function testPlatformInternal( event ) if (event.isError) then online2 = false else online2 = true end end local headers = {} local params = {} params.headers = headers params.timeout = timeOutTest network.request(AccessPoint,"GET",testPlatformInternal, params) end testPlatform () local function myWait( ... ) if ( online2 == true ) then print ("Plataform is ONLINE :) ") elseif ( online2 == false) then print ("Plataform is OFFLINE :(") else print ("PLATFORM IS UNDEFINED ??") end end local timeWait = timeOutTest \* 1000 + 200 -- timeout + 200 msec timer.performWithDelay( timeWait, myWait )

it’s fine, and it’s the typical approach, tho note:

  - all you really need is “HEAD”, not a full “GET”

  - you could use the callback instead of a separate timer

the socket approach can give more control over timeouts, and/or used as an additional fail-fast step prior to a network.request, but if you need neither then network.request alone should be fine.

aside:  network “status” tests (setStatusListener() for iOS, getConnectionStatus() for android) can also be used as a fail-fast step, but won’t tell you if you can actually USE the internet on that connection, so still need some sort of follow-up test.

it’s fine, and it’s the typical approach, tho note:

  - all you really need is “HEAD”, not a full “GET”

  - you could use the callback instead of a separate timer

the socket approach can give more control over timeouts, and/or used as an additional fail-fast step prior to a network.request, but if you need neither then network.request alone should be fine.

aside:  network “status” tests (setStatusListener() for iOS, getConnectionStatus() for android) can also be used as a fail-fast step, but won’t tell you if you can actually USE the internet on that connection, so still need some sort of follow-up test.