flex builder + blazeds 实现点对点的聊天

这两天研究用blazeds实现web点对点的聊天功能,在网上各种查找资料,有点收获。

一 环境搭建
下载 blazeds.war,放在tomcat的webapps下,启动tomcat,会发现多了一个blazeds文件夹。
在webapps下新建文件夹MyTest,将blazeds中的文件拷入
在flex builder3中新建j2ee项目MyTest,在配置server时书写如下:
root folder D:\work\apache-tomcat-6.0.30\webapps\MyTest
root url http://localhost:8080/MyTest/
context root /MyTest

二 代码和配置文件

chat.mxml


<?xml version="1.0" encoding="utf-8"?>
<!-- BlazeDS or LiveCycle Data Services is required to use the approach described in this sample -->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
backgroundGradientColors="[0x000000,0x323232]"
applicationComplete="init()"
viewSourceURL="srcview/index.html">

<mx:Script>
<![CDATA[
import mx.messaging.messages.AsyncMessage;
import mx.messaging.messages.IMessage;


[Bindable]
private var selectorIDs:String

private function init():void
{
selectorIDs="ids= " + Math.floor(Math.random() * 1000);
this.log.text=selectorIDs;
// Start listening for messages published in the "chat" destination
consumer.subscribe();

}

private function send():void
{
var message:IMessage=new AsyncMessage();
message.headers=new Array();
message.headers["ids"]=this.targetId.text;
message.body.chatMessage=msg.text;
producer.send(message);
msg.text="";
}

private function messageHandler(message:IMessage):void
{
log.text+=message.body.chatMessage + "\n";
}
]]>
</mx:Script>

<!-- The producer is used to send messages -->
<!-- See the definition of the "chat" destination in messaging-config.xml -->
<mx:Producer id="producer"
destination="chatServer"/>

<!-- The consumer is used to listen for messages.
When a message is published in the "chat" destination the message event handler
is automatically executed -->
<mx:Consumer id="consumer"
selector="{selectorIDs}"
destination="chatServer"
message="messageHandler(event.message)"/>

<mx:Panel title="ChatServer"
width="100%"
height="100%">
<mx:TextArea id="log"
width="100%"
height="100%"/>
<mx:ControlBar height="63">
<mx:TextInput id="targetId"/>
<mx:TextInput id="msg"
width="100%"
enter="send()"
height="24"/>
<mx:Button label="Send"
click="send()"/>
</mx:ControlBar>
</mx:Panel>

</mx:Application>


massaging -config.xml

<?xml version="1.0" encoding="UTF-8"?>
<service id="message-service"
class="flex.messaging.services.MessageService">

<adapters>
<adapter-definition id="actionscript" class="flex.messaging.services.messaging.adapters.ActionScriptAdapter" default="true" />
<adapter-definition id="jms" class="flex.messaging.services.messaging.adapters.JMSAdapter"/>
</adapters>

<default-channels>
<channel ref="my-streaming-amf"/>
</default-channels>


<destination id="chatClient">

<properties>
<network>
<session-timeout>0</session-timeout>
</network>
<server>
<max-cache-size>1000</max-cache-size>
<message-time-to-live>0</message-time-to-live>
<durable>false</durable>
</server>
</properties>
<channels>
<channel ref="my-streaming-amf" />
</channels>
</destination>

</service>


services -config.xml

<?xml version="1.0" encoding="UTF-8"?>
<services-config>

<services>
<service-include file-path="remoting-config.xml" />
<service-include file-path="proxy-config.xml" />
<service-include file-path="messaging-config.xml" />
</services>

<security>
<login-command class="flex.messaging.security.TomcatLoginCommand" server="Tomcat"/>
<!-- Uncomment the correct app server
<login-command class="flex.messaging.security.TomcatLoginCommand" server="JBoss">
<login-command class="flex.messaging.security.JRunLoginCommand" server="JRun"/>
<login-command class="flex.messaging.security.WeblogicLoginCommand" server="Weblogic"/>
<login-command class="flex.messaging.security.WebSphereLoginCommand" server="WebSphere"/>
-->

