AIR 中 使用 BlazeDS 消息服务

下载了wing酱的新浪微博AIR版,突然对Adobe AIR 产生了兴趣,刚巧遇上国内某聊天软件与某杀毒软件闹矛盾,本来就对这些软件没有好感的我就萌生一个念头,用AIR写个小小聊天工具,嘿嘿,我就立马想到了以前在RIA中使用过的BlazeDS RemoteObject,听说有个message service,所以来试试吧。

 

At the beginning, i tried to use the lastest version of sdk, so i download the Flex SDK 4.1(build 16076) and setup it successfully.

However, when I tried to run my AIR application, I ran into this error:

VerifyError: Error #1014: Class IIMEClient could not be found.

 

After doing a bit of research I found out that my Adobe AIR project’s application descriptor file wasn’t using the correct namespace for the AIR 2.0 SDK. According to the Adobe AIR 2 Release Notes:


AIR 中 使用 BlazeDS 消息服务_第1张图片

 

I made the change in the descriptor file, and now everything works perfectly.

AIR 中 使用 BlazeDS 消息服务_第2张图片

 

 

Flex code as follow:

 

<s:Producer id="producer"/>

<s:Consumer id="consumer"/>

import mx.messaging.ChannelSet;
import mx.messaging.channels.AMFChannel;
import mx.messaging.events.MessageEvent;
import mx.messaging.messages.AsyncMessage;

private function init():void{
	consumer= new Consumer();
	consumer.destination = "chat";
	var channel:AMFChannel=new AMFChannel("my-long-polling-amf","http://192.168.2.80:8080/TestFlex/spring/messagebroker/amflongpolling");
	var channelSet:ChannelSet = new ChannelSet();
	channelSet.addChannel(channel);
	consumer.channelSet=channelSet;
	consumer.addEventListener(MessageEvent.MESSAGE, function(event:MessageEvent):void{
	chatText.text += event.message.body.chatMessage + "\n"; 
        });
	consumer.subscribe();			
				
	producer.destination="chat";
	producer.channelSet= channelSet;
				
}
			
protected function button1_clickHandler(event:MouseEvent):void{
	var message:AsyncMessage = new AsyncMessage(); 
	message.body.chatMessage = inputChat.text; 
	producer.send(message); 
	inputChat.text = ""; 
}
 

The server side configuration:

In order to use long polling amf channel , I added some config into the service-config.xml:

<channel-definition id="my-long-polling-amf" class="mx.messaging.channels.AMFChannel">
	<endpoint url="http://{server.name}:{server.port}/{context.root}/spring/messagebroker/amflongpolling" class="flex.messaging.endpoints.AMFEndpoint"></endpoint>
	<properties>
		<polling-enabled>true</polling-enabled>
		<wait-interval-millis>-1</wait-interval-millis>
		<polling-interval-millis>100</polling-interval-millis>
		<max-waiting-poll-requests>50</max-waiting-poll-requests>
	</properties>	    
</channel-definition>
  • wait-interval-millis dictates how long the server should wait before returning a polling request. The default is 0. By setting it to -1, we are telling it to wait indefinitely until a message is to be routed to the client.
  • polling-interval-millis is the time interval between polling requests. Since we no longer need to pace the requests, we could safely set the interval to 0.
  • max-waiting-poll-requests caps the number of long polling clients. Since the server is not ackowleging right away during long polling, each client hogs one server thread. Once the cap is reached, any new clients fall back to simple polling clients.

We can see the specification of BlazeDS channel and endpoint from here .

 

I use the spring flex integration framework to my app. So i add some code to the applicationContext_flex.xml:

<flex:message-broker>		
	<flex:message-service default-channels="my-streaming-amf,my-long-polling-amf,my-polling-amf" />
</flex:message-broker>
	
<flex:message-destination id="chat" />

I defined a message destination named 'chat', hence the client side can use the name as the destination name.

 

tip:

  • In the AIR app, we also need to set a Flex Server to it for using blazeds.

AIR 中 使用 BlazeDS 消息服务_第3张图片

  • To use a producer or a consumer , we need to set its channel.

In the RIA, i used to code like this:

var channelSet:ChannelSet = new ChannelSet(["my-long-polling-amf"]);

 

But in the AIR, there are some difference.  It need giving fully qualified url's including the port for the destination, and using addChannel function instead of just using the ChannelSet constructor:

var channel:AMFChannel=new AMFChannel("my-long-polling-amf","http://192.168.2.80:8080/TestFlex/spring/messagebroker/amflongpolling");

var channelSet:ChannelSet = new ChannelSet();
channelSet.addChannel(channel);

 

你可能感兴趣的:(spring,新浪微博,Flex,Adobe,AIR)