ホーム

AirからAMFRPCでweb2pyにつなぐ

web2pyのService機能、AMFRPCをAdobe Airから使う方法。

web2pyの解説はFlash MXの時代のものなので、Flash Builder 4.5で接続してみた。

準備:

web2py Book (3rd Ed.) と同じくpyamf をインストール

easy_install pyamf

インストール後、Webサーバーを再起動。


web2py側:

こちらはサンプルと変わらない。
アプリケーション pyamf_test を作成し、コントローラの default.py に次のファンクションを追加。


@service.amfrpc3('mydomain')
def addNumbers(val1, val2):
    return val1 + val2


Air側:

問題はこちら。Flash Builder 4.5 (Flex 4) での例。Airプロジェクトを作成し、例えば、

  
  <fx:Declarations>
    <mx:RemoteObject id="myremote"
       destination="mydomain" 
       endpoint="http://localhost/pyamf_test/default/call/amfrpc3">
      <mx:method name="addNumbers"
        result="resultHandler(event)"
        fault="faultHandler(event)"/>
    </mx:RemoteObject>
  </fx:Declarations>
	
  <fx:Script>
    <![CDATA[
      import mx.rpc.events.FaultEvent;
      import mx.rpc.events.ResultEvent;
			
      protected function send():void
      {
         myremote.addNumbers(11, 12);
      }
      protected function resultHandler(event:ResultEvent):void
      {
         textarea.text += "\n" + event.message.body;
      }
      protected function faultHandler(event:FaultEvent):void
      {
         textarea.text += "\n" + event.fault.message;
      }
    ]]>
  </fx:Script>
  
  <mx:TextArea x="20" y="60" width="300" height="200" id="textarea"/>
  <mx:Button x="20" y="30" label="DO" click="send()"/>  

ボタンを押すと、サーバーからの回答がTextAreaに表示される。
オブジェクトObject(PythonのDictionary、Array(同じくList)でも動く。

あとは工夫次第で便利に使える。


2011年8月18日 23:33

Powered by movabletype