すっかりご無沙汰してしまいました。
書く習慣がなくなると駄目ですね。
というわけで、Data::AMF 0.03 がリリースされました。
http://search.cpan.org/dist/Data-AMF/
このバージョンから AMF3 がサポートされました。
あと、Data::AMF::Remoting というのが追加されていて、
NetConnection を使った Flash Remoting と、Flex の RemoteObject
を使った Flex RPC が簡単に出来るようになりました。
Plack でゲートウェイを実装するとこんな感じです。
use Data::AMF::Remoting;
use Plack::Request;
use UNIVERSAL::require;
sub
{
my $env = shift;
my $req = Plack::Request->new($env);
my $res = $req->new_response(200);
if ($req->path =~ /\/amf\/gateway$/)
{
my $remoting = Data::AMF::Remoting->new(
source => $req->raw_body,
message_did_process => sub
{
my $message = shift;
my ($class_name, $method) = split '\.', $message->target_uri;
$class_name->require;
my $controller = $class_name->new;
return $controller->$method($message->value);
}
);
$remoting->run;
$res->content_type('application/x-amf');
$res->body($remoting->data);
}
return $res->finalize;
};
source に POSTデータを渡して、message_did_process というハンドラで、
メッセージ毎に処理しています。ヘッダー用には別途 header_did_process という
ハンドラが用意されています。
$message->target_uri に、Controller.method という形式の文字列が渡ってくるので、
これを使ってディスパッチします。
$message->value は Perl オブジェクトにデシリアライズされた引数です。
コントローラクラスの方は MooseX::Declare を使うと良い感じに書けます。
use MooseX::Declare;
class HelloController
{
method echo(Str $text)
{
return $text;
}
}
Flex 側はこんな感じです。
<mx:RemoteObject id="helloService"
endpoint="http://localhost:5000/amf/gateway"
destination="perlamf"
source="HelloController"
showBusyCursor="true"
result="trace(event.result)"
fault="trace(event.fault.faultDetail)"
/>
<mx:Button label="Hello" click="helloService.echo('Hello, world!')" />
destication は特に使われないのですが、空にするとランタイムエラーになるので、
適当に書いておきます。
source がサーバー側のクラス名になります。
これでボタンがクリックされると、HelloController クラスの echo メソッドが
コールされます。