<?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>Caleb Adam Haye &#187; Flash Remoting</title>
	<atom:link href="http://caleb.org/blog/category/flash-remoting/feed/" rel="self" type="application/rss+xml" />
	<link>http://caleb.org/blog</link>
	<description></description>
	<lastBuildDate>Fri, 25 Sep 2009 10:40:57 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Flash Remoting: Error-proofing the Connection class</title>
		<link>http://caleb.org/blog/uncategorized/flash-remoting-error-proofing-the-connection-class/</link>
		<comments>http://caleb.org/blog/uncategorized/flash-remoting-error-proofing-the-connection-class/#comments</comments>
		<pubDate>Thu, 19 Oct 2006 23:15:54 +0000</pubDate>
		<dc:creator>Caleb</dc:creator>
				<category><![CDATA[Caleb]]></category>
		<category><![CDATA[Flash Remoting]]></category>

		<guid isPermaLink="false">http://caleb.org/blog/?p=24</guid>
		<description><![CDATA[We all love Flash Remoting (and in particular AMFPHP).  We ambitiously defend this new paradigm of RIA communication, emphasizing it&#8217;s numerous advantages and the significant, immediate benefits we can achieve by introducing remoting techniques into our code.
However, as Jeffery Bennett recently pointed out to me, the Connection class does not dispatch onStatus events if [...]]]></description>
			<content:encoded><![CDATA[<p>We all love Flash Remoting (and in particular AMFPHP).  We ambitiously defend this new paradigm of RIA communication, emphasizing it&#8217;s numerous advantages and the significant, immediate benefits we can achieve by introducing remoting techniques into our code.</p>
<p>However, as <a href="http://meandmybadself.com/" target="_blank">Jeffery Bennett</a> recently pointed out to me, the <a href="http://livedocs.macromedia.com/flashremoting/mx2004/actionscript_api_reference/mx/remoting/Connection.html">Connection class</a> does <strong>not</strong> dispatch onStatus events if the gateway is inaccessable for whatever reason.  This posting shows you have to add this capability with a few simple lines of ActionScript, giving you the ability to elegantly handle such errors.</p>
<p>This is how you would typically construct a simple method call using <a href="http://amfphp.org/">AMFPHP</a>:</p>
<p><code><br />
import mx.remoting.*;<br />
import mx.rpc.*;<br />
import mx.remoting.debug.NetDebug;<br />
var gatewayUrl:String = "http://dev.caleb.org/amfphp/gateway.php"<br />
var service:Service = new Service(gatewayUrl, null, 'HelloWorld');<br />
var pc:PendingCall = service.say("Hello world!");<br />
pc.responder = new RelayResponder(this, "handleResult", "handleError");<br />
function handleResult(re:ResultEvent)<br />
{<br />
	trace('The result is: ' + re.result);<br />
}<br />
function handleError(fe:FaultEvent)<br />
{<br />
	trace('There has been an error');<br />
}<br />
</code></p>
<p>The <a href="http://livedocs.macromedia.com/flashremoting/mx2004/actionscript_api_reference/mx/remoting/Connection.html">Connection </a>class is typically employed by the <a href="http://livedocs.macromedia.com/flashremoting/mx2004/actionscript_api_reference/mx/remoting/Service.html">Service</a> class.  If you inspect the signature of the <a href="http://livedocs.macromedia.com/flashremoting/mx2004/actionscript_api_reference/mx/remoting/Service.html">Service</a> class constructor, you&#8217;ll notice that the 5th [optional] parameter is the responder.  </p>
<p><code>Service ( gatewayURI, logger, serviceName, conn, resp) </code></p>
<p>The first example doesn&#8217;t employ that handler, and even if one where to pass a responder,  it wouldn&#8217;t receive error event dispatches.  The <a href="http://livedocs.macromedia.com/flashremoting/mx2004/actionscript_api_reference/mx/remoting/Connection.html">Service class documentation</a> states, <em>&#8220;The resp parameter allows you to specify the responder object that contains the result and fault handling methods that receive control when the service returns a result or a fault condition.&#8221;</em></p>
<p>The problem is, the <a href="http://livedocs.macromedia.com/flashremoting/mx2004/actionscript_api_reference/mx/remoting/Service.html">Service</a> class doesn&#8217;t receive a fault condition if the <a href="http://livedocs.macromedia.com/flashremoting/mx2004/actionscript_api_reference/mx/remoting/Connection.html">Connection</a>&#8217;s gateway creation fails.  The <a href="http://livedocs.macromedia.com/flashremoting/mx2004/actionscript_api_reference/mx/remoting/Service.html">Service</a> class assumes that the <a href="http://livedocs.macromedia.com/flashremoting/mx2004/actionscript_api_reference/mx/remoting/Connection.html">Connection</a> class has a gateway that is online.  Of course, there is no guarantee that this can happen; it&#8217;s obviously a potentially erroneous assumption.</p>
<p>In order to error-proof the call, we need to make 2 simple changes to the <a href="http://livedocs.macromedia.com/flashremoting/mx2004/actionscript_api_reference/mx/remoting/Connection.html">Connection</a> class, and 1 simple change to the <a href="http://livedocs.macromedia.com/flashremoting/mx2004/actionscript_api_reference/mx/remoting/Service.html">Service</a> class (in addition to passing a valid responder in as the 5th arugment to the Service class).</p>
<p><strong>Changes to the <a href="http://livedocs.macromedia.com/flashremoting/mx2004/actionscript_api_reference/mx/remoting/Connection.html">Connection </a>class:</strong></p>
<ul>
<li>Initialize w/ <a href="http://livedocs.macromedia.com/flash/8/main/wwhelp/wwhimpl/common/html/wwhelp.htm?context=LiveDocs_Parts&#038;file=00001921.html">ASBroadcaster</a></li>
<li>Dispatch onStatus event</li>
</ul>
<p><strong>Change to the <a href="http://livedocs.macromedia.com/flashremoting/mx2004/actionscript_api_reference/mx/remoting/Service.html">Service</a> class:</strong></p>
<ul>
<li>Subscribe to the <a href="http://livedocs.macromedia.com/flashremoting/mx2004/actionscript_api_reference/mx/remoting/Connection.html">Connection</a> class&#8217; events</li>
</ul>
<p><span style="font-size:14px;">Following is the actual code we had to modify:</span></p>
<p><strong><a href="http://livedocs.macromedia.com/flashremoting/mx2004/actionscript_api_reference/mx/remoting/Connection.html">Connection</a> class:</strong></p>
<ol>
<li>Added 1 line to constructor</li>
<p><code>AsBroadcaster.initialize(this);</code></p>
<li>Added onStatus method which dispatches onStatus events using <a href="http://livedocs.macromedia.com/flash/8/main/wwhelp/wwhimpl/common/html/wwhelp.htm?context=LiveDocs_Parts&#038;file=00001921.html">ASBroadcaster</a></li>
<p><code><br />
public function onStatus( status ):Void<br />
{<br />
	trace('[Connection Class] onStatus invoked');<br />
	this.broadcastMessage('onStatus');<br />
}<br />
</code>
</ol>
<p><strong><a href="http://livedocs.macromedia.com/flashremoting/mx2004/actionscript_api_reference/mx/remoting/Service.html">Service</a> class:</strong></p>
<ol>
<li>Added 2 lines @ Line 73</li>
<p><code><br />
__conn.addListener(this);<br />
__conn.addListener(__responder);<br />
</code>
</ol>
<p>The &#8220;error-proof&#8221; example below shows you how to modify both the Service class and the Connection class (Note: I intentionally broke the following code in order to ensure that the onStatus method is invoked.  If you want to see it work properly, change the &#8220;pdhp&#8221; to &#8220;php&#8221;  in the gatewayUrl variable declaration):</p>
<p><code><br />
import mx.remoting.*;<br />
import mx.rpc.*;<br />
import mx.remoting.debug.NetDebug;<br />
var gatewayUrl:String = "http://dev.caleb.org/amfphp/gateway.pdhp"<br />
var service:Service = new Service(gatewayUrl, null, 'HelloWorld', null, this);<br />
var pc:PendingCall = service.say("Hello world!");<br />
pc.responder = new RelayResponder(this, "handleResult", "handleError");<br />
function handleResult(re:ResultEvent)<br />
{<br />
	txtFeedback.text = 'The result is: ' + re.result;<br />
}<br />
function handleError(fe:FaultEvent)<br />
{<br />
	trace('error');<br />
	txtFeedback.text = 'There has been an error';<br />
}<br />
function onStatus( status ):Void<br />
{<br />
	trace('onStatus invoked')<br />
}<br />
</code></p>
<p>The files are available for download here:  <a href="http://dev.caleb.org/download/code/ErrorProofRemotingConnection.zip">http://dev.caleb.org/download/code/ErrorProofRemotingConnection.zip</a></p>
<p>I would love to hear any feedback anyone might have <img src='http://caleb.org/blog/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://caleb.org/blog/uncategorized/flash-remoting-error-proofing-the-connection-class/feed/</wfw:commentRss>
		<slash:comments>16</slash:comments>
		</item>
		<item>
		<title>AMFPHP in  AS3</title>
		<link>http://caleb.org/blog/actionscript-3/amfphp-in-as3/</link>
		<comments>http://caleb.org/blog/actionscript-3/amfphp-in-as3/#comments</comments>
		<pubDate>Mon, 11 Sep 2006 19:46:42 +0000</pubDate>
		<dc:creator>Caleb</dc:creator>
				<category><![CDATA[ActionScript 3]]></category>
		<category><![CDATA[Flash Remoting]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://caleb.org/blog/?p=8</guid>
		<description><![CDATA[Remoting in ActionScript 3 is easy to implement, but there are some important things to keep in mind.  Most notably, the mx.remoting package is no longer required.  There are no additional packages to install, everything you  need is built right in to flash.net
This RemotingConnection class extends NetConnection and sets the objectEncoding to [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://en.wikipedia.org/wiki/Remoting">Remoting</a> in ActionScript 3 is easy to implement, but there are some important things to keep in mind.  Most notably, the <i>mx.remoting</i> package is no longer required.  There are no additional packages to install, everything you  need is built right in to <i>flash.net</i></p>
<p>This <i>RemotingConnection</i> class extends NetConnection and sets the objectEncoding to AMF:</p>
<p><code>package<br />
{<br />
	import flash.net.NetConnection;<br />
	import flash.net.ObjectEncoding;<br />
	public class RemotingConnection extends NetConnection<br />
	{<br />
		public function RemotingConnection( sURL:String )<br />
		{<br />
			objectEncoding = ObjectEncoding.AMF0;<br />
			if (sURL) connect( sURL );<br />
		}<br />
	}<br />
}<br />
</code></p>
<p>This class employs the above class, and makes a remote service call:</p>
<p><code><br />
package<br />
{<br />
	import flash.net.Responder;<br />
	public class AS3RemotingTest<br />
	{<br />
		public var dataProvider:Array;<br />
		public var gateway : RemotingConnection;<br />
		public function AS3RemotingTest()<br />
		{<br />
			this.init();<br />
		}<br />
		public function init()<br />
		{<br />
			var gatewayUrl:String = "http://localhost:88/amfphp/gateway.php";<br />
			gateway = new RemotingConnection(gatewayUrl);<br />
			var responder:Responder = new Responder(onResult, onFault);<br />
			var arg:String = 'foo';<br />
			gateway.call( "HelloWorld.say", responder, arg);<br />
		}<br />
		public function onResult( result ) : void<br />
		{<br />
			trace('onResult invoked');<br />
			trace(result);<br />
		}<br />
		public function onFault( fault : String ) : void<br />
		{<br />
			trace('onFault invoked');<br />
			trace( fault );<br />
		}<br />
	}<br />
}</code></p>
<p>(<strong>Note: </strong>The above code assumes that you have AMFPHP installed and running out of a folder named amfphp on your localhost)</p>
]]></content:encoded>
			<wfw:commentRss>http://caleb.org/blog/actionscript-3/amfphp-in-as3/feed/</wfw:commentRss>
		<slash:comments>13</slash:comments>
		</item>
	</channel>
</rss>
