The NoteBook of EricKong

            BlogJava :: 首頁 :: 聯系 :: 聚合  :: 管理
            611 Posts :: 1 Stories :: 190 Comments :: 0 Trackbacks
          The Messaging Service lets client applications send and receive messages from other clients. In this example, create a Flex application that sends and receives messages from the same BlazeDS destination.
          Define the messaging destination in WEB-INF/flex/messaging-config.xml, as the following example shows:
          <destination id="MessagingDestination" channels="my-amf-poll"/>

          Define the my-amf-poll channel in WEB-INF/flex/services-config.xml, as the following example shows:
          <channel-definition id="my-amf-poll" class="mx.messaging.channels.AMFChannel">
          <endpoint url="http://{server.name}:{server.port}/{context.root}/messagebroker/amfpoll" class="flex.messaging.endpoints.AMFEndpoint"/>
              
          <properties>
               
          <polling-enabled>true</polling-enabled>
               
          <polling-interval-seconds>1</polling-interval-seconds>
              
          </properties>
          </channel-definition>

          This channel definition creates a polling channel with a polling interval of 1 second. Therefore, the client sends a poll message to the server every second to request new messages. Use a polling channel because it is the easiest way for the client to receive updates. Other options include polling with piggybacking, long-polling, and streaming.The following Flex client application uses the Producer component to send a message to the destination, and the Consumer component to receive messages sent to the destination. To send the message, the Producer first creates an instance of the AsyncMessage class and then sets its body property to the message. Then, it calls the Producer.send() method to send it. To receive messages, the Consumer first calls the Consumer.subscribe() method to subscribe to messages sent to a specific destination.
          <?xml version="1.0"?>
          <!-- intro\intro_messaging.mxml -->
          <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
          width
          ="100%" height="100%"
          creationComplete
          ="consumer.subscribe();">
          <mx:Script>
          <![CDATA[
          import mx.messaging.events.MessageFaultEvent;
          import mx.messaging.events.MessageEvent;
          import mx.messaging.messages.AsyncMessage;
          import mx.messaging.Producer;
          import mx.messaging.Consumer;
          // Send the message in response to a Button click.
          private function sendMessage():void {
          var msg:AsyncMessage = new AsyncMessage();
          msg.body = "Foo";
          producer.send(msg);
          }
          // Handle the received message.
          private function messageHandler(event:MessageEvent):void {
          ta.text += "Consumer received message: "+ event.message.body + "\n";
          }
          // Handle a message fault.
          private function faultHandler(event:MessageFaultEvent):void {
          ta.text += "Received fault: " + event.faultString + "\n";
          }
          ]]>
          </mx:Script>
          <mx:Producer id="producer" destination="MessagingDestination" fault="faultHandler(event);"/>
          <mx:Consumer id="consumer" destination="MessagingDestination" fault="faultHandler(event);" message="messageHandler(event);"/>
          <mx:Button label="Send" click="sendMessage();"/>
          <mx:TextArea id="ta" width="100%" height="100%"/>
          </mx:Application>
          Compile the client application into a SWF file by using Flex Builder or the mxmlc compiler, and then deploy it to
          your web application.
          posted on 2011-06-29 11:58 Eric_jiang 閱讀(308) 評論(0)  編輯  收藏 所屬分類: Flex
          主站蜘蛛池模板: 木兰县| 和林格尔县| 扎兰屯市| 广水市| 商南县| 乌海市| 科技| 大竹县| 永和县| 得荣县| 大同县| 水城县| 尼勒克县| 綦江县| 彭阳县| 连江县| 永靖县| 舟山市| 乐业县| 榕江县| 保德县| 高阳县| 大安市| 桦甸市| 许昌市| 兴国县| 灵璧县| 松阳县| 巴彦淖尔市| 石河子市| 科技| 旺苍县| 拜泉县| 通海县| 镇江市| 洛南县| 永安市| 新野县| 万州区| 沽源县| 萍乡市|