Case study: chat

Source: Internet
Author: User

Chat:

This demo shows the message service of blazeds. It is a simple chat room in publish/subscriber mode.

I. Run the demo:
1. Run the program: http: // localhost: 8400/spring-Flex-testdrive/chat/index.html;
2. Access the same URL in another browser window and open the second chat program instance.
3. Enter a message in a chat client and click "send". The message is displayed in two chat clients.

Ii. Understand the Code:

1. Producer/consumer of chat. mxml

MNS manages a set of destination collections that can be published and subscribed to by Flex clients. Flex provides the producer and consumer components for publishing and subscribing to destination. Use the subscribe method of the consumer class to subscribe to the target (destination). When a message is published to the target you subscribe to, the message event is triggered on the consumer.
<Mx: Producer id = "producer" Destination = "chat" channelset = "{CS}"/>
<Mx: Consumer id = "consumer" Destination = "chat" channelset = "{CS}" message = "messagehandler (event. Message)"/>

2. Chat. mxml channelset
The channelset of producer and consumer is CS and the destination is chat.
Chanelsetapi allows you to dynamically define endpoints at runtime. In this example, the endpoint URL is hard-coded in the program. In actual situations, client programs usually dynamically read these parameters. For example, when a client program starts, it uses httpservice to load XML files and read these parameters. The first channel added to the channelset serves as the preferred channel to try to connect to the server. If the preferred channel fails, the system attempts to connect using the second channel defined in the channelset.
<Mx: channelset id = "CS">
<Mx: streamingamfchannel url = "http: // localhost: 8400/spring-Flex-testdrive/messagebroker/streamingamf"/>
<Mx: amfchannel url = "http: // localhost: 8400/spring-Flex-testdrive/messagebroker/amflongpolling"/>

<Mx: amfchannel url = "http: // localhost: 8400/spring-Flex-testdrive/messagebroker/amfpolling"/>
</MX: channelset>
When the program starts, consumer subscribes to chat messages.
Consumer. subscribe ();

3. Chat. mxml asynchronous message

A message is sent by the product by creating an asynchronous message.
VaR message: iMessage = new asyncmessage ();
Message. Body. userid = userid. text;
Message. Body. chatmessage = msg. text;
Producer. Send (Message );

The asyncmessage body is of the object type and contains the specific data that needs to be passed to the remote target.

4. consumer receives message events

When a message event occurs, the message event on the consumer is triggered and processed by the program through messagehandler.
Private function messagehandler (message: iMessage): void
{
Log. Text + = message. Body. userid + ":" + message. Body. chatmessage + "\ n ";
}

5. flex-servlet.xml

Open the flex-servlet.xml to view Message Service configurations. Messageservice is configured using <flex: Message-service/> in <flex: Message-broker/>.
<Flex: Message-broker>
<Flex: Message-Service
Default-channels = "My-streaming-AMF, my-longpolling-AMF, my-polling-AMF"/>
<Flex: Secured/>
</Flex: Message-broker>
The default channel for message-service is my-streaming-AMF, my-longpolling-AMF, And my-polling-AMF is defined in WEB-INF \ flex \ services-config.xml.

6. chat message service configuration

In the flex-servlet.xml, use <flxe: Message-destination ID = "chat"/> to configure the messaging service for chat.

Iii. Summary:

You only need to configure the message service in the background when using the message service of blazeds as a chat room. The core part of the publishing/subscriber mode has been implemented by blazeds, but this is just a simple chat program. This feature of Message Service is also applied in the following demos.

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.