浏览 4882 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (7) :: 隐藏帖 (0)
|
|
---|---|
作者 | 正文 |
发表时间:2009-12-15
最后修改:2009-12-15
对BlazeDS不了解的可以先去这里了解下http://livedocs.adobe.com/blazeds/1/blazeds_devguide/ 下面是步骤: 1.下载BlazeDS.war解压到%TOMCAT_HOME%/webapps目录下文件名为blazeds内包涵了WEB-INF和META-INF文件夹。 2.写一个flex的客户端 dsMessage.mxml <?xml version="1.0" encoding="utf-8"?> <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical" creationComplete="applicationInit()"> <mx:Script> <![CDATA[ import mx.messaging.ChannelSet; import mx.messaging.channels.AMFChannel; import mx.controls.Alert; import mx.messaging.events.MessageEvent; import mx.messaging.messages.AsyncMessage; import mx.messaging.events.MessageFaultEvent; import mx.messaging.events.MessageAckEvent; import mx.rpc.events.ResultEvent; import mx.rpc.events.FaultEvent; private function applicationInit():void{ var url:String ="http://192.168.1.102/blazeds/"; //随便的以个blazeds应用 如果192.168.1.102改成 localhost后其它的电脑就访问不了。 // var myStreamingAMF:StreamingAMFChannel = new StreamingAMFChannel(url+"my-streaming-amf", url+"messagebroker/streamingamf"); var myPollingAMF:AMFChannel = new AMFChannel(url+"my-polling-amf", url+"messagebroker/amfpolling"); myPollingAMF.pollingEnabled = true;//轮询 myPollingAMF.pollingInterval = 1000;//隔多长时间询问一次 1miao var channelSet:ChannelSet = new ChannelSet(); // channelSet.addChannel(myStreamingAMF); channelSet.addChannel(myPollingAMF); messageProducer.channelSet = channelSet; messageConsumer.channelSet = channelSet; messageConsumer.subscribe(); } private function addMessageToArea(message:String):void { chatMessagesArea.text = chatMessagesArea.text + "\n" + message; } private function handleResponse(event:MessageEvent):void { var message:String = null; message = event.message.headers["sender"] + ": " + event.message.body; addMessageToArea(message); } private function sendMessage():void { var message:AsyncMessage = new AsyncMessage(); message.headers = new Array(); message.headers["sender"] = "sunny"; message.body = chatMessageTxt.text; messageProducer.send(message); chatMessageTxt.text = ""; } private function handleFaults(event:MessageFaultEvent):void { Alert.show(event.faultString); } private function handleAcknowledgements(event:MessageAckEvent):void { trace("done"); } ]]> </mx:Script> <mx:Producer id="messageProducer" destination="mychat" fault="handleFaults(event);" acknowledge="handleAcknowledgements(event)" /> <mx:Consumer id="messageConsumer" destination="mychat" fault="handleFaults(event)" message="handleResponse(event)" /> <mx:VBox width="100%" height="100%"> <mx:TextArea id="chatMessagesArea" editable="false" width="100%" height="100%" fontFamily="Georgia" fontSize="19" color="#2194AD"/> <mx:HBox width="100%" height="27"> <mx:TextInput id="chatMessageTxt" width="100%" height="100%" fontFamily="Georgia" fontSize="15" enter="sendMessage()"/> <mx:Button id="chatMessageBtn" label="Send" width="64" height="100%" click="sendMessage();"/> </mx:HBox> </mx:VBox> </mx:Application> 3.写一个java类MessageSender package com.kinorsoft; import java.util.Date; import flex.messaging.MessageBroker; import flex.messaging.messages.AsyncMessage; public class MessageSender { public void sendMessageToClients(String messageBody) { AsyncMessage msg = new AsyncMessage(); msg.setClientId("Java-Based-Producer-For-Messaging"); msg.setTimestamp(new Date().getTime()); //you can create a unique id msg.setMessageId("Java-Based-Producer-For-Messaging-ID"); //destination to which the message is to be sent msg.setDestination("mychat"); //set message body msg.setBody(messageBody != null?messageBody:""); //set message header msg.setHeader("sender", "From the server"); //send message to destination MessageBroker.getMessageBroker(null).routeMessageToService(msg, null); } } 4.MessageSender.jsp调用上面的MessageSender.java <%@page import="com.kinorsoft.MessageSender" %> <% try { com.kinorsoft.MessageSender msg = new com.kinorsoft.MessageSender(); msg.sendMessageToClients("This is from the jsp page"); out.println("sent message"); } catch(Exception e) { e.printStackTrace(); } %> 5.修改一下 WEB-INF/flex/messaging-config.xml 在<service>下加上 <destination id="mychat"></destination> 声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接
|
|
返回顶楼 | |
发表时间:2010-07-20
楼主还是长轮询,还是在隔一段时间,主动访问服务器,服务器压力很大啊,每一秒就要建立连接,请求好了,再关闭。不适合大量的用户使用。
|
|
返回顶楼 | |