1 Reply Latest reply on Sep 10, 2010 10:14 AM by Digital.Smith

    RemoteObject Scope Issue

    Digital.Smith

      I posted this in the ColdFusion forums earlier, but figured it might actually be better here in the Flex forums, so sorry for the repost.

       

      I am trying to create a backend for an AIR application. The application is a poker site. I can return objects from ColdFusion to AIR just fine, but the problem is, everytime I make a call to a function in my CFC, it acts as if each call is a seperate "instance" of the CFC.

       

      Below is the CFC I am using...

       

       

      <cfcomponent output="false" name="Deck">

       

      <cfset suits = ["hearts", "diamonds", "spades", "clubs"]>

      <cfset cards = ["A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"]>

      <cfset names = ["Ace", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King"]>

      <cfset deck = ArrayNew(1)>

       

       

      <cffunction name="CreateDeck" access="remote">

      <cfscript>ArrayClear(deck);</cfscript>

       

      <cfset cardsLength = ArrayLen(cards)/>

      <cfset suitsLength = ArrayLen(suits)/>

       

      <cfloop index="i" from="1" to=#suitsLength#>

      <cfloop index="j" from="1" to=#cardsLength#>

      <cfscript>

      ArrayAppend(deck, [cards[j],suits[i]]);

      </cfscript>

      </cfloop>

      </cfloop>

       

      <cfscript>ShuffleDeck();</cfscript>

      <cfreturn deck />

      </cffunction>

       

       

      <cffunction name="ShuffleDeck">

      <cfset CreateObject("java","java.util.Collections").Shuffle(deck)/>

      </cffunction>

       

       

      <cffunction name="DealCard" access="remote">

      <cfset randomNumber = RandRange(1, ArrayLen(deck), "SHA1PRNG")/>

      <cfreturn ArrayLen(deck)/>

      </cffunction>

       

      </cfcomponent>

       

       

      Below is a sample of the Flex code I am using to call the CFC...

       

       

      <s:WindowedApplication xmlns:fx="http://ns.adobe.com/mxml/2009"

        xmlns:s="library://ns.adobe.com/flex/spark"

        xmlns:mx="library://ns.adobe.com/flex/mx"

        creationComplete="initApp()">

       

      <fx:Declarations>

      <!-- Place non-visual elements (e.g., services, value objects) here -->

      <s:RemoteObject id="svc" destination="ColdFusion" endpoint="http://localhost:8500/flex2gateway/" source="PokerSite.cfc.Deck">

      <s:method name="CreateDeck" result="resultHandler(event)" fault="faultHandler(event)"/>

      <s:method name="DealCard" result="resultHandler(event)" fault="faultHandler(event)"/>

      </s:RemoteObject>

      </fx:Declarations>

       

       

      <fx:Script>

      <![CDATA[

      import mx.rpc.events.FaultEvent;

      import mx.rpc.events.ResultEvent;

       

      private function initApp():void

      {

      svc.CreateDeck();

      }

       

      private function doDeal():void

      {

      svc.DealCard();

      }

       

      private function resultHandler(e:ResultEvent):void

      {

      trace(e.result);

      }

       

      private function faultHandler(e:FaultEvent):void

      {

      trace(e.fault);

      }

      ]]>

      </fx:Script>

       

       

      <s:Panel x="0" y="0" width="250" height="200">

      <s:Button click="doDeal()" label="Deal"/>

      </s:Panel>

       

      </s:WindowedApplication>

       

       

      When I call the CreateDeck() function of the CFC, it returns the entire deck just fine. However, when I then call the DealCard() function by clicking the button, it returns a length of 0 of the deck object in the CFC (just returning the length to test that the deck has been created for now) as if the deck was never created. I did some searching online, and found that I might have to setup the remoting-config.xml file to allow application level scope. I then setup the remoting-config.xml like so...

       

       

      <destination id="ColdFusion">

      <channels>

      <channel ref="my-cfamf"/>

      </channels>

      <properties>

      <source>*</source>

      <scope>application</scope>

      </properties>

      </destination>

       

       

      Yet, this still fails the same way. Does anyone know if I am missing a simple step? Or setting up the application scope in the wrong place? It definitely sounds like the problem I am having, but when I change that remoting-config.xml file, it doesn't seem to change anything in the way the functions are called.