Rails + Flex 再び。

もう2年も前にやったけど、さすがにどんどんバージョンアップしているので、新たに環境を作ろうとすると、前と同じというわけにはいかず…


さらに、最近はがんばって、サーバサイドはvimを使って開発しているので、以前書いたWebORB for Ruby on Rails でFlexと接続する - DROP.FACTORY -- Blogの日記ではeclipseで操作してたけど、コマンドも覚えなきゃというわけでのメモも含め。


とにかく、FlexでHTTPServiceではなく、RemoteObjectを使ってアクセスしたいのです。

Flex version Flex3 Ruby version 1.8.7
Rails version 2.3.2 WebORB version 1.1.1

railsプロジェクト作成・WebORBインストール

とりあえず、Quick Start Guideを参考にとにかく動かしてみる

> rails <プロジェクト名>
> cd <プロジェクト名>
> ruby script/plugin install http://themidnightcoders.net:8089/svn/weborb
> ruby script/server

と、ここまできて!WebRickが動かない…


こちらを参考にさせていただいたところ、WebORBのinit.rbに問題があったようです。

> vim vender/plugin/weborb/init.rb

× Dependencies.mechanism = :load
 ↓
○ ActiveSupport::Dependencies.mechanism = :load

というように書き換えれば、とりあえずWebrickは動きました。


これで、http://localhost:3000/examples/examle.htmlが動けばインストール完了のはずが…
Get Server Info を押しても「Send Failed」になる…


その前に

RubyAMFインストール

こちらのブログを参考にとりあえずHalloWorldまでやってしまう。

> ruby script/plugin install http://rubyamf.googlecode.com/svn/trunk/rubyamf



Rails側】

> ruby script/generate controller Hello

hello_controller.rb

class HelloController < ApplicationController
  def sayhello
    render :amf => "Hello World"
  end
end



Flex側】
services-config.xmlで接続の設定。

services-config.xml

<?xml version="1.0" encoding="UTF-8"?>
<services-config>
  <services>
    <service id="rubyamf-flashremoting-service"
        class="flex.messaging.services.RemotingService"
        messageTypes="flex.messaging.messages.RemotingMessage">

      <destination id="rubyamf">
        <channels>
          <channel ref="rubyamf"/>
        </channels>
        <properties>
          <source>*</source>
        </properties>
      </destination>

    </service>
  </services>

  <channels>
    <channel-definition id="rubyamf" class="mx.messaging.channels.AMFChannel">
      <endpoint uri="http://localhost:3000/rubyamf/gateway"
                class="flex.messaging.endpoints.AMFEndpoint"/>
    </channel-definition>
  </channels>

</services-config>

services-config.xmlコンパイラ引数に。

-services "services-config.xml"

mxmlから接続設定。

RubyAMFTest.mxml

<?xml version="1.0" encoding="utf-8"?>
<mx:Application 
  xmlns:mx="http://www.adobe.com/2006/mxml" 
  layout="absolute">

  <mx:Script>
    <![CDATA[
      import mx.rpc.remoting.Operation;
      import mx.rpc.remoting.RemoteObject;
      import mx.rpc.events.FaultEvent;
      import mx.rpc.events.ResultEvent;
      
      private function resultHandler(event:ResultEvent):void {
        trace(event.result);
      }
      private function faultHandler(event:FaultEvent):void {
        trace(event.fault);
      }
      private function button_clickHandler(event:MouseEvent):void {
        var rmo:RemoteObject = new RemoteObject();
        rmo.source = "hello";
        rmo.destination = "rubyamf";
        rmo.addEventListener(FaultEvent.FAULT, faultHandler);
        rmo["sayhello"].addEventListener(ResultEvent.RESULT, resultHandler);
        var method:Operation = rmo.operations["sayhello"];
        method.send();
      }
    ]]>
  </mx:Script>
  <mx:Button label="push" click="button_clickHandler(event)" />

</mx:Application>

これで、デバッグ実行してボタンを押せば、データが取れるはず…なのに。
Connection Failedになって、FaultEvent発生…

WebORB, RubyAMFどちらでも同様のエラー

エラーを確認したところ

/!\ FAILSAFE /!\  Thu Jul 09 15:11:57 +0900 2009
  Status: 500 Internal Server Error
  private method `split' called for #<Mime::Type:0x82ba61c>
    /usr/local/lib/ruby/gems/1.8/gems/actionpack-2.3.2/lib/action_controller/mime_type.rb:206:in `method_missing'
    /usr/local/lib/ruby/gems/1.8/gems/rack-1.0.0/lib/rack/request.rb:51:in `media_type'
    /usr/local/lib/ruby/gems/1.8/gems/rack-1.0.0/lib/rack/request.rb:117:in `parseable_data?'
    /usr/local/lib/ruby/gems/1.8/gems/rack-1.0.0/lib/rack/request.rb:138:in `POST'
    /usr/local/lib/ruby/gems/1.8/gems/actionpack-2.3.2/lib/action_controller/request.rb:428:in `request_parameters'
    ・
    ・
    /usr/local/lib/ruby/1.8/webrick/server.rb:92:in `start'
    /usr/local/lib/ruby/1.8/webrick/server.rb:23:in `start'
    /usr/local/lib/ruby/1.8/webrick/server.rb:82:in `start'
    /usr/local/lib/ruby/gems/1.8/gems/rack-1.0.0/lib/rack/handler/webrick.rb:13:in `run'
    /usr/local/lib/ruby/gems/1.8/gems/rails-2.3.2/lib/commands/server.rb:111
    /usr/local/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in `gem_original_require'
    /usr/local/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in `require'
    script/server:3

こんな感じ。

どうやら、パラメータの解析かなにか?でsplitメソッドを呼び出したところでおかしくなっているっぽい。
調べたところ、こちらのサイトがかなりドンピシャなもののようだ!

vim config/initializers/mime_types.rb

mime_types.rbに追記

module Mime
  class Type

    def split(*args)
      to_s.split(*args)
    end

  end
end



そして、再びWebrickを起動すると、WebORBでもRubyAMFでも正常にレスポンスがとれました。


めでたしめでたし…