读书人

在Flex中应用消息服务

发布时间: 2012-10-27 10:42:26 作者: rapoo

在Flex中使用消息服务

在Flex中使用消息服务

FlexMessage.mxml:
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" fontSize="13">

<mx:Script>
?<![CDATA[
??import mx.messaging.events.MessageEvent;
??import mx.messaging.messages.AsyncMessage;??
??private function onToggleButtonClicked():void{
???if(toggleButton.label=="subscribe"){
????consumer.subscribe(); //订阅消息
????toggleButton.label="unsubscribe";
???}else{
????consumer.unsubscribe(); //取消订阅
????toggleButton.label="subscribe";
???}
??}
??private function sendChartInfo():void{
???var message:AsyncMessage=new AsyncMessage;
???message.body={msg:chartInput.text,sender:txtme.text,receiver:txtto.text};
???if(consumer.subscribed){
????producer.send(message);//发布消息
???}
???//if (txtto.text != null && txtto.text != ""){
???//?chartField.text += "您对"+txtto.text+"说:" + chartInput.text + "\n";
???//}
???chartInput.text="";
??}
??private function messageHandler(event:MessageEvent):void{
???var chat:Object = event.message.body;
???//if ((chat.receiver != null && chat.receiver != "") && (chat.receiver == txtme.text)){
???//?chartField.text += chat.sender+"对您说:"+chat.msg+"\n";
???//} else if (txtto.text == null || txtto.text == "" && chat.sender != txtme.text){
???
????chartField.text += chat.sender+"对大家说:"+chat.msg+"\n";
???//}
??}
?]]>
</mx:Script>??
?<mx:Button id="toggleButton" label="subscribe" x="454" y="40" width="125" click="onToggleButtonClicked()"/>
?<mx:TextArea id="chartField" x="220" y="88" width="359" height="144"/>
?<mx:TextInput id="chartInput" x="220" y="255" width="290"/>
?<mx:Button x="518" y="255" label="send" click="sendChartInfo()"/>
?<mx:Producer id="producer" destination="ChartDestination"/>
?<mx:Consumer id="consumer" destination="ChartDestination" message="messageHandler(event)"/>
?<mx:TextInput x="266" y="289" width="76" id="txtme"/>
?<mx:Text x="220" y="292" text="自己:
" width="42" height="24" fontSize="12"/>
?<mx:TextInput x="266" y="323" width="76" height="26" id="txtto"/>
?<mx:Label x="220" y="325" text="对方:"/>
</mx:Application>

messaging-config.xml:
<destination id="ChartDestination" channels="my-polling-amf"/>

services-config.xml:?
<channel-definition id="my-polling-amf" class="flex.messaging.endpoints.AMFEndpoint"/>
??????????? <properties>
??????????????? <polling-enabled>true</polling-enabled>
?????????????? <!-- 表示1秒钟轮询一次?-->
??????????????? <polling-interval-seconds>1</polling-interval-seconds>
??????????? </properties>
??????? </channel-definition>

读书人网 >flex

热点推荐