Advertisement
Guest User

URLLoader gets stuck when polling

a guest
Feb 26th, 2012
35
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.07 KB | None | 0 0
  1. private function check_server() {
  2. var url:String = "http://coworkthailand.com/say/snd/index.php?"+Math.random();
  3. if (loader != null) {
  4. trace("was already checking "+loader.bytesLoaded+" / "+loader.bytesTotal);
  5. return;
  6. }
  7. loader = new URLLoader();
  8. loader.dataFormat = URLLoaderDataFormat.TEXT;
  9. loader.addEventListener(Event.COMPLETE, completeHandler);
  10. loader.addEventListener(flash.events.IOErrorEvent.IO_ERROR,
  11. function(e:Event) { loader = null; trace("fail"); })
  12. loader.addEventListener(flash.events.SecurityErrorEvent.SECURITY_ERROR,
  13. function(e:Event) { loader = null; trace("security error"); })
  14. loader.addEventListener(flash.events.HTTPStatusEvent.HTTP_STATUS,
  15. function(e:flash.events.HTTPStatusEvent) { trace("status "+e.status); });
  16. try {
  17. loader.load(new URLRequest(url));
  18. } catch (error:Error) {
  19. trace("Unable to load requested document.");
  20. }
  21. }
  22.  
  23. <?xml version="1.0" encoding="utf-8"?>
  24. <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" initialize="app_init()">
  25. <mx:Script>
  26. <![CDATA[
  27.  
  28. private var timer:Timer;
  29. private var loader:URLLoader;
  30. private var isOpen:Boolean;
  31.  
  32. private function app_init():void
  33. {
  34. timer = new Timer(5000)
  35. timer.addEventListener(TimerEvent.TIMER, timer_tick, false, 0, true)
  36.  
  37. loader = new URLLoader();
  38. loader.addEventListener(Event.OPEN, loader_open);
  39. loader.addEventListener(Event.COMPLETE, loader_complete);
  40.  
  41. // Start the timer
  42. timer.start();
  43. }
  44.  
  45. private function timer_tick(event:TimerEvent):void
  46. {
  47. // Check if the loader's busy before calling load, and/or close()
  48. if (!isOpen)
  49. {
  50. // loader.close();
  51. loader.load(new URLRequest("http://mydomain.com/myfile.txt"));
  52. }
  53. }
  54.  
  55. private function loader_open(event:Event):void
  56. {
  57. // Mark as open
  58. isOpen = true;
  59. }
  60.  
  61. private function loader_complete(event:Event):void
  62. {
  63. // Do work
  64.  
  65. // Mark as closed
  66. isOpen = false;
  67. }
  68.  
  69. ]]>
  70. </mx:Script>
  71. </mx:Application>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement