Universal Messaging 9.10 | Universal Messaging Developer Guide | Web Client APIs | Web Developer's Guide for Adobe Flex | Examples | Flex Example : Chat Application
 
Flex Example : Chat Application
A Sample Adobe Flex Chat Client
The code shown below is a exert of the chat client, containing Flex connection, publishing and subscription logic.

/*
Copyright 2012 Software AG, Darmstadt, Germany and/or Software AG USA
Inc., Reston, United States of America, and/or their licensors.

In the event that you should download or otherwise use this software
you hereby acknowledge and agree to the terms at
http://um.terracotta.org/company/terms.html#legalnotices
*/

import com.pcbsys.nirvana.client.*;
import com.pcbsys.foundation.util.Long;

var mySession:nSession;
var chatChannel:nChannel = null;
private var myNick:String = "";

/* *******************************************************************
* About this demonstration Universal Messaging Client Application:
*
* When this application component is created, Flex will invoke the
* startDemo() function. startDemo() calls createUniversal MessagingSession() which
* in turn calls mySession.init(). One of the parameters passed to
* mySession.init() is the name of the callback function to be invoked
* after a successful session initialization (in this case, we have
* named this callback function "sessionInitCB").
*
* In turn, sessionInitCB calls mySession.findStore(), again passing
* the name of a callback function to be invoked after findStore
* completes (in this case, "chatChannelFoundCB").
*
* Similarly, chatChannelFoundCB() calls channel.addSubscriber()
* passing in the two callback functions - one to be invoked
* when subscription to the channel is successful ("postPubCB"), and
* the other to be invoked every time an event is received on the
* channel ("evtCB"). Note that although the developer is free to name
* these callback functions, the functions themselves must accept the
* parameters shown here; for example, the callback function we have
* named evtCB will always receive an nConsumeEvent object as a
* parameter.
* ******************************************************************/

private function startDemo():void {
showUIMessage("Initializing Session...");
createUniversal MessagingSession();
}

private function createUniversal MessagingSession():void {

var RNAME:String = "nhp://localhost:80";
var appName:String = "Universal MessagingFlexSimpleChatRoomDemo";
var tmpId:String = mySession.getSessionId().toString(10);
tmpId = tmpId.substr(tmpId.length - 5);
myNick = "Flex-Native" + tmpId;


try {
// Create a Universal Messaging session attribute to be passed to the nSession
var attributes:nSessionAttributes = new nSessionAttributes(RNAME, 5);

// Create a Universal Messaging Session Object (nSession):
mySession = nSessionFactory.create(attributes, "username", appName, errorCB);

// Now, start our session. Here we specify the callback for
// when initiation has completed, we also specify a nConnectionListener
// in this case it has been implemented in this class thus "this".
mySession.init(sessionInitCB, this);

}
catch(e:SecurityError) {
}
}

/* *******************************************************************
* Error callback
*
* This function is called if a error is thrown from the server
*
* ******************************************************************/
private function errorCB(error:Error, failedFunction:Function):void {
//trace(error.message);
}

/* *******************************************************************
* Session Init callback
*
* This function is called once the session has initialised, it looks
* for the channel that the chat is on
*
* ******************************************************************/
public function sessionInitCB():void {
mySession.findStore("flex", chatChannelFoundCB);
}

/* *******************************************************************
* Session Callback Functions
*
* These are the implementations of nConnectionListener.
*
* ******************************************************************/


public function disconnected():void {
showUIMessage("Disconnected. Reconnecting");
}

public function reconnected():void {
hideUIMessage();
}

public function retry(failureCount:int, realmName:String):Boolean {
return false;
}

/* *******************************************************************
* Chat Channel Callback Functions:
*
* These three callback functions are kicked off sequentially, starting
* as a response to the mySession.findStore call above.
*
* chatChannelFoundCB is the function called in response to a findChannel
* request will receive a parameters - an nChannel object.
*
* postSubCB is the function called in response to a successful subscription
* to a channel.
*
* go the nEventListener implementation, this function is called when an
* event is received will receive an nConsumeEvent object as its parameter.
*
* ******************************************************************/

public function chatChannelFoundCB(channel:nChannel):void {
chatChannel = channel;
channel.addSubscriber(this, Long.ZERO, postSubCB);
}

public function postSubCB():void {
hideUIMessage();
}


public function go(evt:nConsumeEvent):void {
var dictionary:nEventProperties = evt.properties;

var sender:String = dictionary.get("sender").toString();
var message:String = dictionary.get("message").toString();
var stime:String = evt.attributes.timestamp.toString();
var num:Number = new Number(stime);
var dt:Date = new Date();
dt.setTime(num);
var when:String = dt.toLocaleTimeString();

messages.htmlText += "[" + when + "] " + sender + " : " + message + "\n";
messages.validateNow();
messages.verticalScrollPosition = messages.maxVerticalScrollPosition;
}
/* *******************************************************************
* This function publishes a message to the channel that we are
* connected to.
*
* In this instance it is a chatChannel so the message has been packed
* accordingly.
* ******************************************************************/
public function publishMessage():void {
var dict:nEventProperties = new nEventProperties();
dict.put("message", messageInput.text);
dict.put("sender", myNick);
var evt:nConsumeEvent = new nConsumeEvent();
evt.properties = dict;
chatChannel.publish(evt);
}


/* *******************************************************************
* UI & Utility Functions:
* ******************************************************************/

public function showUIMessage(msg:String):void {
progressbar.x = (this.width / 2) - (progressbar.width / 2);
progressbar.y = (this.height / 2) - (progressbar.height / 2);
progressbar.label = msg;
progressbar.visible = true;
uioverlay.alpha = .7;
uioverlay.visible = true;
}

public function hideUIMessage():void {
progressbar.visible = false;
uioverlay.alpha = 0;
uioverlay.visible = false;
}

Copyright © 2013-2019 | Software AG, Darmstadt, Germany and/or Software AG USA, Inc., Reston, VA, USA, and/or its subsidiaries and/or its affiliates and/or their licensors.