Tuesday 2007/08/28
10:43 AM

Categories: Flash/Actionscript, Ruby/Rails, TextMate, Web Dev

A Quick RubyAMF Test

Update 2007-10-22: Updated the line with the RubyAMF installer to point to the new googlecode location. Updated the link to the SSR library.

Update 2007-08-30: Seth's comment below revealed the need for a cross-domain policy file in /public when testing this via the browser. I did all my testing from the flash debug player and the IDE, so I didn't run into this while writing this up. Aaron has said that he'll include the crossdomain policy file as part of the RubyAMF installer in the future.

Back from my Seattle/Portland vacation with Jordan (details on that to follow, but photos are up on Flickr).

Another quick Rails experiment, this time checking out RubyAMF. I wanted to run the RubyAMF gateway as a Rails plugin, so I used this Flex/Rails screencast on the RubyAMF blog as a reference for getting the Rails part done. You might want to view my earlier post on setting up Locomotive with MAMP.

I first created a new Rails app in Locomotive called test_hello_world (I also switched the port from 3001 to 3000). Once that was done I simply replicated the steps in the screencast:

  • Download RubyAMF.
  • Create a rubyamf folder in the test_hello_world/vendor/plugins directory.
  • Copy the contents of the RubyAMF download to the rubyamf directory.
  • Copy the rubyamf/services/rubyamf_controller.rb file to the test_hello_world/app/controllers directory.
  • As per Aaron's note below, use the rails installer. Type ruby script/plugin install http://rubyamf.googlecode.com/svn/trunk/rubyamf
  • Start the Rails app from Locomotive.
  • Test the RubyAMF gateway at http://localhost:3000/rubyamf/gateway.
  • Generate TestWorld controller. This can be done by opening a Terminal/iTerm session in the current Rails app context via the Locomotive > Applications > Open Terminal (Command-T) item. Once that's done you can type: ruby/script/generate controller TestWorld
  • Define a hello_world method in the controller to return the text string "Hello World!"

At this point the screencast goes into Flex, but I decided to make use of the Super-Simple Remoting (SSR) library from RubyAMF. I copied the source files to a folder containing my test AS3 script RemotingTest.as. Here's the contents of RemotingTest.as:

Actionscript:
  1. package
  2. {
  3.     import flash.net.Responder;
  4.     import flash.display.Sprite;
  5.     import org.rubyamf.remoting.ssr.*;
  6.    
  7.     public class RemotingTest extends Sprite
  8.     {
  9.         private var rs:RemotingService;
  10.        
  11.         function RemotingTest()
  12.         {
  13.             init();
  14.         }
  15.        
  16.         private function init():void
  17.         {   
  18.             trace("RemotingTest::init() ");
  19.             rs = new RemotingService("http://localhost:3000/rubyamf/gateway", "TestWorldController");
  20.             rs.hello_world([], onResult, onFault);
  21.         }
  22.                                                
  23.         private function onResult(re:ResultEvent):void
  24.         {
  25.             trace("RemotingTest::onResult() " + re.result.toString());
  26.         }
  27.        
  28.         private function onFault(fault:FaultEvent):void
  29.         {
  30.             trace("onFault: " + fault);
  31.         }
  32.  
  33.     }
  34. }

Note that when we create an instance of the Remoting service we pass it the path to the gateway, and the name of the controller which we just created (TestWorldController). Once that is defined we can then just call methods in that controller like so:

rs.hello_world([], onResult, onFault);

The empty array parameter is there because the method accepts no parameters.

I merely had the result piped to trace output, but you can have it piped to an onscreen text field as well if you don't have logging set up.

I was using the still-in-development AS3/Flex bundle (more on installing/modifying that in a following post) to compile RemotingTest.as into a SWF, but you should be able to link it as a document class to an FLA and generate the SWF that way.


Responses


Aaron Smith

Tuesday 2007/08/28 2:50 PM

Hi There. Thanks for posting about RubyAMF. I just want to point out that RubyAMF has a rails installer.

To install into Rails do this inside your rails folder:
ruby script/plugin install svn://rubyforge.org/var/svn/rubyamf/tags/current/rubyamf

The screencasts that are on the wiki are a little out of date. I’m working on updating everything for everyone.

Thanks again
-Aaron


ds

Tuesday 2007/08/28 3:20 PM

Thanks Aaron. I’ve updated the post accordingly.


seth

Thursday 2007/08/30 1:42 AM

is there any configuration or directives to pass to mxmlc? it keeps saying \\


Justin Lewis

Monday 2007/10/08 12:06 AM

I had better luck with the following:

ruby script/plugin install http://rubyamf.rubyforge.org/svn/tags/current/rubyamf/


Yui-Ikari

Sunday 2007/10/14 4:58 PM

Pls Aaron, new updates and more tutorials will be MORE than welcome :D
Thanks


Leave a Response