开发环境:
1. eclispe 3.5
2. Flex builder 3.0(eclipse plugin)
3. BlazeDS 3.2
4. Tomcat6.0
操作步骤
1. 从BlazeDS.war建立项目
选择菜单File->import,指定类型为web/war file.下一步选择BlazeDS.war,项目名称为test-server,target-runtime指定为tomcat6,按Finish结束。
2. 建立java类,代码如下:
package remoting;
public class EchoService {
public String echo(String text) {
return "Server says: I received '" + text + "' from you";
}
}
3. 设置amf channel
编辑WEB-INF/flex/services-config.xml,找到amf channel定义的地方,配置如下:
<channel-definition id="my-amf" class="mx.messaging.channels.AMFChannel">
<endpoint url="http://localhost:8080/test-server/messagebroker/amf" class="flex.messaging.endpoints.AMFEndpoint"/>
<properties>
<polling-enabled>false</polling-enabled>
</properties>
</channel-definition>
4. 定义一个destination:
编辑WEB-INF/flex/remoting-config.xml,增加destination定义如下:
<destination id="echoServiceDestination" channels="my-amf">
<properties>
<source>remoting.EchoService</source>
</properties>
</destination>
5. 给项目增加flex特性
鼠标右键点击项目,选择Flex project nature->Add Flex project nature
6. 编辑客户端代码
输入客户端代码如下:
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" width="100%" height="100%">
<mx:Script>
<![CDATA[
import mx.rpc.events.FaultEvent;
import mx.rpc.events.ResultEvent;
// Send the message in response to a Button click.
private function echo():void {
var text:String = ti.text;
remoteObject.echo(text);
}
// Handle the recevied message.
private function resultHandler(event:ResultEvent):void {
ta.text += "Server responded: "+ event.result + "\n";
}
// Handle a message fault.
private function faultHandler(event:FaultEvent):void {
ta.text += "Received fault: " + event.fault + "\n";
}
]]>
</mx:Script>
<mx:RemoteObject
id="remoteObject"
destination="echoServiceDestination"
endpoint="http://localhost:8080/test-server/messagebroker/amf"
result="resultHandler(event);"
fault="faultHandler(event);"/>
<mx:Label text="Enter a text for the server to echo"/>
<mx:TextInput id="ti" text="Hello World!"/>
<mx:Button label="Send" click="echo();"/>
<mx:TextArea id="ta" width="100%" height="100%"/>
</mx:Application>
7. 运行
在Eclipse里运行:确认该项目已经加到tomcat6 server中,启动server.右键点击mxml文件,选择Run As->Flex Application,将会打开一个新的浏览器窗口,其中可以看到运行的程序.
也可以把项目export到war文件,发布到tomcat或其他应用服务器上.