AS3 client for using AMF

Jan 18, 2010 at 11:47 AM
Edited Jan 18, 2010 at 11:51 AM

Hi

I want to contribute with an example for a AS3 client using Amf in WCF:

public class TestCall
    {
        private var _netConnection:NetConnection;
                       
        public function TestCall()
        {
             var remotingGateway:String  = "http://youradress";
             _netConnection = new NetConnection();

            //Use NetStatus event to handle an incorrect gateway URL.
            _netConnection.addEventListener(NetStatusEvent.NET_STATUS, connectionHandler);
            //Add url
            _netConnection.connect(remotingGateway);
            
            //Call parameters(if the metod takes parameters)
            var parameters:Array = new Array();
            parameters[0] = 1;
            parameters[1] = "Widget";          

             //If you want to use headers.
             var header:YourHeaderObject = new YourHeaderObject();
             YourHeaderObject.setSomeValue = "something";
            _netConnection.addHeader("YourHeader",false,YourHeaderObject);           
           
             //to handle incoming headers
            _netConnection.client = this;

            // the actual call
            _netConnection.call("YourMethod", new Responder(successfulResult, erroneousResult),parameters);
                        
        }
        
        /**
         * Outputs error if an invalid gateway is used.
         */
        function connectionHandler(ev:NetStatusEvent):void
        {
            if(ev.info.code == "NetConnection.Call.Failed")
            {
                trace("call failed");
            }
        }
        
        /**
         * Handle successful results
         */
        function successfulResult(obj:Object):void
        {
            trace("Call Successful");
            trace(parseObject(obj));                       
        }
       

        /*
         * Handle response header
         *
         * To handle an AMF context header a suitable method needs to
         * be available, matching the header name. NetConnection is now
         * a sealed type so either it must be subclassed or an object with
         * a suitable implementation needs to be set for the NetConnection
         * client property.
         *
         * for this to work the actual response header from your WCF server most have a name as ResponseHeader
         */
        public function ResponseHeader(obj:*):void
        {
            trace("Got header Successful");
            trace(parseObject(obj));                                
        }
                
        /**
         * Handle failed results
         */
        function erroneousResult(obj:Object):void
        {
            trace("Call Failed");
            trace(parseObject(obj));
        }                           

        // extract the data as object
        public function parseObject(obj:*):String
        {
            var result:String = "";
            
            for (var i : String in obj)
            {
                result += i + " = " + obj[i] + " [" + typeof(obj[i]) + "]\n";
                if (typeof(obj[i]) is Object)
                {
                    result += parseObject(obj[i]);
                }
            }
            
            return result;            
        }
    }

Hopes this example helps someone :-)

Coordinator
Jan 19, 2010 at 1:39 PM

Well, it certainly helps me.  With your sample, I think I'll be able to work out sample code like we have for AS2. 

Thanks for your code sample, sn1ckers!

Feb 15, 2010 at 5:54 PM

Hi sn1ckers, did you get working the headers functionality. If yes, could you post the server side code that you used to process them? Thanks in advance

Feb 17, 2010 at 9:11 AM
Edited Feb 17, 2010 at 9:12 AM

Hi I got it to work. But i posted the answer in wrong thread. See "Amf header discussion" :-)

 

Dec 29, 2010 at 4:01 PM
Edited Dec 30, 2010 at 1:42 PM

Very useful code above thank you!

And I wonder how I might extend this to send and receive complex types?
Or does it already?
And if so what does a typical [OperationContract] look like?

So far I got an Operation Contract like 

[OperationContract]  
string myMethod( string ID,   int Param1,  int Param2,  int Param3  );
to work.

What I want to be able to do is return a complexType as the return value when calling the WCF method

[OperationContract]  
complexType myMethod( string ID,   int Param1,  int Param2,  int Param3  ); 

What I mean is, how do I return a complex type to the Flash client on the Flash client side.
How do I code the Flash side of this. (I can do the WCF side). 

Or does the "parseObject" function just handle this?