Java send message to flex (BlazeDS)

Source: Internet
Author: User


The steps are as follows:
1. Download BlazeDS. war and unzip it to the % TOMCAT_HOME %/webapps directory file named blazeds contains the WEB-INF and META-INF folders.
2. Write a flex client dsMessage. mxml
[Html]
<? 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/"; // use a blazeds application. If 192.168.1.102 is changed to localhost, other computers cannot access it.
// 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; // round robin
MyPollingAMF. pollingInterval = 1000; // The interval at which a miao is queried.
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. Write a java class MessageSender
[Java]
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 calls the above MessageSender. java
[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 ");
} Www.2cto.com
Catch (Exception e)
{
E. printStackTrace ();
}
%>
5. Modify the WEB-INF/flex/messaging-config.xml
Add
<Destination id = "mychat"> </destination>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.