Twitter: raymondcamden


Address: Lafayette, LA, USA

Flex Mobile and ColdFusion Sessions

07-28-2011 4,665 views Mobile, Flex, ColdFusion Comments

It just works. Thanks. Bye.

Ok - maybe a little more detail is in order? ;) I knew that Flex on the desktop, when making calls to CFC, kept a session just like any other normal HTTP request. I was also mostly certain that the same held true for Flex Mobile. I decided to verify it just to be sure though. I created an incredibly simple Flex Mobile project that pings a ColdFusion service. Here is the view:

view plain print about
1<?xml version="1.0" encoding="utf-8"?>
2<s:View xmlns:fx="http://ns.adobe.com/mxml/2009"
3                 xmlns:s="library://ns.adobe.com/flex/spark" title="HomeView">

4    
5    <fx:Declarations>
6        <s:RemoteObject id="testService" destination="ColdFusion" source="demos.july282011.remote" endpoint="http://www.coldfusionjedi.com/flex2gateway/" />            
7    </fx:Declarations>
8
9    <s:layout>
10        <s:VerticalLayout/>
11    </s:layout>
12
13    <s:Button label="Test" click="testService.getKey()" />
14    <s:Label id="resultLabel" text="{testService.getKey.lastResult}" />
15
16</s:View>

For those of you who have never seen a lick of Flex code, you can probably guess as to what this code is doing. I've defined a CFC service that points to my blog. I've got a button that when clicked will run getKey. getKey is a ColdFusion component method. I've taken the label field below and bound it to the last result. That's a built in feature that allows me to quickly say, "Just take the last result and drop it here." Normally you're going to use an event handler for the result but for this simple test it works well enough. Now let's look at the code. First, my Application.cfc:

view plain print about
1component {
2    this.name="flexremotetest1";
3    this.sessionManagement="true";
4    
5    public void function onSessionStart() {
6        session.mykey = randRange(1,9999);
7    }
8    
9}

Nothing too interesting there - just a session start event handler that assigns a random number to the session scope. Now let's look at my CFC:

view plain print about
1component {
2
3    remote string function getKey() {
4        return session.mykey & " - " & session.urltoken;
5    }
6
7}

I've got one method, getKey, that returns the key, and just to be extra sure, the built in URL token for the session. The result is - as expected - a random number (with the urltoken):

I tested it in a few devices just to be extra sure, and as expected, they each had their own sessions. Too easy.

0 Comments

Post Reply

Please refrain from posting large blocks of code as a comment. Use Pastebin or Gists instead. Text wrapped in asterisks (*) will be bold and text wrapped in underscores (_) will be italicized.

Leave this field empty