flex有两种传输模式 request-response模式和producer-consumer模式。http,webservice,rpc采用前者,消息采用后者。
《blazeDS Developer Guide》中给出了例子,我稍加改造,做了个聊天室。测试显示,这种方式的速度不容乐观。没有加缓冲池,不知道加了会不会能有改善。
<?xml version="1.0"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute"
creationComplete="logon();" height="432" width="686" borderColor="#141616">
<mx:Script>
<![CDATA[
import mx.messaging.messages.*;
import mx.messaging.events.*;
private function logon():void {
consumer.subscribe();
}
private var begintime:Date;
private var endtime:Date;
private function messageHandler(event: MessageEvent):void {
ta.text += event.message.body;
endtime=new Date();
var elapse:Number=endtime.valueOf()-begintime.valueOf();
ta.text+=" --耗时:"+elapse+" 毫秒" + "\n";
}
private function sendMessage():void {
var message: AsyncMessage = new AsyncMessage();
message.body = userName.text + ": " + msg.text;
producer.send(message);
msg.text = "";
begintime=new Date();
}
]]>
</mx:Script>
<mx:Producer id="producer" destination="marschat"/>
<mx:Consumer id="consumer" destination="marschat" message="messageHandler(event)"/>
<mx:TextArea id="ta" width="640" height="231" x="13.5" y="37" fontSize="12" fontFamily="Times New Roman" color="#DF1500"/>
<mx:TextInput id="userName" x="100" y="275" width="200" fontWeight="bold" text="Eric Han"/>
<mx:Label text="UserName" x="10" y="275" width="80" color="#EEF5F6" fontWeight="bold"/>
<mx:Label text="message" x="10" y="320" width="80" color="#EEF5F6" fontWeight="bold"/>
<mx:TextInput id="msg" width="546" x="98" y="318" />
<mx:Button label="Send" click="sendMessage();" x="318.5" y="377"/>
</mx:Application>
messaging-config.xml配置文件加入:
<destination id="marschat" channels="my-polling-amf" />
启动服务器(我的是tomcat),输入地址:http://localhost:8888/GoNow/chatTest.html

本文通过一个Flex聊天室实例,介绍了Flex的两种传输模式:request-response及producer-consumer,并展示了如何使用BlazeDS进行实时消息传递。测试结果显示,消息传递速度有待提升。
1869

被折叠的 条评论
为什么被折叠?



