remoting Flash remoting is a big improvement over forms/cgi for communication between flash and server.  There’s a great little project called amfphp for using php with flash remoting.  There’s a whole lot less great (but appreciated!) version called amf::perl for perl and python. There is little documentation, so I thought I’d post an example. 

Here’s my remoting notes dealing with amf::perl.  For context, I was working on a movie recommendation system.

Steps:

(1) On the Server side, in the cgi-bin, create a perl file, and call it, say “recommendation.pl”.   Your version of this file will look the same, except replace “/recommendation” with an appropriate subdirectory where you will put the code in step 2. 

#!perl  -w
use strict;
use AMF::Perl;

my $gateway = AMF::Perl->new;
$gateway->setBaseClassPath("./recommendation/");
$gateway->service();
 

(2) In a subdirectory, in this case “/recommendation”, create a perl module, in this case called “rec.pm”.  This module contains the “methodTable” which will be accessed by the flash code.  The example below has both get and set calls. 

#!perl  -w

use AMF::Perl; 
use lib 'Filesystem\\Active Projects\\'; ## directory where my server side code is
use SimMan::TitleLookup; ## my own server side perl modules
use SimMan::Recommendation; package rec;
## must have this constructor
sub new { my ($proto)=@_; my $self={}; bless $self, $proto; return $self; }
sub methodTable { return { "getFeatureWeights" => { "description" => "Returns the current feature weights", "access" => "remote", }, "setFeatureWeights" => { "description" => "Sets the current feature weights", "access" => "remote", },
        "disambiguateQuery" => {
            "description" => "Returns possible titles and their ids",
            "access" => "remote",
        },
    };
}

sub getFeatureWeights
{
    my ($self) = @_;
    my $hash = SimMan::Readers::readFeatureWeights();
    my @array;
    push @array, $hash;
    return \@array;
}

sub setFeatureWeights
{
    my ($self, $featureWeights) = @_;
    SimMan::Readers::setFeatureWeights($featureWeights);
} sub disambiguateQuery { my ($self, $usertext) = @_; return $titles = SimMan::TitleLookup::getCandidateTitles($usertext); } 1;
 

(3) On the client side, first include the necessary files and give the location of the perl files we just created.  

Then you can directly call the methods listed in the methodTable.  Notice that the results sent back by the perl module will be returned to a function called “method_result”, where method is the name of the method.

import mx.remoting.NetServices;
NetDebug.initialize();
NetServices.setDefaultGatewayUrl("http://localhost/cgi-bin/recommendation.pl");
var connection = NetServices.createGatewayConnection();
var recommender = connection.getService("rec", this);

function disambiguateQuery_Result(result){
    if (result.length == 0){
        Results.htmlText = "<b>No movies found</b>";
    }
    else{
        Results.htmlText = "<b>Are you looking for...</b><br><br>";
        for (i=0; i< result.length; i++) {
            Results.htmlText += "result[i].id + " " + result[i].title + "<br>";
        }
    }
}

searchText.enter = function(){
    recommender.disambiguateQuery(searchText.text, 10);
};

Hope this helps someone!

Share