I was wondering how to find out internet connectivity of ios and android based device?
for example, I want to write code like this..
Code: Select all
if there is an internet connection then
   doTheWork
else
  skipTheWork
end if
Cheers,
Louis
Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller
Code: Select all
if there is an internet connection then
   doTheWork
else
  skipTheWork
end if
Code: Select all
put url "http://www.google.com" into tURL
 
  if tURL is empty then
      --skipTheWork
   else
      --doTheWork
   end ifCode: Select all
function appIsConnected
   
   local tShellOutput,tResult
   
   switch (the platform)
      case "MacOS"
         put shell("ping -c 1 -t 5" && the uRIP["website"] of stack the mainstack of this stack) into tShellOutput
         put the result into tResult
         break
      case "Win32"
         set the hideConsoleWindows to true
         put shell("ping -n 1 -w 5000" && the uRIP["website"] of stack the mainstack of this stack) into tShellOutput
         put the result into tResult
         break
      case "Linux"
         put shell("ping -c 1 -w 5") && the uRIP["website"] of stack the mainstack of this stack into tShellOutput
         put the result into tResult
         break
      default -- all other OSes
         put param(0) & colon && the platform && "platform not supported" into tResult
         break
      end switch
   
      -- check for connection failure
   if tResult <> empty then
      return false -- failed
   else -- connected, but how well?
      set the itemDel to comma
      switch the platform
         case "Win32"
            get tShellOutput 
            filter it with "*(0% *"
            break
         case "MacOS" 
            repeat for each item tFilter in "* 0% *,* 0.0% *" -- for different OS versons
               get tShellOutput
               filter it with tFilter
               if it <> empty then exit repeat
            end repeat
            break
      end switch
      return (it <> empty)
   end if
end appIsConnected
Code: Select all
<html>
 <head>
  <title>PHP Test</title>
 </head>
 <body>
 <?php echo 'User IP Address_'.$_SERVER['REMOTE_ADDR']; ?>
 </body>
</html>Code: Select all
local sMyIP
on mouseUp pButtonNumber
     set the visible of image "LoadingSpinner.gif" to true
     put empty into sMyIP
     load URL "https://www.referi.it/returnip.php" with message "CheckGetMyIPreceived"
     repeat with U = 1 to 15
          if sMyIP is not empty then
               exit repeat
          end if
          wait 500 milliseconds with messages
     end repeat
     set the visible of image "LoadingSpinner.gif" to false
     if sMyIP is not empty then
          put lineOffset("User IP Address", sMyIP ) into tLineNum
          set the itemdelimiter to "_"
          answer word 1 of item 2 of line tLineNum of sMyIP 
     else
          answer "no Internet"
     end if
end mouseUp
command CheckGetMyIPreceived  pURL, pURLStatus
    if pURLStatus is "cached" then
         put URL pURL into sMyIP
    else
         put empty into sMyIP
    end if
    unload url pURL
end CheckGetMyIPreceivedCode: Select all
on mouseUp
    put "" into  fld "TheAnswer"
    tsNetInit
    tsNetSetTimeouts 5,5000, 5000, 5000, 0, 0
    put "https://api.ipify.org?format=json" into tGetString
    put tsNetGetSync(tGetString, tHeaders, tRecvHeaders, tResult, tBytes) into tTheAnswer
    tsNetClose
    put tTheAnswer into fld "TheAnswer"
end mouseUpCode: Select all
function CheckConnection
 local tAnswer,tArray,tHeaders, tRecvHeaders, tResult, tBytes
     if the platform <>  "Android" then tsNetInit 
     tsNetSetTimeouts 5,7000, 7000, 7000, 0, 0
     put "https://api.ipify.org?format=json" into tGetString
     put tsNetGetSync(tGetString, tHeaders, tRecvHeaders, tResult, tBytes) into tAnswer
     if the platform <>  "Android" then tsNetClose
     if tAnswer is not empty then
          put JSONToArray(tAnswer) into tArray
     end if
     return tArray["IP"]
