1. --!movie
  2. --!encoding=utf-8
  3.  
  4. global $
  5.  
  6. ----------------------------------------
  7. -- Asynchronous or non-blocking mode, using callbacks
  8. ----------------------------------------
  9. on startMovie
  10.     _player.debugPlaybackEnabled = 1
  11.  
  12.     -- libs
  13.     $.import("curl")
  14.  
  15.     --------------------------------------------------
  16.     -- Curl connection 1: DOWNLOAD
  17.     --------------------------------------------------
  18.     _global.ch1 = $.curl.init() -- important for async mode: make CURL handle persistant!
  19.  
  20.     -- specify options
  21.     _global.ch1.setOption($.curl.CURLOPT.URL, "http://valentin.dasdeck.com/xtras/curl_xtra/.test/test.mp3")
  22.  
  23.     _global.ch1.setDestinationFile($.PATH & "downloaded.mp3")
  24.  
  25.     -- returnMode: 0=return error code (=default), 1=return data, 2=return chunks immediately
  26.  
  27.     -- A) Use async xtra function directly. Drawback: lingo errors in callback code fail silently
  28.     -- _global.ch1.execAsync(#slotCurlResult1)
  29.  
  30.     -- B) Use helper function to "detach" callbacks from original thread, so errors are shown and can be debugged!
  31.     $.curl.execAsyncDetached(_global.ch1, #slotCurlResult1)
  32.  
  33.     --------------------------------------------------
  34.     -- Curl connection 2: GET (ERROR)
  35.     --------------------------------------------------
  36.     _global.ch2 = $.curl.init() -- important for async mode: make CURL handle persistant!
  37.  
  38.     -- specify options
  39.     _global.ch2.setOption($.curl.CURLOPT.URL, "http://doesntexist.xxxrtzr.com/") -- test network error handling
  40.  
  41.     $.curl.execAsyncDetached(_global.ch2, #slotCurlResult2)
  42.  
  43.     --------------------------------------------------
  44.     -- Curl connection 3: POST
  45.     --------------------------------------------------
  46.     _global.ch3 = $.curl.init() -- important for async mode: make CURL handle persistant!
  47.  
  48.     -- specify options
  49.     _global.ch3.setOption($.curl.CURLOPT.URL, "http://valentin.dasdeck.com/xtras/curl_xtra/.test/echo.php")
  50.     _global.ch3.setOption($.curl.CURLOPT.POSTFIELDS, "foo=äöü&bar=hello%20world")
  51.  
  52.     $.curl.execAsyncDetached(_global.ch3, #slotCurlResult3)
  53.  
  54.     put "CONTINUE LINGO EXECTION..."
  55. end
  56.  
  57. ----------------------------------------
  58. -- @callback
  59. ----------------------------------------
  60. on slotCurlResult1 (res)
  61.     -- x = 1/0 -- uncomment to check if errors fail silently or not
  62.     put
  63.     put "--------------------------------------------------"
  64.     put " CALLBACK FOR CURL CONNECTION 1"
  65.     put "--------------------------------------------------"
  66.     if integerP(res) then
  67.         put "ERROR:" && curl_error(res)
  68.     else
  69.         put "RESULT:" && res.readRawString(res.length)
  70.     end if
  71. end
  72.  
  73. ----------------------------------------
  74. -- @callback
  75. ----------------------------------------
  76. on slotCurlResult2 (res)
  77.     put
  78.     put "--------------------------------------------------"
  79.     put "CALLBACK FOR CURL CONNECTION 2"
  80.     put "--------------------------------------------------"
  81.     if integerP(res) then
  82.         put "ERROR:" && curl_error(res)
  83.     else
  84.         put "RESULT:" && res.readRawString(res.length)
  85.     end if
  86. end
  87.  
  88. ----------------------------------------
  89. -- @callback
  90. ----------------------------------------
  91. on slotCurlResult3 (res)
  92.     put
  93.     put "--------------------------------------------------"
  94.     put "CALLBACK FOR CURL CONNECTION 3"
  95.     put "--------------------------------------------------"
  96.     if integerP(res) then
  97.         put "ERROR:" && curl_error(res)
  98.     else
  99.         put "RESULT:" && res.readRawString(res.length)
  100.     end if
  101. end
[raw code]