Hi Trevix and Bernd
Actually Trevix is correct.
I don't understand why - when I was calling the handler in my stack from the messageBox it worked flawlessly and I had no cacheing issues, but this doesn't seem to be the case when called from a stack.
When calling the code from a stack (while I checked Trevix's stack, I made a new one to ensure not issues with other code),
the first time the button is pressed with the internet off, there is a 30+ second delay.
Every subsequent press is just a few milliseconds and works as expected. Not only that, but when I kept trying it eventually crashed my IDE several times. I suspect there some kind of weird bug there, possibly a memory leak of some kind? Should this be reported?
I didn't see any change commenting out the openSockets-related code in preOpenStack, this issue remained. I comment on that code below.
Anyway, I was able to work around the issue using
networkInterfaces
When connected to my wifi, this returned
but with the internet switched off, only the loopback IP is available:
so the obvious is to use this to check if internet connection: if
networkInterfaces = "127.0.0.1" is true, there is no internet connection.
I updated my code also to fit in with Trevix's
myLog handler (effectively does the same as my ipDelegate handler, no point in having both) and added a global to log the elapsed time.
What I don't know is if this will work on Android - may Trevix has already tried this and found
networkInterfaces doesn't? (it is
supposed to)
Anyway the updated code is in the stack script:
Code: Select all
global gStart
local sCurrentTry
command putExternalIP
local tJSON, tArray
if the networkInterfaces = "127.0.0.1" then // only loopback interface available; if internet connection should be >1 lines
add 1 to sCurrentTry
if sCurrentTry > field "fldCounter" then
myLog "Sorry: no internet after" && field "fldCounter" && "attempts"
put 0 into sCurrentTry
MyLog "Elapsed:" && the milliseconds - gStart && "ms"
else
send "putExternalIP" to me in 500 milliseconds
MyLog "No internet connection - attempt " & sCurrentTry && "of" && fld "fldCounter"
end if
else
put JSONToArray(URL ("https://ipv4.ipleak.net/json/" )) into tArray
myLog tArray["ip"]
MyLog "Elapsed:" && the milliseconds - gStart && "ms"
put 0 into sCurrentTry
end if
end putExternalIP
command MyLog pText
put pText & cr after fld "MyLog"
end MyLog
and the button code:
Code: Select all
global gStart
command Mouseup
put the milliseconds into gStart
put empty into fld "MyLog"
putExternalIP
end Mouseup
Out of habit I move the preOpenStack into the card script because I think it's supposed to reside in the script of card 1
I don't really understand the openSockets-related code... if you want the local IP then I would use
networkInterfaces instead... on my system the openSockets() just returns a 5-digit number (eg: 49729) so the code below that just won't work... but maybe that's an Android thing?
As it is, I don't understand it (at least on desktop...).
My new stack attached below.
--- EDIT --- I just noticed the tJSON variable is superfluous - I had added this because calling JSONToArray with a non-JSON string (eg empty) caused an error, but this is no longer needed as JSONToArray is only called if there is a network connection and is reduced to a single line.