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:

[as]
package
{
import flash.net.Responder;
import flash.display.Sprite;
import org.rubyamf.remoting.ssr.*;

public class RemotingTest extends Sprite
{
private var rs:RemotingService;

function RemotingTest()
{
init();
}

private function init():void
{
trace(“RemotingTest::init() “);
rs = new RemotingService(“http://localhost:3000/rubyamf/gateway”, “TestWorldController”);
rs.hello_world([], onResult, onFault);
}

private function onResult(re:ResultEvent):void
{
trace(“RemotingTest::onResult() ” + re.result.toString());
}

private function onFault(fault:FaultEvent):void
{
trace(“onFault: ” + fault);
}

}
}
[/as]

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.