end CheckConnectionCode: Select all
put tsNetGetTimeouts() into tOldTimeouts
...
tsNetSetTimeouts  tOldTimeoutsCode: Select all
put item 1 of tOldTimeouts into tDnsCacheTimeout
put item 2 of tOldTimeouts into tRequestTimeoutMS
....
--and then
tsNetSetTimeouts tDnsCacheTimeout, tRequestTimeoutMS, ...Code: Select all
local sCurrentTry, sNumberOfTries = 10 // number of attempts to connect to wifi
command putExternalIP
    local tJSON, tArray
    put URL ("https://ipv4.ipleak.net/json/") into tJSON // force ipv4 with ipleak.net
    if tJSON is empty then
        add 1 to sCurrentTry
        if sCurrentTry > sNumberOfTries then 
            ipDelegate "Sorry - could not connect tot he internet after" && sNumberOfTries && "attempts"
            put 0 into sCurrentTry
        else
            send "putExternalIP" to me in 500 milliseconds
            ipDelegate "No internet connection - attempt " & sCurrentTry && "of" && sNumberOfTries
        end if
    else
        put JSONToArray(tJSON) into tArray -- JSONToArray chokes if no internet, so only if JSON received. Could this have been the cause of your instabilities?
        ipDelegate tArray["ip"]
        put 0 into sCurrentTry
    end if
end putExternalIP
command ipDelegate pMessage // put the failure message or the ip in the msgBox
    put return & pMessage after msg
end ipDelegate

Code: Select all
On PreOpenstack
   tsNetInit --always init the library. In some case this is not needed but doesn't harm.
end Preopenstack
on mouseUp
     local tMyIP
     put CheckConnection() into tMyIP
     put tMyIP into tTest
     replace "." with "" in tTest
     if tTest is a number then
          answer tMyIP
     else
          answer "no internet"
     end if
end mouseUp
function CheckConnection
     local tAnswer,tArray,tHeaders, tRecvHeaders, tResult, tBytes
     set the visible of image "LoadingSpinner.gif"  of card "cdWelcome" to true --my wait spinner
     put tsNetGetTimeouts() into tOldTimeouts --save old timeout
     tsNetSetTimeouts 5,7000, 7000, 7000, 0, 0 --max 7 seconds wait, if no connection
     put "https://api.ipify.org?format=json" into tGetString
     put true into pSettings["no_reuse"] --this clear the cache??
     put tsNetGetSync(tGetString, tHeaders, tRecvHeaders, tResult, tBytes, pSettings) into tAnswer
     tsNetSetTimeouts item 1 of tOldTimeouts, item 2 of tOldTimeouts, item 3 of tOldTimeouts, item 4 of tOldTimeouts, item 5 of tOldTimeouts, item 6 of tOldTimeouts
     if tAnswer is not empty then
          put JSONToArray(tAnswer) into tArray --mergJson extension must be enabled on mobile
     end if
     set the visible of image "LoadingSpinner.gif" of card "cdWelcome" to false
     return tArray["IP"]
end CheckConnectionit will - it's an open source driven VPN provider aiming to promote net neutrality - been around for a while and will continue to be (well as a far anything will I suppose)
I'm not sure how to replicate your 20 sec delay... or even understand your testing proposition... the steps in the script are:trevix wrote: ↑Sat Sep 09, 2023 9:50 am
- The "put URL" is a blocking command and there is no way, that I know, to decide its timeout in LC. You can test this problem, using your script on the IDE (OSX at least): If you go from wifi enabled to wifi disabled, before reaching the "send" loop your script will stop on the line "put URL" for more then 20 seconds. This, for me, doesn't fit.
Code: Select all
line 3 "put url" 
line 4 "if Json is empty" and following will run fine, reporting immediately the IPCode: Select all
line 3 "put url" 
--at this point, not being connected, the script stops for an amount of seconds (in my case more then 20)
line 4 "if Json is empty" and following will run fine, adding another 10 seconds to the waitCode: Select all
command putExternalIP
    local tJSON, tArray, tCount, tMaxTries = 10
    repeat 
        put URL ("https://ipv4.ipleak.net/json/") into tJSON // force ipv4 with ipleak.net
        if tJSON is not empty then
            put JSONToArray(tJSON) into tArray
            put tArray["ip"]
            exit repeat
        end if
        add 1 to tCount
        if tCount > tMaxTries then exit repeat
        wait 500 milliseconds // blocking command
        put return &  "No internet connection - attempt" && tCount && "of" && tMaxTries after msg
    end repeat
end putExternalIP