1. global gCurl
  2.  
  3. ----------------------------------------
  4. --
  5. ----------------------------------------
  6. on startMovie
  7.   -- create a single global instance of the curl manager
  8.   gCurl = script("CurlManager").new()
  9.  
  10.   -- test the curl manager
  11.   test1()
  12.   test2()
  13. end
  14.  
  15. ----------------------------------------
  16. -- test 2 concurrent HTTP GET requests
  17. ----------------------------------------
  18. on test1
  19.   getNetTextCurlAsync("https://dasdeck.com", #curlGetRequestCallback)
  20.   getNetTextCurlAsync("https://valentin.dasdeck.com", #curlGetRequestCallback)
  21. end
  22.  
  23. ----------------------------------------
  24. -- test 2 concurrent HTTP downloads
  25. ----------------------------------------
  26. on test2
  27.   downloadNetThingCurlAsync("https://valentin.dasdeck.com/tmp/test.mp3", _movie.path&"test_a.mp3", #curlDownloadCallback)
  28.   downloadNetThingCurlAsync("https://valentin.dasdeck.com/tmp/test.mp3", _movie.path&"test_b.mp3", #curlDownloadCallback)
  29. end
  30.  
  31. ----------------------------------------
  32. --
  33. ----------------------------------------
  34. on getNetTextCurlAsync (tUrl, tCallbackHandler, tCallbackTarget)
  35.   if voidP(tCallbackTarget) then tCallbackTarget = _movie
  36.   ch = gCurl.init()
  37.   ch.setOption(gCurl.CURLOPT.SSL_VERIFYPEER, FALSE)
  38.   ch.setOption(gCurl.CURLOPT.URL, tUrl)
  39.   gCurl.execAsync(ch, tCallbackHandler, tCallbackTarget, 1)
  40. end
  41.  
  42. ----------------------------------------
  43. -- @callback
  44. ----------------------------------------
  45. on curlGetRequestCallback (res)
  46.   if integerP(res) then
  47.     put "An error occurred:" && curl_error(res)
  48.   else
  49.     put "Result:" && res.readRawString(res.length)
  50.   end if
  51. end
  52.  
  53. ----------------------------------------
  54. --
  55. ----------------------------------------
  56. on downloadNetThingCurlAsync (tUrl, tLocalFile, tCallbackHandler, tCallbackTarget)
  57.   if voidP(tCallbackTarget) then tCallbackTarget = _movie
  58.   ch = gCurl.init()
  59.   ch.setOption(gCurl.CURLOPT.SSL_VERIFYPEER, FALSE)
  60.   ch.setOption(gCurl.CURLOPT.URL, tUrl)
  61.   ch.setDestinationFile(tLocalFile)
  62.   gCurl.execAsync(ch, tCallbackHandler, tCallbackTarget, 0, tLocalFile)
  63. end
  64.  
  65. ----------------------------------------
  66. -- @callback
  67. ----------------------------------------
  68. on curlDownloadCallback (res, ch, localFile)
  69.   if res=0 then
  70.     put "File was successfully downloaded to" && localFile
  71.   else
  72.     put "An error occurred:" && curl_error(res)
  73.    
  74.     -- since an async download creates a local file immediately,
  75.     -- in case of error try to delete erroneous 0 byte file
  76.     fp = xtra("fileIO").new()
  77.     fp.openFile(localFile, 0)
  78.     fp.delete()
  79.    
  80.   end if
  81. end
  82.  
[raw code]