Posted By

reboltutorial on 12/11/10


Tagged


Versions (?)

test of async


 / Published in: R
 

URL: http://rebol.wik.is/Protocols/Test-async-http.r

  1. REBOL [
  2. file: %test-async-http.r
  3. author: "Graham"
  4. email: [ compkarori<@>gmail.com ]
  5. date: 10-June-2010
  6. ]
  7.  
  8. do %async-protocol.r
  9. do %async-http.r
  10.  
  11. buffer: copy ""
  12. content-length: 0
  13.  
  14. handler: func [port [port!] event [error! word!]
  15. ] [
  16. switch event [
  17. connect [print "Connected." false]
  18. read [
  19. if port/sub-port/state/inbuffer [
  20. statustxt/text: form length? port/sub-port/state/inbuffer
  21. show statustxt
  22. if zero? content-length [
  23. either parse/all port/sub-port/state/inbuffer [ thru "content-length: " copy content-length to newline to end ][
  24. if content-length [
  25. content-length: to-integer trim/all content-length
  26. ]
  27. ][ content-length: none ]
  28. ]
  29. if all [ content-length content-length > 0 ][
  30. prog/data: ( to-integer statustxt/text ) / content-length
  31. show prog
  32. ]
  33. ]
  34. false
  35. ]
  36. write [false]
  37. close [print "Peer closed connection." append buffer copy port
  38. close port
  39. write/binary last split-path to-url urlfld/text buffer
  40. false ; if you use true here, you'll exit the View event handler as well
  41. ]
  42. ]
  43. ]
  44.  
  45. do-download: has [url link ][
  46. if not empty? url: urlfld/text [
  47. if parse/all url [ "http" opt "s" "://" to end][
  48. insert head url 'a
  49. link: open to-url url
  50. link/awake: :handler
  51. ]
  52. ]
  53. ]
  54.  
  55.  
  56. view/new center-face layout [
  57. across
  58. label "URL: " gold urlfld: field 300 return
  59. prog: progress 340x15 return
  60. btn "Download" [ do-download ] return
  61. label "You can type while downloading" black return
  62. area 340x200 wrap
  63. at 160x50 statustxt: text "bytes" 100 white
  64. ]
  65.  
  66. wait []

Report this snippet  

You need to login to post a comment.