当前位置: 代码迷 >> Web前端 >> 简单化的消息订阅例子
  详细解决方案

简单化的消息订阅例子

热度:417   发布时间:2012-11-03 10:57:43.0
简化的消息订阅例子
后台是BlazeDS自带的例子traderdesktop
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical">
	<mx:Script>
		<![CDATA[
			import mx.messaging.channels.AMFChannel;
			import mx.messaging.Channel;
			import mx.messaging.ChannelSet;
			import mx.collections.ArrayCollection;
			import mx.messaging.config.ServerConfig;
			import mx.messaging.events.MessageEvent;
			import samples.portfolio.Stock;
			import mx.messaging.Consumer;
			
			[Bindable]private var items:ArrayCollection = new ArrayCollection();
			
			private var consumer:Consumer;	
			
			private function subscribe(symbol:String):void{
				consumer = new Consumer();
				consumer.destination = "market-data-feed";
				consumer.subtopic = symbol;
/**************定义ChannelSet方式一************/				
/* 				var channelStr:String = ServerConfig.getChannelSet("market-data-feed").channelIds[0];
				consumer.channelSet = new ChannelSet([channelStr]); */
/**************定义ChannelSet方式二************/					
				var pollingChannel:AMFChannel = new AMFChannel("my-polling-amf","http://localhost:8400/samples/messagebroker/amfpolling");
				pollingChannel.pollingEnabled = true; //开启轮询
				pollingChannel.pollingInterval = 10000;//轮询间隔
				var channelSet:ChannelSet = new ChannelSet();
				channelSet.addChannel(pollingChannel);
				consumer.channelSet = channelSet;
				
				consumer.addEventListener(MessageEvent.MESSAGE, messageHandler);
				consumer.subscribe();
			}
			
			private function messageHandler(event:MessageEvent):void{
				var stock:Stock = event.message.body as Stock;
				items.addItem(stock);
	        }
	        
			private function unsubscribe(symbol:String):void{
				consumer.removeEventListener(MessageEvent.MESSAGE, messageHandler);
				if (consumer.subscribed)
				{
					consumer.unsubscribe();
				}
				consumer.channelSet.disconnectAll();
			}	        			
		]]>
	</mx:Script>
	<mx:HBox width="100%">
		<mx:Button label="订阅" click="subscribe('IBM')"/>
		<mx:Button label="取消订阅" click="unsubscribe('IBM')"/>
	</mx:HBox>
	<mx:DataGrid id="dg" dataProvider="{items}" width="100%" height="100%">
<!--		<mx:columns>
			<mx:DataGridColumn headerText="Symbol" dataField="symbol" width="80"/>
			<mx:DataGridColumn headerText="Open" dataField="open" labelFunction="formatNumber" textAlign="right" width="60"/>
			<mx:DataGridColumn headerText="Last" dataField="last" itemRenderer="BackgroundColorRenderer" labelFunction="formatNumber" textAlign="right" width="60"/>
			<mx:DataGridColumn headerText="Change" dataField="change" itemRenderer="ColorRenderer" labelFunction="formatNumber" textAlign="right" width="60"/>
			<mx:DataGridColumn headerText="High" dataField="high" labelFunction="formatNumber" textAlign="right" width="60"/>
			<mx:DataGridColumn headerText="Low" dataField="low" labelFunction="formatNumber" textAlign="right" width="60"/>
		</mx:columns>-->
	</mx:DataGrid>
</mx:Application>
  相关解决方案