Hello
I was originally using the code below to download files for my app, the download would start, get the file and continue on.
[lua]local http = require(“socket.http”)
local ltn12 = require(“ltn12”)
local path = system.pathForFile( “truth.db”, system.DocumentsDirectory )
local myFile = io.open( path, “w+b” )
http.request{ url = “https://ssl.domain-name-removed.com/app-name/4/truth.db”, sink = ltn12.sink.file(myFile),}[/lua]
I decided to upgrade to SSL and make all communications secure (API keys etc in post data) but this broke socket.http.
I upgraded to Async network.download as it had no troubles with SSL. I am using this code.
[lua]local function networkListener( event )
if ( event.isError ) then
print( “Network error!”)
elseif ( event.phase == “began” ) then
print(“3”)
if event.bytesEstimated <= 0 then
print( “Download starting, size unknown” )
else
print( "Download starting, estimated size: " … event.bytesEstimated )
end
elseif ( event.phase == “progress” ) then
print(“4”)
if event.bytesEstimated <= 0 then
print( "Download progress: " … event.bytesTransferred )
else
print( "Download progress: " … event.bytesTransferred … " of estimated: " … event.bytesEstimated )
end
elseif ( event.phase == “ended” ) then
print( “Download complete (” … event.response.filename … "), total bytes transferred: " … event.bytesTransferred )
print(“5”)
bIsDownloading = false
end
end
print(“1”)
local params = {}
params.progress = “download”
params.response = {filename = “helloworld.png”,baseDirectory = system.DocumentsDirectory}
print(“2”)
bIsDownloading = true
network.request( “https://ssl.domain-name-removed.com/app-name/4/helloworld.png”, “GET”, networkListener, params)
print(“6”)[/lua]
This downloads the file ok but because it is Async the code moves on and out of my GetFile function before the file has downloaded?
See Output from code above (note “6” gets fired before step 3,4,5).
2013-09-03 06:18:59.213 Corona Simulator[6345:707] 1
2013-09-03 06:18:59.213 Corona Simulator[6345:707] 2
2013-09-03 06:18:59.213 Corona Simulator[6345:707] 6
2013-09-03 06:19:00.910 Corona Simulator[6345:707] 3
2013-09-03 06:19:00.911 Corona Simulator[6345:707] Download starting, estimated size: 2796
2013-09-03 06:19:00.911 Corona Simulator[6345:707] 4
2013-09-03 06:19:00.912 Corona Simulator[6345:707] Download progress: 2796 of estimated: 2796
2013-09-03 06:19:00.913 Corona Simulator[6345:707] Download complete (corona.jpg), total bytes transferred: 2796
2013-09-03 06:19:00.913 Corona Simulator[6345:707] 5
How should I download the file file but pause the code from moving on until the file has downloaded.
Should I create a local var called bIsBusyDownloading and create a loop after the download is triggered and check to see when the file has been downloaded?
If should I not use Async for SSL? Is there a simpler way to download over SSL?
I only need one file downloaded at time.
Thanks In Advance