Hello flash remoting

here is an example creating data from a cfc into flash using remoting.

create a text field on the stage, this is where you will display your information. It also uses the trace window.

Actionscript 3.0


import flash.net.*;

var gateway:NetConnection = new NetConnection();

var responder:Responder=new Responder(onResult,onFault);



unction onResult(responds:Object):void {

	trace("cfc result"+":"+" "+responds.toString());

	runtimeOutput.text = responds.toString();

}



function onFault(responds:Object):void {

	//loop over the fault structure

	for (var i:String in responds) {

		runtimeOutput.text = String(responds);

		trace( i + ":" + responds[i] );

	}

}





gateway.connect("http://localhost/flashservices/gateway/");





gateway.call("com.HelloWorld.sayHello", responder);





The ColdFusion


<cfcomponent>

<cfset TheTime = Now()>

     <cffunction name="sayHello" returntype="string" access="remote">

          <cfreturn "Hello from the ColdFusion Component, the time is currently #TimeFormat(TheTime, 'short')#" />

     </cffunction>

</cfcomponent>

the final output

Next time I will show an example using database results. I am having issues getting around the fact that there is no longer a recordset in AS 3:( I think that I can just create a for loop to deal with the query results, but I will blog about it in a few days.

Setting up Remoting with Flex & ColdFusion

To get Coldfusion and flash to talk to each other is just as easy in Flex as it is in flash. Remember Flex is just the tool, and nothing more. When you compile flex you are just created a swf! To start this out I am just setting up the remoting call, later I will write a full app, which I have been meaning to do, but I have been too busy with school lately. You can check out flexcf Which as so many great examples of using this technology. It is run by Paul who has helped me a great deal in CF in the past! You can check out his blog too.

The mxml file


<?xml version="1.0" encoding="utf-8"?>

<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" creationComplete="initApp()" layout="absolute" pageTitle="Remoting Example" backgroundColor="White">

    <mx:Script>

        <![CDATA[

            import mx.rpc.events.ResultEvent;

            import mx.utils.ObjectUtil;

            import mx.controls.Alert;



            [Bindable]

            public var cfcResponse:String;



            //this gets called when the application is done being created

            public function initApp():void{

                //this calls the confirmCFC method in remoting.cfc

                cfc_ro.confirmCFC();

            }



            public function loadConfirm(event:ResultEvent):void{

                //this binds the cfcResponse var to the result of the RO call

                cfcResponse = event.result as String;

            }



        ]]>

    </mx:Script>



    <mx:RemoteObject id="cfc_ro" destination="ColdFusion" source="com.flex.remoting" showBusyCursor="true">

        <mx:method name="confirmCFC" result="loadConfirm(event)" fault="Alert.show(ObjectUtil.toString(event))"/>

    </mx:RemoteObject>

    <mx:Label text="{cfcResponse}" width="100%" />



</mx:Application>

the cfc


<cfcomponent output="false">



	<cffunction name="confirmCFC" returntype="any" access="remote">

		<cfreturn "This is a test to see if Flex is conecting to ColdFusion, from the file remoting.cfc">

	</cffunction>



</cfcomponent>

Setting up Flash Remoting with ColdFusion

I would like very much to thank Eric Meyer from curious minds

for helping me figure this out`-` I had started to try to convert a actionscript 2 remoting app to actionscript 3, but got really confused towards the end. I saw a post on his blog, and he was nice enough to work with me over e-mail fixing all my confusion!

Check out his blog, lots of great information.

Setting up flash remoting with actionscript 3.0 and flash CS 3.
Create a fla, call it remoting(or something that makes sense to you).

In the actions, type the following code


import flash.net.*;
var gateway:NetConnection = new NetConnection();

var responder:Responder=new Responder(onResult,onFault);





function onResult(responds:Object):void {

	trace("cfc result"+":"+" "+responds.toString());

}



function onFault(responds:Object):void {

	//loop over the fault structure

	for (var i:String in responds) {

		trace( i + ":" + responds[i] );



	}

}



// name the default cf test server on this machine

gateway.connect("http://localhost/flashservices/gateway/");



// name of the cfc and the function in it that you want to hit in dot notation

gateway.call("com.cfcs.getTest.getTestConn", responder/*this is where any arguments to the remote call go*/);



The cfc lives on my localhost/com/cfcs/gettestConn.cfc

The cfc will look like this


<cfcomponent displayName="getTest">

  <!--- Establish a Flash Remoting Connection --->

  <cffunction name="getTestConn" access="remote" returnType="string" output="true">

    <cfreturn "....connection successful">

   </cffunction>

 </cfcomponent>

This is just the set up, and yep just returns the string from the cfc. Once set up, you can use remoting the way that it is meant to be used`-`

Any question, problems? Let me know I will try to help.

I will posting another example on how to display the results from your cfc, and later how to pull database results into your flash movie as well.

happy coding`-`