Amfphp Remote Service class

I have told you all that I would post this a while ago so here it is. This is the class I use to dynamically grab data via amfphp programmatically in actionscript 3 without needing to create a remote object mxml node.

package com.mrconnerton.proxy
{
	import flash.events.EventDispatcher;
	import flash.events.IEventDispatcher;
	import flash.external.ExternalInterface;
	
	import mx.core.Application;
	import mx.messaging.ChannelSet;
	import mx.messaging.channels.AMFChannel;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;
	import mx.rpc.remoting.Operation;
	import mx.rpc.remoting.RemoteObject;

	[Event(name="result", type="mx.rpc.events.ResultEvent")]
	[Event(name="fault", type="mx.rpc.events.FaultEvent")]
	
	public class RemoteService extends EventDispatcher implements IEventDispatcher
	{
		private var channelSet:ChannelSet = new ChannelSet();
		private var channel:AMFChannel;
		private var service:RemoteObject = new RemoteObject();
		private var operation:Operation;
		private var _source:String;
		private var _method:String;
		private var _faultAt:String;
		private var url:String = ExternalInterface.call("window.location.href.toString");
		private var gateway:String = "amfphp/gateway.php";
		
		public function RemoteService(source:String, method:String):void {
			channel = new AMFChannel("amfphp", url+gateway);
			_source = source; 
			_method = method;
			
			operation = new Operation(service);
			channelSet.addChannel(channel);
			service.destination = "amfphp";
			service.channelSet = channelSet;
			service.source = _source;
		}
		public function send(...args):void {
			service.getOperation(_method).addEventListener(ResultEvent.RESULT, resultHandler);
			service.getOperation(_method).addEventListener(FaultEvent.FAULT, faultHandler);
			var func:Function = service.getOperation(_method).send;
				func.apply(service.getOperation(_method), args);
		} 

		public function resultHandler(event:ResultEvent):void {
			service.getOperation(_method).removeEventListener(ResultEvent.RESULT, resultHandler);
			service.getOperation(_method).removeEventListener(FaultEvent.FAULT, faultHandler);
			dispatchEvent(event);
		}
		public function faultHandler(event:FaultEvent):void {
			service.getOperation(_method).removeEventListener(ResultEvent.RESULT, resultHandler);
			service.getOperation(_method).removeEventListener(FaultEvent.FAULT, faultHandler);
			dispatchEvent(event);
		}
	}
}

Then anywhere in your flex (or flash) application you want to call an amfphp method you use:

private function loadNode():void {
  var rs:RemoteService = new RemoteService('node','load');
      rs.addEventListener(ResultEvent.RESULT, onNodeLoad);
      rs.addEventListener(ResultEvent.RESULT, function(event:ResultEvent):void {
        rs.removeEventListener(ResultEvent.RESULT, arguments.callee);
          var node:Object = event.result;
          // Do stuff with node
      }
      rs.addEventListener(FaultEvent.FAULT, function(event:FaultEvent):void {
        rs.removeEventListener(FaultEvent.FAULT, arguments.callee);
          // AAAAHHHH!!!!!!!!
      }
      rs.send();
}

Post new comment

The content of this field is kept private and will not be shown publicly.
CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Copyright © mrconnerton.com, 2008-2010. All Rights Reserved