读书人

AIR 中 应用 BlazeDS 消息服务

发布时间: 2012-10-28 09:54:44 作者: rapoo

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 消息服务

?

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

AIR 中 应用 BlazeDS 消息服务

?

?

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" name="code"><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 消息服务

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);

?

读书人网 >网络基础

热点推荐