Event.COMPLETE doesn't fire...

Monkey Targets Forums/Flash/Event.COMPLETE doesn't fire...

Xaron(Posted 2011) [#1]
Hi all,

I try to expand my MNet module to flash but I can't get it to work because no event is fired at all...

The related HTML5 version works fine (async both).

Here's the native code for flash (mnet.flash.as):
class MNet
{
  public function HttpGet( url:String, timeoutConnection:int ):void
  {
    var request:URLRequest = new URLRequest();
    request.url = url;
    request.method = URLRequestMethod.GET;
    var loader:URLLoader = new URLLoader();
    loader.dataFormat = URLLoaderDataFormat.TEXT;
    loader.addEventListener( Event.OPEN,             onConnectionEstablished );
    loader.addEventListener( ProgressEvent.PROGRESS, onRequestProcessing );
    loader.addEventListener( Event.COMPLETE,         onCompleted );
    loader.addEventListener( IOErrorEvent.IO_ERROR,  onIOError );
    try
    {
      loader.load( request );
    }
    catch( error:Error )
    {
      trace( "Unable to load URL: " + error );
    }
    print(url);
  }

  public function onConnectionEstablished():void
  {
    print("onConnectionEstablished");
  }

  public function onRequestReceived():void
  {
    print("onRequestReceived");
  }

  public function onRequestProcessing():void
  {
    print("onRequestProcessing");
  }

  public function onCompleted( e:Event ):void
  {
    print("onCompleted");
    onRequestComplete( e.target.data );
  }

  public function onIOError():void
  {
    print("onIOError");
  }

  public function onRequestComplete( response:String ):void
  {
    print("onRequestComplete");
  }
}


Monkey interface:
Strict

Extern

Class MNet = "MNet"
  Method HttpGet:Void( url:String, timeoutConnection:Int = 5000 )
  Method onConnectionEstablished:Void() Abstract
  Method onRequestProcessing:Void() Abstract
  Method onIOError:Void() Abstract
  Method onRequestComplete:Void( response:String ) Abstract
  Method onRequestReceived:Void() Abstract
End Class

Public

Class AsyncHttpGet Extends MNet
  Method New()
  End Method

  Method onConnectionEstablished:Void()
    Print "Connection established"
  End Method
  
  Method onRequestProcessing:Void()
    Print "Processing request"
  End Method
  
  Method onIOError:Void()
    Print "Error"
  End Method

  Method onRequestComplete:Void( response:String )
    Print "Request complete"
  End Method	

  Method onRequestReceived:Void()
    Print "Request received"
  End Method
End Class


And finally my test code:
Strict

Import mojo
Import mnet
Global testApp:HttpGetTest

Class MyHttpGet Extends AsyncHttpGet
  Field result:String

  Method onRequestComplete:Void( response:String )
    Print "R: " + response
    Self.result = response
  End Method
  
  Method getResult:String()
    Return result
  End Method
End Class

Function Main:Int()
  testApp = New HttpGetTest()
  Return 0
End Function

Class HttpGetTest Extends App
  Field myhttp:MyHttpGet

  Method OnCreate:Int()
    SetUpdateRate(30)
    myhttp = New MyHttpGet()
    myhttp.HttpGet( "http://www.hexbattles.com/test/test.php" )
    Return 0
  End Method

  Method OnUpdate:Int()
    Return 0
  End Method
  
  Method OnSuspend:Int()
    Return 0
  End Method
  
  Method OnResume:Int()
    Return 0
  End Method

  Method OnRender:Int()
    Cls
    DrawText( "Result: " + myhttp.getResult(), 10, 10 )
    Return 0
  End Method

End Class


I think the bug is in the flash native code. The function HttpGet is called correctly, it prints out the URL at the bottom but never gets into any of the event listeners...


Rixarn(Posted 2011) [#2]
Wow, i was struggling with the same thing for hours.

I'm too having this problem... I second you on this.


slenkar(Posted 2011) [#3]
You might be using a weak reference,
make loader and request fields in the Mnet object so they are not garbage collected.
A lot of people have problems with this in forums.

If it isnt that try this:


http://www.actionscript.org/forums/showthread.php3?t=126665

if it still doesnt work, make sure you are not loading info from a server that the SWF is not on, because then you would need a crossdomain.xml file on the remote server.

another thing to try:
http://stackoverflow.com/questions/603941/why-is-my-urlloader-not-dispatching-when-it-completes


Xaron(Posted 2011) [#4]
Thanks slenkar,

I already tried that with the weak reference even though it shouldn't make any difference because it's set to false for addEventListener so the GC doesn't remove it.

Thanks for the link as well but this is for the Loader class not the URLLoader class. For the Loader you have to add the contentLoaderInfo...

Part III might be the solution, don't know yet. I just want to load text from a remote server (just the URL). Do you mean with "server that the SWF is not on" the local flash file? I just start it from Monkey and of course it's started locally and should get updates there?

edit: Regarding your second link, I already use URLLoaderDataFormat.TEXT. ;)

I will create some simple test project in both pure native Actionscript and combined with Monkey...


Xaron(Posted 2011) [#5]
And here's the test project:

http://www.xaron.net/dl/monkey/mnet_async.zip

Copy the folder "mnet" into the modules folder and start the httpget.monkey file within the examples folder. HTML5 works fine async now but not flash.

Will add async support meanwhile for iOS and Android...


slenkar(Posted 2011) [#6]
Part III might be the solution, don't know yet. I just want to load text from a remote server (just the URL). Do you mean with "server that the SWF is not on" the local flash file? I just start it from Monkey and of course it's started locally and should get updates there?



paste this into a text file
<?xml version="1.0" ?>
<cross-domain-policy>
<site-control permitted-cross-domain-policies="master-only"/>
<allow-access-from domain="*"/>
<allow-http-request-headers-from domain="*" headers="*"/>
</cross-domain-policy>

save as xml

put the xml file on the root of the remote server

alternatively upload the SWF to the server and run it in a webpage


Xaron(Posted 2011) [#7]
Alright... If I put it in the same webspace where my php file is located it works. :)

I still understand this xml stuff though. What do you mean with remote server? I should be able to run the swf locally or at any place and it should be able to access any url?!

So you mean I should put this xml file onto the webspace where my php file is located? That doesn't make sense to me, I guess I totally misunderstand you here.


slenkar(Posted 2011) [#8]
the remote server is www.xaron.net
you should paste the above code into a textfile and then rename it as crossdomain.xml

then simply upload the xml file to xaron.net (root html folder)

then it should work so that you can you run the SWF from anywhere
even on your pc's hard drive.

These are security measures so a random swf cant access your database.

you can get flash to tell you what went wrong:
http://injun.ru/flash10api/flash/events/SecurityErrorEvent.html


Xaron(Posted 2011) [#9]
Thanks so much slenkar, will try this tomorrow!


slenkar(Posted 2011) [#10]
youre welcome,

Im trying to use the example but nothing happens, even with the html5 version

am I doing something wrong?

in firefox I get :
Connection established
Request received

but nothing in chrome


Xaron(Posted 2011) [#11]
HTML5 only works with IE9 due to reasons beyond my imagination.


slenkar(Posted 2011) [#12]
the flash one works, good job