这是一个应用RemoteObject和Rubyamf 的HelloWorld的演示实例。我们点击sayHello按钮,然后访问服务器端Ruby代码,然后在客户端显示服务器端返回的结果。
现在让我们开始:
1.打开Flex Builder 2。
2.创建一个HelloWorld的新工程。(File > New > Flex Project)
3.选择Basic application。
3.选择Basic application。
4.拷贝下面的代码到HelloWorld.mxml
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" verticalAlign="middle" horizontalAlign="center" >
<mx:RemoteObject id="myservice" fault="sayHelloFault(event)" showBusyCursor="true" source="tutorials.HelloWorld" destination="rubyamf">
<mx:method name="sayHello" result="sayHelloResult(event)" />
</mx:RemoteObject>
<mx:Script>
<![CDATA[
import mx.managers.CursorManager;
import mx.rpc.events.ResultEvent;
import mx.rpc.events.FaultEvent;
private function sayHelloFault(fault:FaultEvent):void
{
CursorManager.removeBusyCursor();
result_text.text = "code: " + fault.fault.faultCode + " Message: " + fault.fault.faultString + " Detail: " + fault.fault.faultDetail;
}
private function sayHelloResult(evt:ResultEvent):void
{
result_text.text = evt.message.body.toString(); // same as: evt.result.toString();
}
]]>
</mx:Script>
<mx:Panel title="Rubyamf Says Hello" paddingTop="20" paddingLeft="20" paddingBottom="20" paddingRight="20">
<mx:Label x="10" y="10" text="Result:"/>
<mx:TextArea x="10" y="36" width="319" height="113" id="result_text"/>
<mx:Button x="250" y="157" label="sayHello" width="79" click="myservice.getOperation('sayHello').send();"/>
<mx:Button x="250" y="187" label="test fault" click="myservice.getOperation('foo').send(); "/>
</mx:Panel>
</mx:Application>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" verticalAlign="middle" horizontalAlign="center" >
<mx:RemoteObject id="myservice" fault="sayHelloFault(event)" showBusyCursor="true" source="tutorials.HelloWorld" destination="rubyamf">
<mx:method name="sayHello" result="sayHelloResult(event)" />
</mx:RemoteObject>
<mx:Script>
<![CDATA[
import mx.managers.CursorManager;
import mx.rpc.events.ResultEvent;
import mx.rpc.events.FaultEvent;
private function sayHelloFault(fault:FaultEvent):void
{
CursorManager.removeBusyCursor();
result_text.text = "code: " + fault.fault.faultCode + " Message: " + fault.fault.faultString + " Detail: " + fault.fault.faultDetail;
}
private function sayHelloResult(evt:ResultEvent):void
{
result_text.text = evt.message.body.toString(); // same as: evt.result.toString();
}
]]>
</mx:Script>
<mx:Panel title="Rubyamf Says Hello" paddingTop="20" paddingLeft="20" paddingBottom="20" paddingRight="20">
<mx:Label x="10" y="10" text="Result:"/>
<mx:TextArea x="10" y="36" width="319" height="113" id="result_text"/>
<mx:Button x="250" y="157" label="sayHello" width="79" click="myservice.getOperation('sayHello').send();"/>
<mx:Button x="250" y="187" label="test fault" click="myservice.getOperation('foo').send(); "/>
</mx:Panel>
</mx:Application>
5.新建一个services-config.xml的文件,然后拷贝下面代码。
<?xml version="1.0" encoding="UTF-8"?>
<services-config>
<services>
<service id="rubyamf" 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:8024/gateway.rb" class="flex.messaging.endpoints.AMFEndpoint"/>
</channel-definition>
</channels>
</services-config>
<services-config>
<services>
<service id="rubyamf" 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:8024/gateway.rb" class="flex.messaging.endpoints.AMFEndpoint"/>
</channel-definition>
</channels>
</services-config>
6.然后在Project > Properties 下的additional compiler arguments里填写 -locale en_US –services “services-config.xml”。
7.编译生成文件
编译生产后的swf文件 就可以访问rubyamf里的方法了。