Skip to content

Instantly share code, notes, and snippets.

@imageaid
Created December 13, 2010 20:19
Show Gist options
  • Save imageaid/739532 to your computer and use it in GitHub Desktop.
Save imageaid/739532 to your computer and use it in GitHub Desktop.
A very simple MXML Application component to handle server-side messaging
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
creationComplete="initSubscribers()"
layout="absolute"
minWidth="640"
minHeight="480">
<!-- The two consumer tags setup the connection to our ActiveMQ topics -->
<!-- Note how the value of the destination attribute matches our messaging-config.xml destinations -->
<!-- The selector attribute is used to filter messages in a topic for a specific client. For the purpose of this example, I've hard-coded the value whereas in a real application, we set the .NUMBER dynamically -->
<!-- The last important attribute are your handlers, of course. Don't forget those :) -->
<mx:Consumer id="consumerFriendChatNotifer"
selector="subscriberSelector='friendchatroom.1'"
resubscribeAttempts="5"
resubscribeInterval="30000"
destination="JMSMessages"
message="{friendNotifierMessageHandler( event )}"
fault="{friendNotifierFaultHandler( event )}" />
<!-- Not much here. Same as the above mx:Consumer tag but there is no need for a selector attribute as this is a consumer for a topic that goes to all Flex clients -->
<mx:Consumer id="consumerAnnouncements"
resubscribeAttempts="5"
resubscribeInterval="30000"
destination="JMSAnnouncements"
message="{announcementMessageHandler( event )}"
fault="{announcementFaultHandler( event )}" />
<mx:Script>
<![CDATA[
import mx.controls.Alert;
import mx.messaging.events.MessageEvent;
import mx.messaging.events.MessageFaultEvent;
// To start, we need to have our consumers actually subscribe!
// this method is called when the creationComplete event is fired for this component
private function initSubscribers():void{
consumerFriendChatNotifer.subscribe();
consumerAnnouncements.subscribe();
}
// for our example, I'm not going to do anything other than spit out an alert with the message from the server.
private function friendNotifierMessageHandler(event:MessageEvent):void{
Alert.show(event.message.body.message.toString(),"General Announcement");
}
private function friendNotifierFaultHandler(event:MessageFaultEvent):void{
Alert.show(event.message.toString(),"Fault with Message");
}
private function announcementMessageHandler(event:MessageEvent):void{
Alert.show(event.message.body.message.toString(),"Specific-Client Announcement");
}
private function announcementFaultHandler(event:MessageFaultEvent):void{
Alert.show(event.message.toString(),"Fault with Message");
}
]]>
</mx:Script>
</mx:Application>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment