Interconnecting Flex modules


Adobe Flex 3 has a very interesting component : LocalConnection. The idea behind is that the modules (which runs on client side) might change some data. The exchanged data is not limited at all to simple forms, but can be from a simple String to a XML or Arrays.

Few things must be known from the start, but as any technology, only after you learn it everything becomes easy.

  1. First of all, the communication can be restrained to a certain domain, let’s say by example www.radupoenaru.com .
  2. You can’t have bidirectional communication from a single LocalConnection. One module acts as a server and another one as a client.
  3. Server module opens a connection to which client connects. If the client loads but the server don’t, then an error occurs in client module.
  4. Be very careful to the name of the procedure called by server – should be the same in server as in client module.

All these pointed out, let’s see some code, first on server module:

// server module function for communication
private function CommunicateToClient(evt:ContextMenuEvent):void {
  if (dataGrid.selectedIndex != -1){
    // just a variable
    var id:String = new String();  
    //instantiate the connection
    var commLocalConnection:LocalConnection
      = new LocalConnection();
    // add error error handling
    commLocalConnection.addEventListener(
    StatusEvent.STATUS,commLocalConnection_Status);
    // create on this connection a logical connection called
    // showModule which calls filepath function with
    // two parameters: pathtofile=movie.flv and the id of the file
    commLocalConnection.send("showMovie", "filePath", 
     "movie.flv", dg.selectedItem.name.toString());     
  }
}

And on client module there are more modifications, first in the init() function, to instantiate and configure the communication link :
 // this local conn needs to be public, placed just above init() function  
public var viewPicsConn:LocalConnection = new LocalConnection();
private function init():void {
    //set viewPics conn !!! next line is very important !!!
    commLocalClient.client = this;
    // restrict the domains to whom this server listen - security caution
    commLocalClient.allowDomain(strServer);
    //test the connection
    try {
      commLocalClient.connect("filePath");
    } catch (error:ArgumentError) {
      Alert.show("Can't connect to Server module. Please check this.\n 
      Error: " + error.message);
      // also add a trace to know where error occured
      trace("Can't connect in init function.");
    }
}

Second modification is the client module function called from the server module:
 // it must be public in order to be available for linkage 
 public function filePath(path:String, Name: String):void{
    trace("path:" + path + "\nname:" +Name);
    // now do your stuff with the parameters received 
    // have fun !
    //
    // If you consider this article useful, 
    // please add a link to it !
 }

Leave a comment

Your email address will not be published. Required fields are marked *