<!--
<security-constraint id="basic-read-access">
<auth-method>Basic</auth-method>
<roles>
<role>guests</role>
<role>accountants</role>
<role>employees</role>
<role>managers</role>
</roles>
</security-constraint>
-->
</security>

<channels>
<channel-definition id="my-streaming-amf" class="mx.messaging.channels.StreamingAMFChannel">
<endpoint url="http://{server.name}:{server.port}/{context.root}/messagebroker/streamingamf" class="flex.messaging.endpoints.StreamingAMFEndpoint"/>
<properties>
<idle-timeout-minutes>0</idle-timeout-minutes>
<max-streaming-clients>10</max-streaming-clients>
<server-to-client-heartbeat-millis>5000</server-to-client-heartbeat-millis>
<user-agent-settings>
<user-agent match-on="MSIE" kickstart-bytes="2048" max-streaming-connections-per-session="3"/>
<user-agent match-on="Firefox" kickstart-bytes="2048" max-streaming-connections-per-session="3"/>
</user-agent-settings>
</properties>
</channel-definition>



<channel-definition id="my-amf" class="mx.messaging.channels.AMFChannel">
<endpoint url="http://{server.name}:{server.port}/{context.root}/messagebroker/amf" class="flex.messaging.endpoints.AMFEndpoint"/>
</channel-definition>

<channel-definition id="my-secure-amf" class="mx.messaging.channels.SecureAMFChannel">
<endpoint url="https://{server.name}:{server.port}/{context.root}/messagebroker/amfsecure" class="flex.messaging.endpoints.SecureAMFEndpoint"/>
<properties>
<add-no-cache-headers>false</add-no-cache-headers>
</properties>
</channel-definition>

<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>4</polling-interval-seconds>
</properties>
</channel-definition>

<!--
<channel-definition id="my-http" class="mx.messaging.channels.HTTPChannel">
<endpoint url="http://{server.name}:{server.port}/{context.root}/messagebroker/http" class="flex.messaging.endpoints.HTTPEndpoint"/>
</channel-definition>

<channel-definition id="my-secure-http" class="mx.messaging.channels.SecureHTTPChannel">
<endpoint url="https://{server.name}:{server.port}/{context.root}/messagebroker/httpsecure" class="flex.messaging.endpoints.SecureHTTPEndpoint"/>
<properties>
<add-no-cache-headers>false</add-no-cache-headers>
</properties>
</channel-definition>
-->
</channels>

<logging>
<target class="flex.messaging.log.ConsoleTarget" level="Error">
<properties>
<prefix>[BlazeDS] </prefix>
<includeDate>false</includeDate>
<includeTime>false</includeTime>
<includeLevel>false</includeLevel>
<includeCategory>false</includeCategory>
</properties>
<filters>
<pattern>Endpoint.*</pattern>
<pattern>Service.*</pattern>
<pattern>Configuration</pattern>
</filters>
</target>
</logging>

<system>
<redeploy>
<enabled>false</enabled>
<!--
<watch-interval>20</watch-interval>
<watch-file>{context.root}/WEB-INF/flex/services-config.xml</watch-file>
<watch-file>{context.root}/WEB-INF/flex/proxy-config.xml</watch-file>
<watch-file>{context.root}/WEB-INF/flex/remoting-config.xml</watch-file>
<watch-file>{context.root}/WEB-INF/flex/messaging-config.xml</watch-file>
<watch-file>{context.root}/WEB-INF/flex/data-management-config.xml</watch-file>
<touch-file>{context.root}/WEB-INF/web.xml</touch-file>
-->
</redeploy>
</system>

</services-config>


三 点对点的通信
代码中有所体现,生产者指定目的id(自己定的,比如用户在数据库中的唯一识别符),服务器通过id对订阅者进行过滤。

四 维护在线状态
查了下资料,可能是通过监听session的状态来实现(这样只能保证服务器知道在线状态,可能还需要服务器广播次id的状态,然后使每一个客户端都能更新在线列表,只是想法,不知能否实现)

参考资料
[url]http://hi.baidu.com/sant009/blog/item/a7fe4edb571f3561d1164e7b.html[/url]
[url]http://blog.csdn.net/kvgnt/article/details/6822267[/url]
[url]http://ewardluo.blog.163.com/blog/static/4845696200951704352172/[/url]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值