This is the only code in the module (main.agc). You should just have to copy and paste it.
I discovered that AppGameKit returns a zero-size file when you attempt to download a file from the web with no internet connection. This can be used to determine whether or not a user is connected to the internet.
Secondly, if the app starts downloading, it will keep downloading until the file is downloaded -
even if the internet connection is dropped during the download. By using this, if AppGameKit says the file is done downloading, you're good to go. Otherwise, count how long it takes. This code times out after 25 seconds, and says that the connection was lost (and deletes the partially downloaded file).
To test this out
1) run the app with full internet connection, wait to finish
2) run app again without any internet connection, wait to finish
3) run app again and kill the internet connection a few seconds after the app starts
You'll get all three outcomes. The only sizable file I had on my webserver was my Water app, and it's 20MB. This is a decent sized file that allows you to accurately test all three scenarios above (it takes about 20 seconds to download onto my PC at least).
The code isn't the best, but it's the best working sample I found so far on here that provides basic error-checking on any downloaded file!
-Jeff
rem downloadprogress contains a variable of the download status
rem 0=active, 1='finished', -1 = timeout
downloadProgress as integer
rem our variable to store the time taken to download the file
downloadTime as float
rem set it so that we're downloading a file and time is at zero
downloadTime = 0
downloadProgress = 0
rem set our timeout variable (in seconds)
timeoutLength as float
timeoutLength = 25
rem create httpConnection so that we can go download from the host
myHttpConnection as integer
myHttpConnection = CreateHttpConnection()
rem connect to the host
SetHttpHost(myHttpConnection, "www.playbukketgames.com", 0)
rem delete any remanents of the file that we already might have in place
deletefile("downloads/WaterPC_105.exe")
rem delcare variable to hold the downloaded process ID
downloadFileID as integer
downloadFileID = GetHTTPFile(myHttpConnection, "/games/WaterPC_105.exe", "downloads/WaterPC_105.exe")
rem system loop
do
rem only go through this if the file is currently being downloaded, e.g., not complete or timed out
if downloadProgress = 0
rem verify that the file hasn't completely arrived yet
if GetHTTPFileComplete(myHttpConnection) = 0
rem increase downloadtime variable in case we timeout
downloadTime = downloadTime + GetFrameTime()
rem verify that we haven't timed out
if downloadTime >= timeoutLength
rem cleanup
downloadTime = 0
rem set progress var to -1, that we timedout.
downloadProgress = -1
endif
else
rem the file is downloaded, or we have no connection to download with (zero-length file will result)
downloadTime = 0
downloadProgress = 1
endif
endif
print("Download Time: " + str(downloadTime))
Sync()
rem exit loop if the file as we know it is completed
if downloadProgress > 0 or downloadProgress < 0
exit
endif
loop
rem close the http connection since we don't need it anymore
closehttpconnection(myHttpConnection)
rem declare the fileID used to reference the file we just downloaded
openFileID as integer
rem verify the file downloaded correctly
if downloadProgress = 1
rem open file to read
openFileID = opentoread("downloads/WaterPC_105.exe")
rem if the file size is greater than zero, it downloaded correctly
if getfilesize(openFileID) > 0
message("download completed-the file is ready to use")
else
rem if not, it is an empty file and failed
message("download failed-do you have an active internet connection?")
rem delete the zero length file
deletefile("downloads/WaterPC_105.exe")
endif
closefile(openFileID)
else
rem if the download is interrupted while downloading, it will time out
message("download failed-did you lose your internet connection?")
rem kill the file since we don't need it
deletefile("downloads/WaterPC_105.exe")
endif
end
Hi there. My name is Dug. I have just met you, and I love you.