Monday, April 26, 2010

cfObjective 2010 Presentation "Real-Time Death Match"

I presented this year at cfObjective. I had the pleasure to present on the topic of "Real-Time" You can download my slides and code below.
Download Presentation

Monday, April 5, 2010

A Smarter Flex AMFChannel

I use the AMFChannel class probably in all of my Flex projects. Below is a sample of how I use the AMFChannel class to make remote calls from Flex.
   import mx.messaging.ChannelSet;
   import mx.messaging.channels.AMFChannel;
   import mx.rpc.remoting.RemoteObject;

   public function setup():void {
     var remoteChannelSet:ChannelSet = new ChannelSet();
     remoteChannelSet.addChannel(new AMFChannel("my-amf","http://someURL.com/flex2gateway/"));
    
     var service:RemoteObject = new RemoteObject();
     service.channelSet = remoteChannelSet;
    
     //more code goes here...
   }
One thing that I had to tackle early on in my Adobe Flex career was figuring out why my AMF remote calls using the AMFChannel class stopped working when I pushed the code to a secure(https) production environment. Well, it turns out that when you make secure remote calls, you need to use the SecureAMFChannel class. So, instead you would need to replace the code above with something like below.
import mx.messaging.ChannelSet;
import mx.messaging.channels.SecureAMFChannel;
import mx.rpc.remoting.RemoteObject;

public function setup():void {
  var remoteChannelSet:ChannelSet = new ChannelSet();
  remoteChannelSet.addChannel(new SecureAMFChannel("my-secure-amf","https://someURL.com/flex2gateway/secure"));
    
  var service:RemoteObject = new RemoteObject();
  service.channelSet = remoteChannelSet;
    
  //more code goes here...
}
The above code will work in a secure (https) production environment, but now we have hard coded the channel type. It won't work in our development environment anymore. How do we solve this little dilemma? Well, it's pretty easy. Just extend the AMFChannel class and override the getter function for the property "protocol" and write the logic to figure out if the remote calls are going to be secure or not. Below is a smarter AMFChannel class which I just named CustomAMFChannel.
package net.flashdan.utils.channels {
 import mx.messaging.channels.AMFChannel;
 
 public class CustomAMFChannel extends AMFChannel {

   public function CustomAMFChannel(id:String=null, uri:String=null) {
    super(id, uri);
   }
  
   override public function get protocol():String {
    if(this.url.toLowerCase().search("https://") == -1){
     return "http";
    }
    else {
     return "https";
    }
   }
 }
}
You would then use the above class like so.
import mx.messaging.ChannelSet;
import mx.rpc.remoting.RemoteObject;
import net.flashdan.utils.channels.CustomAMFChannel;

 protected function setup():void {
   var remoteChannelSet:ChannelSet = new ChannelSet();
   remoteChannelSet.addChannel(new CustomAMFChannel("my-amf", "https://someURL.com/flex2gateway/secure"));
    
   var service:RemoteObject = new RemoteObject();
   service.channelSet = remoteChannelSet;
    
   //more code goes here...
 }
Hopefully, this will help someone out.
Download Source