zoukankan      html  css  js  c++  java
  • 在Flex中使用消息服务

    Flex与服务器的通信组件除了HTTPService,RemoteObject,WebService外,还有Producer、Consumer组件。 Producer和Consumer使用Messaging的方式通信,与请求响应机制不同,这是一钟发布订阅机制。客户端向服务器订阅消息,当服务器消息发生变化后,订阅了该消息的客户端会取得更新数据即时更新,所以可以使用这它们制作出多人交互的应用,如即时聊天,多人对战,实时会议等。 Consumer用来订阅消息,Producer用来发布消息。下面看看如何借助BlazeDS实现一个即时聊天的原型。 1.定义messaging通信的destination 打开部署在服务器的WEB-INF\flex文件价,在messaging-config.xml文件中添加destination。 <destination id="ChartDestination" channels="my-polling-amf"/> 该destination使用了services-config.xml文件中名为my-polling-amf的Channel: <channel-definition id="my-polling-amf" class="mx.messaging.channels.AMFChannel"> <endpoint url="http://{server.name}:{server.port}/{context.root}/messagebroker/amfpolling" class="flex.messaging.endpoints.AMFEndpoint"/> <properties> <polling-enabled>true</polling-enabled> <polling-interval-seconds>1</polling-interval-seconds> </properties> </channel-definition> 2.构建客户端程序 创建一个subscribe按钮,点击则订阅消息,再点击则取消订阅。 创建一个文本域接受聊天内容,一个send按钮发送聊天内容。 创建Producer组件和Consumer组件。 <?xml version="1.0" encoding="utf-8"?> <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute"> <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=chartInput.text; chartInput.text=""; if(consumer.subscribed){ producer.send(message); } } private function messageHandler(event:MessageEvent):void{ chartField.text+=event.message.body+"\n"; } ]]> </mx:Script>   <mx:Button id="toggleButton" label="subscribe" x="537" y="177" width="125" click="onToggleButtonClicked()"/> <mx:TextArea id="chartField" x="303" y="207" width="359" height="144"/> <mx:TextInput id="chartInput" x="303" y="371" width="286"/> <mx:Button x="597" y="371" label="send" click="sendChartInfo()"/> <mx:Producer id="producer" destination="ChartDestination"/> <mx:Consumer id="consumer" destination="ChartDestination" message="messageHandler(event)"/> </mx:Application> 3.在浏览器中测试 打开2个或者更多的窗口,点击subscribe按钮后进行聊天测试。
  • 相关阅读:
    C语言编程获取PE文件导出表内容
    C语言编程获取PE文件导入函数
    C语言编程获取PE文件Section_Header
    C语言编程获取PE文件Option_Header
    C语言编程获取PE文件File_Header内容
    C语言编程获取PE文件DOS头
    Spring源码剖析开篇:什么是Spring?
    重新学习Mysql数据13:Mysql主从复制,读写分离,分表分库策略与实践
    重新学习MySQL数据库12:从实践sql语句优化开始
    重新学习MySQL数据库10:MySQL里的那些日志们
  • 原文地址:https://www.cnblogs.com/zack/p/1434470.html
Copyright © 2011-2022 走看看