Page 1 of 1

Check Internet Connection - when there is poor coverage

Posted: Fri Apr 29, 2016 5:38 am
by KimD
Checking internet connectivity, again ;-) My mobile app needs to be able to communicate via the internet, or fail elegantly when this isn't possible. To achieve this I've got the following function :

Function CheckMyWebsiteAvailable
Put "Status:NoInternet" into ccMyWebsiteStatus
Set the socketTimeoutInterval to 1500
Put true into ccTryTransmit
Put 1 into ccCount
// Try twice to make contact
Repeat until not ccTryTransmit
post "" to URL "http://MyWebsite.com/test-status.php"
put it into ccMyWebsiteResponse
If ccMyWebsiteResponse = "Status:Received" then
Put "Status:OKMyWebsite" into ccMyWebsiteStatus
Else
put URL "http://www.google.com" into ccConnCheck
if ccConnCheck is empty then
Put "Status:NoInternet" into ccMyWebsiteStatus
Else
Put "Status:NoMyWebsite" into ccMyWebsiteStatus
End if
End if
Put ccCount+1 into ccCount
If ((ccMyWebsiteStatus = "Status:OKMyWebsite") or (ccCount = 3)) then put false into ccTryTransmit
end Repeat
Return ccMyWebsiteStatus
end CheckMyWebsiteAvailable

Then I either display an error message, or carrying on with whatever action required communication to my website, depending on the value returned by CheckMyWebsiteAvailable().

If I have GOOD internet connectivity - the above works fine.

If I have ZERO internet connectivity (test device in flight mode) - the above works fine. My app comes back with my elegant error message after a couple of seconds.

But if I have V.POOR internet connectivity - the above function doesn't work. The user pushes a button that should trigger some communication with my website, then nothing happens. I've sat there and watched over a minute go past - no error message, no actions. FYI - V.POOR internet connectivity = I downloaded a map of my mobile service providers network coverage, then drove out and parked up in a spot where the test device alternated between 0 bar and 1 bar of 3G coverage. Then I spent an hour testing my app. It must have looked very odd ;-)

What I want to happen is have my function return a "Status:NoInternet" if it can't get a response from myWebsite or Google in under 10 seconds.

Does anyone else have some code for testing for internet connectivity, that correctly handles situations of present but very poor connectivity? Re-reading the forums has got me wondering whether I need an On SocketTimeout handler. I know nothing about sockets, but is this what I'm missing?

Thanks in advance

Re: Check Internet Connection - when there is poor coverage

Posted: Fri Apr 29, 2016 1:22 pm
by theotherbassist
Declare theFlag a local var

After the first line of your fn put:

Code: Select all

put true into theFlag
send "stopTrying" to me in 10 seconds
and just before "end checkMyWebsiteAvailable":

Code: Select all

put false into theFlag
then define below:

Code: Select all

command stopTrying
put false into theFlag
end stopTrying
Then make your repeat contingent upon theFlag being true, else exit and return text for whatever error you want. Maybe I messed up the syntax of this somehow, but you get the idea. You could also just use the difference in "the seconds" in your repeat loop (repeat... until time2 - time1 > 10) rather than having to send another command.

Re: Check Internet Connection - when there is poor coverage

Posted: Sat Apr 30, 2016 1:55 am
by KimD
Thanks. Yes - I get the idea. I'm concerned that LC will "hold" while on waiting on a response to the PUT or POST commands, and therefore not get to the point that it tests theFlag variable, but I'll try and report back what happens.

Regards

Re: Check Internet Connection - when there is poor coverage

Posted: Mon May 02, 2016 4:54 am
by KimD
Sorry - another long post. Here's what I've done:

1) Created a new stack containing:

--------------------------------------------------------
Button -
on mouseUp
Put empty into field "Output1"
Put the milliseconds into ggStartMilliseconds
Put CheckCogismoAvailable() into ssTemp
end mouseUp

Function CheckMyWebsiteAvailable
Put ((the milliseconds - ggStartMilliseconds) & " Start Check " & Return) after field "Output1"
Put "Status:NoInternet" into ccMyWebsiteStatus
Set the socketTimeoutInterval to 5000
Put ((the milliseconds - ggStartMilliseconds) & " Start Post MyWebsite " & Return) after field "Output1"
post "" to URL http://MyWebsite.com/ test-status.php
put it into ccMyWebsiteResponse
Put ((the milliseconds - ggStartMilliseconds) & " End Post MyWebsite " & Return) after field "Output1”
If ccMyWebsiteResponse = "Status:Received" then
Put "Status:OKMyWebsite" into ccMyWebsiteStatus
Else
Put ((the milliseconds - ggStartMilliseconds) & " Start Put Google " & Return) after field "Output1"
put URL "http://www.google.com" into ccConnCheck
Put ((the milliseconds - ggStartMilliseconds) & " End Put Google " & Return) after field "Output1"
if ccConnCheck is empty then
Put "Status:NoInternet" into ccMyWebsiteStatus
Else
Put "Status:NoMyWebsite" into ccMyWebsiteStatus
End if
End if
Put ((the milliseconds - ggStartMilliseconds) & " Returning Status : " & ccMyWebsiteStatus) after field "Output1"
Return ccMyWebsiteStatus
end CheckMyWebsiteAvailable

----------------------------------------------------------------

2) Tested, under a variety of coverage internet conditions, to see how long it took the function CheckMyWebsiteAvailable to return a result.

The results:

With the test device attached to an Ultra Fast broadband network - CheckMyWebsiteAvailable returns a result ("Status:OKMyWebsite") in between 300 and 900 milliseconds. All good.

With the test device attached to a 4G network - CheckMyWebsiteAvailable returns a result ("Status:OKMyWebsite") in between 500 and 1100 milliseconds. All good.

With the test device in flight mode - CheckMyWebsiteAvailable returns a result ("Status:NoInternet") in between 50 and 70 milliseconds. All good again.

With the test device "attached" to a 3G network with zero bars of coverage - CheckMyWebsiteAvailable returns a result ("Status:NoInternet") in between 45,000 and 90,000 milliseconds. That is 45 to 90 seconds that the user is looking at an un-responsive screen. I did eight test runs. Of this delay:
- The post "" to URL http://MyWebsite.com/test-status.php statement always took 5,000 milliseconds to return a result. I concluded that the post statement is being controlled by the socketTimeoutInterval statement.
- The put URL "http://www.google.com" into ccConnCheck statement took between 40,000 and 85,000 milliseconds to return a result.

I then went through and repeated the same experiment adding an On SocketTimeout (close the socket) handler to my stack. This didn't make any significant difference.

My options, as I see them:

1) Drop the idea of double testing - once to see if my website is contactable, and if it isn't then a second test to see whether the internet (using google as an indicator) is contactable - and only test whether my website is contactable.

2) Given that Post & socketTimeoutInterval seem to work fine as-is - find another website that is "up" a very large % of the time and that has PhP function that indicates it is contactable, and use a Post statement to this (instead of a Put statement to google) as an indicator of whether the internet itself is contactable.

3) Keep the put URL "http://www.google.com" approach, but find a way to send a kill function CheckMyWebsiteAvailable to me in 10 seconds command. I don't know if this is possible. I've tried a few things with the EXIT function. I find the dictionary ambiguous as to whether one handler can cause another to exit, but so far I have not managed to get it to work.

I don't see how any solution that involves testing a condition WITHIN the CheckMyWebsiteAvailable function is going to work - as the 40 to 85 second "pause" that I'm witnessing is within the execution of a single statement (put URL "http://www.google.com").

Am I missing something obvious?

Thanks in advance.