Sunday, February 6, 2011

How to make Chat in Flex

In this chapter we will make simple chat in flex.Flex have 2 component. First is Producer, Producer have a utility to send a message to every client. Second is Consumer, consumer have function to accept every message when sent by producer. At bellow is a sample chat with flex:





import mx.controls.Alert;
import mx.messaging.events.MessageEvent;
import mx.messaging.events.MessageFaultEvent;
import mx.messaging.messages.AsyncMessage;

public function onLoad():void{
_consumer.subscribe();
}
public function sendMessage():void {
var message:AsyncMessage=new AsyncMessage();
message.body=_txtInputUser.text+" : "+_txtInputMessage.text;
_producer.send(message);
}
public function messageHandler(event:MessageEvent):void {
_txtArea.text+=event.message.body.toString() +"\n";
_txtInputMessage.text="";
}
public function faultHandler(event:MessageFaultEvent):void {
_txtArea.text=event.faultString;
}
]]>































Note:
1. At Line 2- 25, Make Script and method.
2. At Line 26, Producer component (ID=”_producer”).
3. At Line 27, Consumer component (ID=”_consumer”)
4. At Line 28-54, Declaration User Interface in flex.

Explanation:
1. At method sendMessage it use to send message using procedure component and it will fire when Button is pressed . Message will be retrieved from textInput (with ID =_txtInputUser and _txtInputMessage) and will be stored at SyncMessage.
2. At method messageHandler have a utility to accept every asnycmessage.

After you paste code at above there a have a little setting. Go to LCDS Folder on your computer after that find remotig-config.xml copy the code bellow at xml file (before ""):




true
8




after copt that save a change. go to at messaging-service.xml and copt code at bellow:





Result:


Source Code:
SimpleChat.mxml

Okay that's it and congratulation you have made simple chat using flex. I hope this tutorial can help you..Have a nice day...^^

No comments:

Post a Comment