<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Thinking in Code &#187; consumer</title>
	<atom:link href="http://www.michelboudreau.com/tags/consumer/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.michelboudreau.com</link>
	<description>Michel Boudreau&#039;s Personal Blog</description>
	<lastBuildDate>Wed, 25 Apr 2012 20:18:24 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<item>
		<title>Using BlazeDS with AS2</title>
		<link>http://www.michelboudreau.com/2009/03/18/using-blazeds-with-as2/</link>
		<comments>http://www.michelboudreau.com/2009/03/18/using-blazeds-with-as2/#comments</comments>
		<pubDate>Wed, 18 Mar 2009 21:53:21 +0000</pubDate>
		<dc:creator>Michel Boudreau</dc:creator>
				<category><![CDATA[Actionscript]]></category>
		<category><![CDATA[Flash Platform]]></category>
		<category><![CDATA[Flex]]></category>
		<category><![CDATA[blazeds]]></category>
		<category><![CDATA[consumer]]></category>
		<category><![CDATA[swfbridge]]></category>
		<category><![CDATA[xmlsocket]]></category>

		<guid isPermaLink="false">http://www.michelboudreau.com/?p=65</guid>
		<description><![CDATA[A few months ago, the project I was working on ...]]></description>
			<content:encoded><![CDATA[<p>A few months ago, the project I was working on came to a little dilemma.  The system was built using AS2 (not my decision of course), and was using a mix of web services(ws) and an XMLSocket to communicate.  The ws calls would be outgoing, and the XMLSocket would be incoming, that way the server could push any changes done on the back-end and the UI would then appropriately display the proper information.</p>
<p>Our dilemma came when the manager of the project then wanted the application to be accessed from behind any firewall as long as http requests are accepted behind that firewall (as in, a corporate firewall).  Since XMLSocket doesn&#8217;t do http request, it would of been filtered out and our system wouldn&#8217;t work at all.  While looking for the solution, we found <a href="http://opensource.adobe.com/wiki/display/site/Projects#Projects-BlazeDS" target="_blank">BlazeDS</a>, an open source solution from Adobe.  It&#8217;s a Java  library that let&#8217;s you do remoting and web messaging using various different protocols.   They have a pushing solution that works over port 80 with http request.  Perfect.</p>
<p><span id="more-65"></span></p>
<p>Only problem is that AS2 isn&#8217;t built to do this kind of thing, it can&#8217;t communicate with this technology at all.  Now from this point, I strongly suggested that we rebuild the system using Flex, and they listened this time because we tapped out the limitations of AS2.  However, they still needed the AS2 system to work until we complete development on the Flex version which would take many months.</p>
<p>The idea behind the solution was easy enough; have two swfs on the page, one with our main AS2 app, and a very small flex app who&#8217;s only purpose is to send ws calls using the RemoteObject component and receive server pushes with the Consumer component, then have both swfs communicate between each other using LocalConnection.  The only real limitation of the system is because you&#8217;d have two swfs (one being a flex app), the overhead would be a bit bigger, and also the data being sent through the LocalConnection cannot be bigger than 40kb.  Since it&#8217;s LocalConnection, we&#8217;d also need to do a check to make sure the browser has cookies enabled.</p>
<p>Where to start?  Well, with a quick check online, there&#8217;s already classes out there made so that AS2 and AS3 can communicate.  The best one I found was <a href="http://www.gskinner.com/blog/archives/2007/07/swfbridge_easie.html" target="_blank">Grant Skinner&#8217;s SWFBridge</a>. So let&#8217;s start with adding our MXML:</p>
<p><strong>AS2Bridge.mxml</strong></p>

<div class="wp_syntax"><div class="code"><pre class="mxml" style="font-family:monospace;"><span style="color: #000000;">&lt;?xml version=<span style="color: #ff0000;">&quot;1.0&quot;</span> encoding=<span style="color: #ff0000;">&quot;utf-8&quot;</span>?<span style="color: #7400FF;">&gt;</span></span>
<span style="color: #000000;"><span style="color: #7400FF;">&lt;mx:Application</span> xmlns:mx=<span style="color: #ff0000;">&quot;http://www.adobe.com/2006/mxml&quot;</span> layout=<span style="color: #ff0000;">&quot;absolute&quot;</span> creationComplete=<span style="color: #ff0000;">&quot;initApp()&quot;</span> <span style="color: #7400FF;">&gt;</span></span>
	<span style="color: #000000;"><span style="color: #7400FF;">&lt;mx:Script</span> source=<span style="color: #ff0000;">&quot;AS2Bridge.as&quot;</span> <span style="color: #7400FF;">/&gt;</span></span>
	<span style="color: #000000;"><span style="color: #7400FF;">&lt;mx:Consumer</span> id=<span style="color: #ff0000;">&quot;consumer&quot;</span> destination=<span style="color: #ff0000;">&quot;Topic&quot;</span> message=<span style="color: #ff0000;">&quot;streamingChannelResult(event);&quot;</span> fault=<span style="color: #ff0000;">&quot;streamingChannelFault(event);&quot;</span><span style="color: #7400FF;">&gt;</span></span>
		<span style="color: #000000;"><span style="color: #7400FF;">&lt;mx:channelSet</span><span style="color: #7400FF;">&gt;</span></span>
			<span style="color: #000000;"><span style="color: #7400FF;">&lt;mx:ChannelSet</span> id=<span style="color: #ff0000;">&quot;consumerChannelSet&quot;</span> result=<span style="color: #ff0000;">&quot;loginResult(event)&quot;</span> fault=<span style="color: #ff0000;">&quot;loginFault(event)&quot;</span><span style="color: #7400FF;">&gt;</span></span>
				<span style="color: #000000;"><span style="color: #7400FF;">&lt;mx:channels</span><span style="color: #7400FF;">&gt;</span></span>
					<span style="color: #000000;"><span style="color: #7400FF;">&lt;mx:StreamingAMFChannel</span> id=<span style="color: #ff0000;">&quot;consumerAMFChannel&quot;</span> url=<span style="color: #ff0000;">&quot;http://domain.com/services/messagebroker/streamingamf&quot;</span><span style="color: #7400FF;">/&gt;</span></span>
				<span style="color: #000000;"><span style="color: #7400FF;">&lt;/mx:channels</span><span style="color: #7400FF;">&gt;</span></span>
			 <span style="color: #000000;"><span style="color: #7400FF;">&lt;/mx:ChannelSet</span><span style="color: #7400FF;">&gt;</span></span>
		<span style="color: #000000;"><span style="color: #7400FF;">&lt;/mx:channelSet</span><span style="color: #7400FF;">&gt;</span></span>
	<span style="color: #000000;"><span style="color: #7400FF;">&lt;/mx:Consumer</span><span style="color: #7400FF;">&gt;</span></span>
	<span style="color: #000000;"><span style="color: #7400FF;">&lt;mx:RemoteObject</span> id=<span style="color: #ff0000;">&quot;remoteObject&quot;</span> destination=<span style="color: #ff0000;">&quot;WebService&quot;</span> result=<span style="color: #ff0000;">&quot;channelResult(event)&quot;</span> fault=<span style="color: #ff0000;">&quot;channelFault(event)&quot;</span><span style="color: #7400FF;">&gt;</span></span>
		<span style="color: #000000;"><span style="color: #7400FF;">&lt;mx:channelSet</span><span style="color: #7400FF;">&gt;</span></span>
	        <span style="color: #000000;"><span style="color: #7400FF;">&lt;mx:ChannelSet</span> id=<span style="color: #ff0000;">&quot;remoteObjectChannelSet&quot;</span><span style="color: #7400FF;">&gt;</span></span>
	            <span style="color: #000000;"><span style="color: #7400FF;">&lt;mx:channels</span><span style="color: #7400FF;">&gt;</span></span>
	                <span style="color: #000000;"><span style="color: #7400FF;">&lt;mx:AMFChannel</span> uri=<span style="color: #ff0000;">&quot;http://domain.com/services/messagebroker/amf&quot;</span><span style="color: #7400FF;">/&gt;</span></span>
	            <span style="color: #000000;"><span style="color: #7400FF;">&lt;/mx:channels</span><span style="color: #7400FF;">&gt;</span></span>
	        <span style="color: #000000;"><span style="color: #7400FF;">&lt;/mx:ChannelSet</span><span style="color: #7400FF;">&gt;</span></span>
	    <span style="color: #000000;"><span style="color: #7400FF;">&lt;/mx:channelSet</span><span style="color: #7400FF;">&gt;</span></span>
	<span style="color: #000000;"><span style="color: #7400FF;">&lt;/mx:RemoteObject</span><span style="color: #7400FF;">&gt;</span></span>
<span style="color: #000000;"><span style="color: #7400FF;">&lt;/mx:Application</span><span style="color: #7400FF;">&gt;</span></span></pre></div></div>

<p>This small amount of code is pretty self explanatory.  We added an external script, then created the Consumer and RemoteObject, set the appropriate parameters, then created the channelsets needed.  This part is pretty easy, we&#8217;ve set up our connection, now let&#8217;s make it functional:</p>
<p><strong>AS2Bridge.as</strong></p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #0033ff; font-weight: bold;">import</span> com<span style="color: #000066; font-weight: bold;">.</span>gskinner<span style="color: #000066; font-weight: bold;">.</span>utils<span style="color: #000066; font-weight: bold;">.</span>SWFBridgeAS3<span style="color: #000066; font-weight: bold;">;</span>
<span style="color: #0033ff; font-weight: bold;">import</span> mx<span style="color: #000066; font-weight: bold;">.</span>messaging<span style="color: #000066; font-weight: bold;">.*;</span>
<span style="color: #0033ff; font-weight: bold;">import</span> mx<span style="color: #000066; font-weight: bold;">.</span>messaging<span style="color: #000066; font-weight: bold;">.</span>events<span style="color: #000066; font-weight: bold;">.*;</span>
<span style="color: #0033ff; font-weight: bold;">import</span> mx<span style="color: #000066; font-weight: bold;">.</span>messaging<span style="color: #000066; font-weight: bold;">.</span>errors<span style="color: #000066; font-weight: bold;">.*;</span>
<span style="color: #0033ff; font-weight: bold;">import</span> mx<span style="color: #000066; font-weight: bold;">.</span>rpc<span style="color: #000066; font-weight: bold;">.</span>events<span style="color: #000066; font-weight: bold;">.*;</span>
<span style="color: #0033ff; font-weight: bold;">import</span> <span style="color: #004993;">flash.events</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">Event</span><span style="color: #000066; font-weight: bold;">;</span>
&nbsp;
<span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #6699cc; font-weight: bold;">var</span> bridge<span style="color: #000066; font-weight: bold;">:</span>SWFBridgeAS3<span style="color: #000066; font-weight: bold;">;</span>
&nbsp;
<span style="color: #3f5fbf;">/**
* Initializes object after the application has been created
*
* @return void
*/</span>
<span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #339966; font-weight: bold;">function</span> initApp<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span>
<span style="color: #000000;">&#123;</span>
	<span style="color: #0033ff; font-weight: bold;">if</span><span style="color: #000000;">&#40;</span><span style="color: #0033ff; font-weight: bold;">this</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">parent</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">loaderInfo</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">parameters</span><span style="color: #000066; font-weight: bold;">.</span>bridgeId<span style="color: #000000;">&#41;</span>
	<span style="color: #000000;">&#123;</span>
                <span style="color: #009900; font-style: italic;">// Creates an instance of SWFBridge to communicate to AS2</span>
                <span style="color: #009900; font-style: italic;">// First parameter needed is a unique ID for the bridge which is being sent from</span>
                <span style="color: #009900; font-style: italic;">// the html to both the AS2 and AS3 swfs.  This way we can have multiple tabs of this</span>
                <span style="color: #009900; font-style: italic;">// application without having any conflicts</span>
		<span style="color: #0033ff; font-weight: bold;">this</span><span style="color: #000066; font-weight: bold;">.</span>bridge = <span style="color: #0033ff; font-weight: bold;">new</span> SWFBridgeAS3<span style="color: #000000;">&#40;</span><span style="color: #0033ff; font-weight: bold;">this</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">parent</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">loaderInfo</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">parameters</span><span style="color: #000066; font-weight: bold;">.</span>bridgeId<span style="color: #000066; font-weight: bold;">,</span> <span style="color: #0033ff; font-weight: bold;">this</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
		<span style="color: #0033ff; font-weight: bold;">this</span><span style="color: #000066; font-weight: bold;">.</span>bridge<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">addEventListener</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">Event</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">CONNECT</span><span style="color: #000066; font-weight: bold;">,</span> onBridgeConnect<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
	<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span>
&nbsp;
<span style="color: #3f5fbf;">/**
* Calls the specified function on the RemoteObject
*
* @param method:String The function to call on the RemoteObject
* @param arguments:Array An array containing all the arguments to send to the function
* @return void
*/</span>
<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> <span style="color: #004993;">call</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">method</span><span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">String</span><span style="color: #000066; font-weight: bold;">,</span> <span style="color: #004993;">arguments</span><span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">Array</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span>
<span style="color: #000000;">&#123;</span>
	<span style="color: #0033ff; font-weight: bold;">if</span><span style="color: #000000;">&#40;</span>consumer<span style="color: #000066; font-weight: bold;">.</span>authenticated <span style="color: #000066; font-weight: bold;">&amp;&amp;</span> consumer<span style="color: #000066; font-weight: bold;">.</span>subscribed<span style="color: #000000;">&#41;</span>
	<span style="color: #000000;">&#123;</span>
		remoteObject<span style="color: #000066; font-weight: bold;">.</span>getOperation<span style="color: #000000;">&#40;</span><span style="color: #004993;">method</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">send</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">apply</span><span style="color: #000000;">&#40;</span><span style="color: #0033ff; font-weight: bold;">null</span><span style="color: #000066; font-weight: bold;">,</span> <span style="color: #004993;">arguments</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
	<span style="color: #000000;">&#125;</span><span style="color: #0033ff; font-weight: bold;">else</span><span style="color: #000000;">&#123;</span>
		<span style="color: #0033ff; font-weight: bold;">this</span><span style="color: #000066; font-weight: bold;">.</span>bridge<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">send</span><span style="color: #000000;">&#40;</span><span style="color: #990000;">'onResult'</span><span style="color: #000066; font-weight: bold;">,</span> <span style="color: #990000;">'0&lt;end/&gt;'</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
	<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span>
&nbsp;
<span style="color: #3f5fbf;">/**
* Event call when the bridge is connected. Logs you into the backend.
*
* @param e:Event The event object being sent over
* @return void
*/</span>
<span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #339966; font-weight: bold;">function</span> onBridgeConnect<span style="color: #000000;">&#40;</span>e<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">Event</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span>
<span style="color: #000000;">&#123;</span>
	 consumerChannelSet<span style="color: #000066; font-weight: bold;">.</span>login<span style="color: #000000;">&#40;</span><span style="color: #990000;">'username'</span><span style="color: #000066; font-weight: bold;">,</span> <span style="color: #990000;">'password'</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
<span style="color: #000000;">&#125;</span>
&nbsp;
<span style="color: #3f5fbf;">/**
* Event function called when a message gets pushed from the backend to the Consumer
*
* @param event:MessageEvent The event object being sent over
* @return void
*/</span>
<span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #339966; font-weight: bold;">function</span> streamingChannelResult<span style="color: #000000;">&#40;</span>event<span style="color: #000066; font-weight: bold;">:</span>MessageEvent<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span>
<span style="color: #000000;">&#123;</span>
	<span style="color: #0033ff; font-weight: bold;">if</span><span style="color: #000000;">&#40;</span>consumer<span style="color: #000066; font-weight: bold;">.</span>subscribed<span style="color: #000000;">&#41;</span>
	<span style="color: #000000;">&#123;</span>
                <span style="color: #009900; font-style: italic;">// Workaround for LocalConnection limitations</span>
		<span style="color: #6699cc; font-weight: bold;">var</span> str<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">String</span> = event<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">message</span><span style="color: #000066; font-weight: bold;">.</span>body<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">toString</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000066; font-weight: bold;">+</span> <span style="color: #990000;">'&lt;end/&gt;'</span><span style="color: #000066; font-weight: bold;">;</span>
		<span style="color: #6699cc; font-weight: bold;">var</span> byteArray<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">ByteArray</span> = <span style="color: #0033ff; font-weight: bold;">new</span> <span style="color: #004993;">ByteArray</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
		byteArray<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">writeUTF</span><span style="color: #000000;">&#40;</span>str<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
		<span style="color: #0033ff; font-weight: bold;">if</span><span style="color: #000000;">&#40;</span>byteArray<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">length</span> <span style="color: #000066; font-weight: bold;">&lt;</span> <span style="color: #000000; font-weight:bold;">40000</span><span style="color: #000000;">&#41;</span>
		<span style="color: #000000;">&#123;</span>
			<span style="color: #0033ff; font-weight: bold;">this</span><span style="color: #000066; font-weight: bold;">.</span>bridge<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">send</span><span style="color: #000000;">&#40;</span><span style="color: #990000;">'onStreamingResult'</span><span style="color: #000066; font-weight: bold;">,</span> str<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
		<span style="color: #000000;">&#125;</span><span style="color: #0033ff; font-weight: bold;">else</span><span style="color: #000000;">&#123;</span>
			<span style="color: #0033ff; font-weight: bold;">for</span><span style="color: #000000;">&#40;</span><span style="color: #6699cc; font-weight: bold;">var</span> i<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">int</span> = <span style="color: #000000; font-weight:bold;">0</span><span style="color: #000066; font-weight: bold;">;</span> i <span style="color: #000066; font-weight: bold;">&lt;</span> byteArray<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">length</span><span style="color: #000066; font-weight: bold;">;</span> i <span style="color: #000066; font-weight: bold;">+</span>= <span style="color: #000000; font-weight:bold;">40000</span><span style="color: #000000;">&#41;</span>
			<span style="color: #000000;">&#123;</span>
				byteArray<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">position</span> = i<span style="color: #000066; font-weight: bold;">;</span>
				str = byteArray<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">readUTFBytes</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#40;</span>i <span style="color: #000066; font-weight: bold;">+</span> <span style="color: #000000; font-weight:bold;">40000</span><span style="color: #000000;">&#41;</span> <span style="color: #000066; font-weight: bold;">&gt;</span> byteArray<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">length</span><span style="color: #000066; font-weight: bold;">?</span>byteArray<span style="color: #000066; font-weight: bold;">.</span>length<span style="color: #000066; font-weight: bold;">-</span>i<span style="color: #000066; font-weight: bold;">:</span><span style="color: #000000; font-weight:bold;">40000</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
				<span style="color: #0033ff; font-weight: bold;">this</span><span style="color: #000066; font-weight: bold;">.</span>bridge<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">send</span><span style="color: #000000;">&#40;</span><span style="color: #990000;">'onStreamingResult'</span><span style="color: #000066; font-weight: bold;">,</span> str<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
			<span style="color: #000000;">&#125;</span>
		<span style="color: #000000;">&#125;</span>
	<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span>
&nbsp;
<span style="color: #3f5fbf;">/**
* Event function called when an error occurs on the Consumer
*
* @param event:MessageFaultEvent The event object being sent over
* @return void
*/</span>
<span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #339966; font-weight: bold;">function</span> streamingChannelFault<span style="color: #000000;">&#40;</span>event<span style="color: #000066; font-weight: bold;">:</span>MessageFaultEvent<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span>
<span style="color: #000000;">&#123;</span>
	<span style="color: #0033ff; font-weight: bold;">if</span><span style="color: #000000;">&#40;</span>consumer<span style="color: #000066; font-weight: bold;">.</span>subscribed<span style="color: #000000;">&#41;</span>
	<span style="color: #000000;">&#123;</span>
		<span style="color: #0033ff; font-weight: bold;">this</span><span style="color: #000066; font-weight: bold;">.</span>bridge<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">send</span><span style="color: #000000;">&#40;</span><span style="color: #990000;">'onStreamingFault'</span><span style="color: #000066; font-weight: bold;">,</span>event<span style="color: #000066; font-weight: bold;">.</span>faultCode<span style="color: #000066; font-weight: bold;">,</span> event<span style="color: #000066; font-weight: bold;">.</span>faultString<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
	<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span>
&nbsp;
<span style="color: #3f5fbf;">/**
* Event function called when the RemoteObject receives a reply from the web service
*
* @param event:ResultEvent The event object being sent over
* @return void
*/</span>
<span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #339966; font-weight: bold;">function</span> channelResult<span style="color: #000000;">&#40;</span>event<span style="color: #000066; font-weight: bold;">:</span>ResultEvent<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span>
<span style="color: #000000;">&#123;</span>
	<span style="color: #0033ff; font-weight: bold;">if</span><span style="color: #000000;">&#40;</span>consumer<span style="color: #000066; font-weight: bold;">.</span>subscribed<span style="color: #000000;">&#41;</span>
	<span style="color: #000000;">&#123;</span>
		<span style="color: #6699cc; font-weight: bold;">var</span> str<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">String</span> = event<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">message</span><span style="color: #000066; font-weight: bold;">.</span>body<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">toString</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000066; font-weight: bold;">+</span> <span style="color: #990000;">'&lt;end/&gt;'</span><span style="color: #000066; font-weight: bold;">;</span>
		<span style="color: #6699cc; font-weight: bold;">var</span> byteArray<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">ByteArray</span> = <span style="color: #0033ff; font-weight: bold;">new</span> <span style="color: #004993;">ByteArray</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
		byteArray<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">writeUTF</span><span style="color: #000000;">&#40;</span>str<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
		<span style="color: #0033ff; font-weight: bold;">if</span><span style="color: #000000;">&#40;</span>byteArray<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">length</span> <span style="color: #000066; font-weight: bold;">&lt;</span> <span style="color: #000000; font-weight:bold;">40000</span><span style="color: #000000;">&#41;</span>
		<span style="color: #000000;">&#123;</span>
			<span style="color: #0033ff; font-weight: bold;">this</span><span style="color: #000066; font-weight: bold;">.</span>bridge<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">send</span><span style="color: #000000;">&#40;</span><span style="color: #990000;">'onResult'</span><span style="color: #000066; font-weight: bold;">,</span> str<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
		<span style="color: #000000;">&#125;</span><span style="color: #0033ff; font-weight: bold;">else</span><span style="color: #000000;">&#123;</span>
			<span style="color: #0033ff; font-weight: bold;">for</span><span style="color: #000000;">&#40;</span><span style="color: #6699cc; font-weight: bold;">var</span> i<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">int</span> = <span style="color: #000000; font-weight:bold;">0</span><span style="color: #000066; font-weight: bold;">;</span> i <span style="color: #000066; font-weight: bold;">&lt;</span> byteArray<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">length</span><span style="color: #000066; font-weight: bold;">;</span> i <span style="color: #000066; font-weight: bold;">+</span>= <span style="color: #000000; font-weight:bold;">40000</span><span style="color: #000000;">&#41;</span>
			<span style="color: #000000;">&#123;</span>
				byteArray<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">position</span> = i<span style="color: #000066; font-weight: bold;">;</span>
				str = byteArray<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">readUTFBytes</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#40;</span>i <span style="color: #000066; font-weight: bold;">+</span> <span style="color: #000000; font-weight:bold;">40000</span><span style="color: #000000;">&#41;</span> <span style="color: #000066; font-weight: bold;">&gt;</span> byteArray<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">length</span><span style="color: #000066; font-weight: bold;">?</span>byteArray<span style="color: #000066; font-weight: bold;">.</span>length<span style="color: #000066; font-weight: bold;">-</span>i<span style="color: #000066; font-weight: bold;">:</span><span style="color: #000000; font-weight:bold;">40000</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
				<span style="color: #0033ff; font-weight: bold;">this</span><span style="color: #000066; font-weight: bold;">.</span>bridge<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">send</span><span style="color: #000000;">&#40;</span><span style="color: #990000;">'onResult'</span><span style="color: #000066; font-weight: bold;">,</span> str<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
			<span style="color: #000000;">&#125;</span>
		<span style="color: #000000;">&#125;</span>
	<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span>
&nbsp;
<span style="color: #3f5fbf;">/**
* Event function called when the RemoteObject receives an error from the web service
*
* @param event:FaultEvent The event object being sent over
* @return void
*/</span>
<span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #339966; font-weight: bold;">function</span> channelFault<span style="color: #000000;">&#40;</span>event<span style="color: #000066; font-weight: bold;">:</span>FaultEvent<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span>
<span style="color: #000000;">&#123;</span>
	<span style="color: #0033ff; font-weight: bold;">if</span><span style="color: #000000;">&#40;</span>consumer<span style="color: #000066; font-weight: bold;">.</span>subscribed<span style="color: #000000;">&#41;</span>
	<span style="color: #000000;">&#123;</span>
		<span style="color: #0033ff; font-weight: bold;">this</span><span style="color: #000066; font-weight: bold;">.</span>bridge<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">send</span><span style="color: #000000;">&#40;</span><span style="color: #990000;">'onFault'</span><span style="color: #000066; font-weight: bold;">,</span>event<span style="color: #000066; font-weight: bold;">.</span>fault<span style="color: #000066; font-weight: bold;">.</span>faultCode<span style="color: #000066; font-weight: bold;">,</span> event<span style="color: #000066; font-weight: bold;">.</span>fault<span style="color: #000066; font-weight: bold;">.</span>faultString<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
	<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span>
&nbsp;
<span style="color: #3f5fbf;">/**
* Event function called when a successful login has been established on the Consumer
*
* @param event:ResultEvent The event object being sent over
* @return void
*/</span>
<span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #339966; font-weight: bold;">function</span> loginResult<span style="color: #000000;">&#40;</span>event<span style="color: #000066; font-weight: bold;">:</span>ResultEvent<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span>
<span style="color: #000000;">&#123;</span>
	<span style="color: #0033ff; font-weight: bold;">if</span><span style="color: #000000;">&#40;</span><span style="color: #000066; font-weight: bold;">!</span>consumer<span style="color: #000066; font-weight: bold;">.</span>subscribed<span style="color: #000000;">&#41;</span>
	<span style="color: #000000;">&#123;</span>
                <span style="color: #009900; font-style: italic;">// Subscribe to the consumer if login is successful and listen for disconnects</span>
		consumer<span style="color: #000066; font-weight: bold;">.</span>subscribe<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
		consumer<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">addEventListener</span><span style="color: #000000;">&#40;</span>ChannelEvent<span style="color: #000066; font-weight: bold;">.</span>DISCONNECT<span style="color: #000066; font-weight: bold;">,</span> streamingDisconnect<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
	<span style="color: #000000;">&#125;</span>
	<span style="color: #0033ff; font-weight: bold;">this</span><span style="color: #000066; font-weight: bold;">.</span>bridge<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">send</span><span style="color: #000000;">&#40;</span><span style="color: #990000;">'onResult'</span><span style="color: #000066; font-weight: bold;">,</span> event<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">message</span><span style="color: #000066; font-weight: bold;">.</span>body<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">toString</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">+</span><span style="color: #990000;">'&lt;end/&gt;'</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
<span style="color: #000000;">&#125;</span>
&nbsp;
<span style="color: #3f5fbf;">/**
* Event function called when an unsuccessful login occurs
*
* @param event:FaultEvent The event object being sent over
* @return void
*/</span>
<span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #339966; font-weight: bold;">function</span> loginFault<span style="color: #000000;">&#40;</span>event<span style="color: #000066; font-weight: bold;">:</span>FaultEvent<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span>
<span style="color: #000000;">&#123;</span>
	<span style="color: #0033ff; font-weight: bold;">this</span><span style="color: #000066; font-weight: bold;">.</span>bridge<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">send</span><span style="color: #000000;">&#40;</span><span style="color: #990000;">'onResult'</span><span style="color: #000066; font-weight: bold;">,</span> <span style="color: #990000;">'fail&lt;end/&gt;'</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
<span style="color: #000000;">&#125;</span>
&nbsp;
<span style="color: #3f5fbf;">/**
* Event function called when the Consumer disconnects from the server
*
* @param event:ChannelEvent The event object being sent over
* @return void
*/</span>
<span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #339966; font-weight: bold;">function</span> streamingDisconnect<span style="color: #000000;">&#40;</span>event<span style="color: #000066; font-weight: bold;">:</span>ChannelEvent<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span>
<span style="color: #000000;">&#123;</span>
	consumer<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">removeEventListener</span><span style="color: #000000;">&#40;</span>ChannelEvent<span style="color: #000066; font-weight: bold;">.</span>DISCONNECT<span style="color: #000066; font-weight: bold;">,</span> streamingDisconnect<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
	consumerChannelSet<span style="color: #000066; font-weight: bold;">.</span>logout<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
	consumer<span style="color: #000066; font-weight: bold;">.</span>unsubscribe<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
	<span style="color: #0033ff; font-weight: bold;">this</span><span style="color: #000066; font-weight: bold;">.</span>bridge<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">send</span><span style="color: #000000;">&#40;</span><span style="color: #990000;">'onStreamingFault'</span><span style="color: #000066; font-weight: bold;">,</span><span style="color: #990000;">'Server.Disconnect'</span><span style="color: #000066; font-weight: bold;">,</span> <span style="color: #990000;">''</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>You might have noticed that at some of the result event functions there&#8217;s a quick algorithm that seems to count the amount of bytes the message has.  This is a work-around for the limitations of LocalConnection.  LocalConnection can only send over 40kb in total, so if the string being pushed is more than 40kb (I set it to 39kb, just to be safe), we split it up in separate messages to be pushed.  The receiving end (AS2) waits for the &#8216;&lt;end/&gt;&#8217; tag before concatenating the message and processing it.</p>
<p>So we&#8217;ve got our AS3 side of things working.  Now to add our AS2 code to handle our bridge.</p>
<p><strong>AS3Bridge.as</strong></p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">/**
* ConnectionState class is a workaround for an enumerator type
*/</span>
<span style="color: #000000; font-weight: bold;">class</span> ConnectionState <span style="color: #0066CC;">extends</span> <span style="color: #0066CC;">String</span>
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">static</span> <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">var</span> NOTCONNECTED:ConnectionState = <span style="color: #000000; font-weight: bold;">new</span> ConnectionState<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'NotConnected'</span><span style="color: #66cc66;">&#41;</span>;
	<span style="color: #0066CC;">static</span> <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">var</span> READY:ConnectionState = <span style="color: #000000; font-weight: bold;">new</span> ConnectionState<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'Ready'</span><span style="color: #66cc66;">&#41;</span>;
	<span style="color: #0066CC;">static</span> <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">var</span> WAITING:ConnectionState = <span style="color: #000000; font-weight: bold;">new</span> ConnectionState<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'Waiting'</span><span style="color: #66cc66;">&#41;</span>;
	<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> ConnectionState<span style="color: #66cc66;">&#40;</span>value:<span style="color: #0066CC;">String</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span> <span style="color: #0066CC;">super</span><span style="color: #66cc66;">&#40;</span>value<span style="color: #66cc66;">&#41;</span>; <span style="color: #66cc66;">&#125;</span>
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> <span style="color: #0066CC;">valueOf</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">String</span> <span style="color: #66cc66;">&#123;</span>
		<span style="color: #b1b100;">return</span> <span style="color: #0066CC;">super</span>.<span style="color: #0066CC;">valueOf</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #0066CC;">import</span> mx.<span style="color: #006600;">utils</span>.<span style="color: #006600;">Delegate</span>;
<span style="color: #0066CC;">import</span> com.<span style="color: #006600;">gskinner</span>.<span style="color: #006600;">utils</span>.<span style="color: #006600;">SWFBridgeAS2</span>;
&nbsp;
<span style="color: #808080; font-style: italic;">/**
 * Sends and receives data from a bridge that connects to a BlazeDS server
 */</span>
<span style="color: #000000; font-weight: bold;">class</span> AS3Bridge
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #808080; font-style: italic;">/** The socket object used throughout the class */</span>
	<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> bridge:SWFBridgeAS2;
	<span style="color: #808080; font-style: italic;">/** The id of the bridge to connected to */</span>
	<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> id:<span style="color: #0066CC;">String</span>;
	<span style="color: #808080; font-style: italic;">/** Let's the system know in which state we're in*/</span>
	<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> stateData:ConnectionState;
	<span style="color: #808080; font-style: italic;">/** The function to call when xml is received */</span>
	<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> callback:<span style="color: #000000; font-weight: bold;">Function</span>;
	<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> streamCallback:<span style="color: #000000; font-weight: bold;">Function</span>;
	<span style="color: #808080; font-style: italic;">/** Queue array to store socket calls to come */</span>
	<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> queue:<span style="color: #0066CC;">Array</span>;
	<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> streamResultCache:<span style="color: #0066CC;">String</span>;
	<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> resultCache:<span style="color: #0066CC;">String</span>;
&nbsp;
	<span style="color: #808080; font-style: italic;">/**
	*  AS3Bridge class constructor
	*
	* @param _id:String the id of the bridge to connect to
	* @param _streamCallback:Function the function to call when data is being pushed to the frontend
	*/</span>
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> AS3Bridge<span style="color: #66cc66;">&#40;</span>_id:<span style="color: #0066CC;">String</span>, _streamCallback:<span style="color: #000000; font-weight: bold;">Function</span><span style="color: #66cc66;">&#41;</span>
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">this</span>.<span style="color: #006600;">stateData</span> = ConnectionState.<span style="color: #006600;">NOTCONNECTED</span>;
		<span style="color: #0066CC;">this</span>.<span style="color: #006600;">queue</span> = <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">Array</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #0066CC;">this</span>.<span style="color: #006600;">id</span> = _id;
		<span style="color: #0066CC;">this</span>.<span style="color: #006600;">streamCallback</span> = _streamCallback;
		<span style="color: #0066CC;">this</span>.<span style="color: #006600;">resultCache</span> = <span style="color: #0066CC;">this</span>.<span style="color: #006600;">streamResultCache</span> = <span style="color: #ff0000;">''</span>;
		<span style="color: #0066CC;">this</span>.<span style="color: #006600;">bridge</span> = <span style="color: #000000; font-weight: bold;">new</span> SWFBridgeAS2<span style="color: #66cc66;">&#40;</span>_id, <span style="color: #0066CC;">this</span><span style="color: #66cc66;">&#41;</span>;
	<span style="color: #66cc66;">&#125;</span>
&nbsp;
	<span style="color: #808080; font-style: italic;">/**
	*  Calls a function through the bridge
	*
	* @param _method the server method to call
	* @param _callback the function to callback after a response is received from the server
	* @param _data the data to be sent to the server
	* @return Void
	*/</span>
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> <span style="color: #0066CC;">call</span><span style="color: #66cc66;">&#40;</span>_method:<span style="color: #0066CC;">String</span>, _callback:<span style="color: #000000; font-weight: bold;">Function</span>, _data:<span style="color: #0066CC;">Array</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">Void</span>
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #b1b100;">switch</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">this</span>.<span style="color: #006600;">state</span><span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #b1b100;">case</span> ConnectionState.<span style="color: #006600;">NOTCONNECTED</span>:
			<span style="color: #b1b100;">case</span> ConnectionState.<span style="color: #006600;">WAITING</span>:
				<span style="color: #808080; font-style: italic;">// Queue call until the bridge is ready</span>
				<span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">this</span>.<span style="color: #006600;">queue</span><span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">!</span>= <span style="color: #66cc66;">&#123;</span>operation:_method, callback:_callback, <span style="color: #0066CC;">data</span>:_data<span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span>
				<span style="color: #66cc66;">&#123;</span>
					<span style="color: #0066CC;">this</span>.<span style="color: #006600;">queue</span>.<span style="color: #0066CC;">push</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#123;</span>operation:_method, callback:_callback, <span style="color: #0066CC;">data</span>:_data<span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span>;
				<span style="color: #66cc66;">&#125;</span>
				<span style="color: #b1b100;">break</span>;
			<span style="color: #b1b100;">case</span> ConnectionState.<span style="color: #006600;">READY</span>:
				<span style="color: #808080; font-style: italic;">// Change State</span>
				<span style="color: #0066CC;">this</span>.<span style="color: #006600;">state</span> = ConnectionState.<span style="color: #006600;">WAITING</span>;
&nbsp;
				<span style="color: #808080; font-style: italic;">// save callback</span>
				<span style="color: #0066CC;">this</span>.<span style="color: #006600;">callback</span> = _callback;
				<span style="color: #0066CC;">this</span>.<span style="color: #006600;">bridge</span>.<span style="color: #0066CC;">send</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'call'</span>, _method, _data<span style="color: #66cc66;">&#41;</span>;
				<span style="color: #b1b100;">break</span>;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
&nbsp;
	<span style="color: #808080; font-style: italic;">/**
	*  Sets the state of the connection
	*
	* @param value:ConnectionState the current state the system is in
	* @return Void
	*/</span>
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> <span style="color: #0066CC;">set</span> state<span style="color: #66cc66;">&#40;</span>value:ConnectionState<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">Void</span>
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>value <span style="color: #66cc66;">!</span>= <span style="color: #0066CC;">this</span>.<span style="color: #006600;">stateData</span><span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">this</span>.<span style="color: #006600;">stateData</span> = value;
			<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">this</span>.<span style="color: #006600;">stateData</span> == ConnectionState.<span style="color: #006600;">READY</span><span style="color: #66cc66;">&#41;</span>
			<span style="color: #66cc66;">&#123;</span>
				<span style="color: #808080; font-style: italic;">// Go through queue</span>
				<span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">this</span>.<span style="color: #006600;">queue</span>.<span style="color: #0066CC;">length</span> <span style="color: #66cc66;">&gt;</span> <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span>
				<span style="color: #66cc66;">&#123;</span>
					<span style="color: #000000; font-weight: bold;">var</span> op:<span style="color: #0066CC;">Object</span> = <span style="color: #0066CC;">this</span>.<span style="color: #006600;">queue</span>.<span style="color: #006600;">shift</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
					<span style="color: #0066CC;">this</span>.<span style="color: #0066CC;">call</span><span style="color: #66cc66;">&#40;</span>op.<span style="color: #006600;">operation</span>, op.<span style="color: #006600;">callback</span>, op.<span style="color: #0066CC;">data</span>, op.<span style="color: #006600;">timeout</span><span style="color: #66cc66;">&#41;</span>;
				<span style="color: #66cc66;">&#125;</span>
			<span style="color: #66cc66;">&#125;</span>
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
&nbsp;
	<span style="color: #808080; font-style: italic;">/**
	*  Returns the current state
	*
	* @return ConnectionState
	*/</span>
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> <span style="color: #0066CC;">get</span> state<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:ConnectionState
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #b1b100;">return</span> <span style="color: #0066CC;">this</span>.<span style="color: #006600;">stateData</span>;
	<span style="color: #66cc66;">&#125;</span>
&nbsp;
	<span style="color: #808080; font-style: italic;">/**
	*  The return of the RemoteObject
	*
	* @param _result:String the result returned by the RemoteObject
	* @return Void
	*/</span>
	<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> onResult<span style="color: #66cc66;">&#40;</span>_result:<span style="color: #0066CC;">String</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">Void</span>
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">this</span>.<span style="color: #006600;">resultCache</span> += _result;
		<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>_result.<span style="color: #0066CC;">lastIndexOf</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">''</span><span style="color: #66cc66;">&#41;</span> == <span style="color: #66cc66;">&#40;</span>_result.<span style="color: #0066CC;">length</span> - <span style="color: #cc66cc;">6</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">this</span>.<span style="color: #006600;">resultCache</span> = <span style="color: #0066CC;">this</span>.<span style="color: #006600;">resultCache</span>.<span style="color: #0066CC;">slice</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">0</span>, <span style="color: #0066CC;">this</span>.<span style="color: #006600;">resultCache</span>.<span style="color: #0066CC;">length</span> - <span style="color: #cc66cc;">6</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #0066CC;">this</span>.<span style="color: #006600;">callback</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">this</span>.<span style="color: #006600;">resultCache</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #0066CC;">this</span>.<span style="color: #006600;">resultCache</span> = <span style="color: #ff0000;">''</span>;
&nbsp;
			<span style="color: #808080; font-style: italic;">// Change state</span>
			<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>Core.<span style="color: #006600;">loginHandler</span>.<span style="color: #006600;">isAuthenticated</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span> <span style="color: #0066CC;">this</span>.<span style="color: #006600;">state</span> = ConnectionState.<span style="color: #006600;">READY</span>; <span style="color: #66cc66;">&#125;</span>
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
&nbsp;
	<span style="color: #808080; font-style: italic;">/**
	*  The pushed information from the BlazeDS server
	*
	* @param _result:String the result received by the Consumer
	* @return Void
	*/</span>
	<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> onStreamingResult<span style="color: #66cc66;">&#40;</span>_result:<span style="color: #0066CC;">String</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">Void</span>
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #0066CC;">this</span>.<span style="color: #006600;">streamResultCache</span> += _result;
		<span style="color: #808080; font-style: italic;">// checks if the message is done</span>
		<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>_result.<span style="color: #0066CC;">lastIndexOf</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">''</span><span style="color: #66cc66;">&#41;</span> == <span style="color: #66cc66;">&#40;</span>_result.<span style="color: #0066CC;">length</span> - <span style="color: #cc66cc;">6</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">this</span>.<span style="color: #006600;">streamResultCache</span> = <span style="color: #0066CC;">this</span>.<span style="color: #006600;">streamResultCache</span>.<span style="color: #0066CC;">slice</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">0</span>, <span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">this</span>.<span style="color: #006600;">streamResultCache</span>.<span style="color: #0066CC;">length</span> - <span style="color: #cc66cc;">6</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #808080; font-style: italic;">// Then send it to the callback function</span>
			<span style="color: #0066CC;">this</span>.<span style="color: #006600;">streamCallback</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">this</span>.<span style="color: #006600;">streamResultCache</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #0066CC;">this</span>.<span style="color: #006600;">streamResultCache</span> = <span style="color: #ff0000;">''</span>;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
&nbsp;
	<span style="color: #808080; font-style: italic;">/**
	*  RemoteObject error event
	*
	* @param _code:String the fault code
	* @param _fault:String the fault message
	* @return Void
	*/</span>
	<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> onFault<span style="color: #66cc66;">&#40;</span>_code:<span style="color: #0066CC;">String</span>, _fault:<span style="color: #0066CC;">String</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">Void</span>
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #808080; font-style: italic;">// RemoteObject fault, handle it yourself</span>
	<span style="color: #66cc66;">&#125;</span>
&nbsp;
	<span style="color: #808080; font-style: italic;">/**
	*  Consumer error event
	*
	* @param _code:String the fault code
	* @param _fault:String the fault message
	* @return Void
	*/</span>
	<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> onStreamingFault<span style="color: #66cc66;">&#40;</span>_code:<span style="color: #0066CC;">String</span>, _fault:<span style="color: #0066CC;">String</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">Void</span>
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #b1b100;">switch</span><span style="color: #66cc66;">&#40;</span>_code<span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #b1b100;">case</span> <span style="color: #ff0000;">'Server.Processing'</span>:
				<span style="color: #808080; font-style: italic;">// Can be a slew of blazeds problems</span>
				<span style="color: #b1b100;">break</span>;
			<span style="color: #b1b100;">case</span> <span style="color: #ff0000;">'Server.Disconnect'</span>:
				<span style="color: #808080; font-style: italic;">// Server timeout, make your own processing here</span>
				<span style="color: #0066CC;">this</span>.<span style="color: #006600;">state</span> = ConnectionState.<span style="color: #006600;">NOTCONNECTED</span>;
				<span style="color: #b1b100;">break</span>;
			<span style="color: #000000; font-weight: bold;">default</span>:
				<span style="color: #808080; font-style: italic;">// Send fault to callback function</span>
				<span style="color: #0066CC;">this</span>.<span style="color: #006600;">callback</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'FAULT: '</span>+ _code +<span style="color: #ff0000;">' - '</span>+ _fault<span style="color: #66cc66;">&#41;</span>;
				<span style="color: #b1b100;">break</span>;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>Now, from within flash, we just need to instantiate our class using the bridge id being sent over by html (_root.bridgeId) and then add both swfs to an html page using <a href="http://code.google.com/p/swfobject/" target="_blank">SWFObject</a> like so:</p>
<p><strong>index.html</strong></p>

<div class="wp_syntax"><div class="code"><pre class="html4strict" style="font-family:monospace;"><span style="color: #009900;">&lt; !DOCTYPE html PUBLIC <span style="color: #ff0000;">&quot;-//W3C//DTD XHTML 1.0 Strict//EN&quot;</span> <span style="color: #ff0000;">&quot;http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd&quot;</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">html</span> xmlns<span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;http://www.w3.org/1999/xhtml&quot;</span> xml:<span style="color: #000066;">lang</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;en&quot;</span> <span style="color: #000066;">lang</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;en&quot;</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">head</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">meta</span> <span style="color: #000066;">http-equiv</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;Content-Type&quot;</span> <span style="color: #000066;">content</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text/html; charset=utf-8&quot;</span> <span style="color: #66cc66;">/</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">title</span>&gt;</span>Bridge Test<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">title</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">style</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text/css&quot;</span>&gt;</span>
	#as3bridge
	{
		height:0px;
		width:0px;
	}
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">style</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">script</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text/javascript&quot;</span> <span style="color: #000066;">src</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;swfobject.js&quot;</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">script</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">script</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text/javascript&quot;</span>&gt;</span>
function init(){
	document.cookie=&quot;check=true&quot;;
	if(document.cookie.length != 0){
		<span style="color: #009900;">&lt; ?php $id <span style="color: #66cc66;">=</span> time<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>.rand<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">1000000</span><span style="color: #66cc66;">&#41;</span>; ?&gt;</span>
		swfobject.embedSWF(
					&quot;AS2Bridge.swf&quot;, 
					&quot;as3bridge&quot;, 
					&quot;1px&quot;, 
					&quot;1px&quot;, 
					&quot;9.0.0&quot;, 
					&quot;expressInstall.swf&quot;, 
					{bridgeId: 'bridge<span style="color: #009900;">&lt; ?php echo $id;?&gt;</span>'}
				);
		swfobject.embedSWF(
					&quot;AS3Bridge.swf&quot;, 
					&quot;flashcontent&quot;, 
					&quot;100%&quot;, 
					&quot;100%&quot;, 
					&quot;8.0.0&quot;, 
					&quot;expressInstall.swf&quot;, 
					{bridgeId: 'bridge<span style="color: #009900;">&lt; ?php echo $id;?&gt;</span>'})
				;
	}else{ 
		alert('Please enable cookies to view this site')
	}
}
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">script</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">head</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">body</span> <span style="color: #000066;">onLoad</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;init()&quot;</span>&gt;</span>
    <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;as3bridge&quot;</span>&gt;</span>
		<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">a</span> <span style="color: #000066;">href</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;http://www.macromedia.com/go/getflashplayer&quot;</span>&gt;</span>
			<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">strong</span>&gt;</span>Please download and install the latest version of flash player<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">strong</span>&gt;</span>
		<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">a</span>&gt;</span>
	<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span>
	<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;flashcontent&quot;</span>&gt;</span>
		<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">a</span> <span style="color: #000066;">href</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;http://www.macromedia.com/go/getflashplayer&quot;</span>&gt;</span>
			<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">strong</span>&gt;</span>Please download and install the latest version of flash player<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">strong</span>&gt;</span>
		<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">a</span>&gt;</span>
	<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">body</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">html</span>&gt;</span></pre></div></div>

<p>With a bit of php, a unique id can be created and used by the bridge so that the LocalConnection can establish a link between the two swfs without any external interference from other connections.  Also added a bit of javascript to check if cookies are enabled.  If they aren&#8217;t, LocalConnection will not function.  That&#8217;s about it.  I did run into a few quirks here and there along the way, but this code should do 95% of the job and please post if anyone has comments.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.michelboudreau.com/2009/03/18/using-blazeds-with-as2/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Login After BlazeDS Session Timeout</title>
		<link>http://www.michelboudreau.com/2009/03/17/login-after-blazeds-session-timeout/</link>
		<comments>http://www.michelboudreau.com/2009/03/17/login-after-blazeds-session-timeout/#comments</comments>
		<pubDate>Tue, 17 Mar 2009 20:48:01 +0000</pubDate>
		<dc:creator>Michel Boudreau</dc:creator>
				<category><![CDATA[Actionscript]]></category>
		<category><![CDATA[Flex]]></category>
		<category><![CDATA[blazeds]]></category>
		<category><![CDATA[consumer]]></category>

		<guid isPermaLink="false">http://www.michelboudreau.com/?p=68</guid>
		<description><![CDATA[While I was working on a Flex project, I noticed ...]]></description>
			<content:encoded><![CDATA[<p>While I was working on a Flex project, I noticed a weird little quirk.  We&#8217;re currently using BlazeDS to connect to the back-end and it keeps pushing new information to the front-end.  However, we have a session timeout set to 15 minutes for security purposes that needs to be handled on the front-end and then let them be able to re-login.</p>
<p>When the user tried to re-login using the consumer, it wouldn&#8217;t work.  No errors, no message, nothing.  A nice silent error; great.  There wasn&#8217;t a lot of documentation on this phenomenon and the internet didn&#8217;t seem to provide any solutions.</p>
<p><span id="more-68"></span></p>
<p>After days of getting to know everything I could about the Consumer component, I learned this nice little tidbit:</p>
<blockquote><p><code>resubscribeInterval:int</code> [read-write]</p>
<p>The number of milliseconds between resubscribe attempts.    If a Consumer doesn&#8217;t receive an acknowledgement for a subscription    request, it will wait the specified number of milliseconds before     attempting to resubscribe.     Setting the value to zero disables resubscriptions.</p></blockquote>
<p>Apparently, the Consumer tries to resubscribe when the connection goes down.  I personally don&#8217;t like this logic;  shouldn&#8217;t it be up to the developer to handle the event and use the proper logic within that event function? But I digress.</p>
<p>For future reference for anyone out there, if you need to handle the server disconnect event on your own (ie. log back in after a disconnect), make sure you have <em>resubscribeAttempts=&#8221;0&#8243; </em>and<em> resubscribeInterval=&#8221;0&#8243;</em> set within your consumer.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.michelboudreau.com/2009/03/17/login-after-blazeds-session-timeout/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Minified using disk: basic
Page Caching using disk: enhanced
Database Caching 1/30 queries in 0.009 seconds using disk: basic
Object Caching 322/383 objects using disk: basic

Served from: www.michelboudreau.com @ 2012-05-21 07:10:59 -->
