Posts Tagged ‘btnLogin_click’

Advanced Chat Application-Part 1

November 7th, 2010 by aabhaanjan

Now Lets move to develop an advanced chat application  using  SmartFoxserver :

here we are going to add following actions in our existing simple chat application :

1. Room list creation.

2. User can send private message as well.

3. User can join a specific room.

4. User can exit from a specific room.

5. User can create his/her new room.

our main chat window will now look like this :

advanced_chat_app

lets move to the code part. Here I am going to complete the point 1 & 2 of above mention task list :).

After  addition of some code for above  two points  in our simple chat application the code will look like this :

[xml]

package {

import flash.display.MovieClip;
import flash.events.MouseEvent;
import flash.events.Event;
import flash.events.IOErrorEvent;
import flash.net.*;
import fl.controls.DataGrid;
import fl.data.DataProvider;
import fl.events.ListEvent;

//——————————import SmartFoxServer classes
import it.gotoandplay.smartfoxserver.SFSEvent;
import it.gotoandplay.smartfoxserver.data.Room;
import it.gotoandplay.smartfoxserver.data.User;
import it.gotoandplay.smartfoxserver.SmartFoxClient;

public class ChatManager extends MovieClip {

private var smartFoxObject:SmartFoxClient;
public var serverZone:String;// Defining the server zone
public var serverIp:String;
public var serverPort:int;
private var selectedUserId:int;

// Calling the Constructor……..
public function ChatManager() {
trace( ” ChatManager  called “);
init();
}

//initializing the config load function
private function init():void {
smartFoxObject= new SmartFoxClient();
serverZone=”simpleChat”;
loadConfigData();
}

//As we have done earliar we will start from the loading config
//And we will call the smartFoxServer’s Events

private function loadConfigData():void {
var loader:URLLoader=new URLLoader  ;
loader.addEventListener(Event.COMPLETE,onConfigLoaded);
loader.addEventListener(IOErrorEvent.IO_ERROR,onConfigLoadFailed);
loader.load(new URLRequest(‘XML/config.xml’));

}

private function onConfigLoaded(evt:Event):void {

connectionEventsOfSFS();
handleConnectionToSFS(evt);
}

private function onConfigLoadFailed(evt:Event):void {

connectionStatus_Mc.msg_txt.text = ” Config Loading is Failed”
}

private function connectionEventsOfSFS():void {

smartFoxObject.addEventListener(SFSEvent.onConnection, onConnectedToSFS);
smartFoxObject.addEventListener(SFSEvent.onConnectionLost, onConnectionLost);
login_MC.login_btn.addEventListener(MouseEvent.CLICK,btnLogin_click);
handleSmartFoxEvents();

}

private function handleConnectionToSFS(evt):void {

var loader:URLLoader=evt.target as URLLoader;
var xmlDoc:XML=new XML(loader.data);
serverIp=xmlDoc.ip;
serverPort=int(xmlDoc.port);
serverZone=xmlDoc.zone;
connectToSFS();
}

public function connectToSFS() {
smartFoxObject.connect(serverIp,serverPort);

}

public function onConnectedToSFS(evt:SFSEvent) {
login_MC.visible=true;
login_MC.server_status_txt.text=”Connected”;
connectionStatus_Mc.visible = false            ;
}

public function onConnectionLost(evt:SFSEvent) {
login_MC.server_status_txt.text=”Disconnected”;
}

public function btnLogin_click(evt:Event):void {
sendLoginRequest();
}

private function sendLoginRequest():void {
if (login_MC.username_txt.text.length>0&&login_MC.pwd_txt.text.length>0) {
var USER_NICK=login_MC.username_txt.text;
var USER_PASSWORD=login_MC.pwd_txt.text;
smartFoxObject.login(“simpleChat”, USER_NICK, “”);

} else {
login_MC.msg_txt.text=”Error, Login and Password should not be blank!”;
}
}

//Here we are going to handle all the smartfoxEvents
private function handleSmartFoxEvents():void {

smartFoxObject.addEventListener(SFSEvent.onExtensionResponse, onExtensionResponse);
smartFoxObject.addEventListener(SFSEvent.onRoomListUpdate, onRoomListUpdate);
smartFoxObject.addEventListener(SFSEvent.onJoinRoom, onJoinRoom);
smartFoxObject.addEventListener(SFSEvent.onJoinRoomError, onJoinRoomError);
smartFoxObject.addEventListener(SFSEvent.onUserCountChange, onUserCountChange);
smartFoxObject.addEventListener(SFSEvent.onUserEnterRoom, onUserEnterRoom);
smartFoxObject.addEventListener(SFSEvent.onUserLeaveRoom, onUserLeaveRoom);
smartFoxObject.addEventListener(SFSEvent.onPublicMessage, onPublicMessage);
smartFoxObject.addEventListener(SFSEvent.onPrivateMessage, onPrivateMessage);
smartFoxObject.addEventListener(SFSEvent.onLogout, onLogout)
}

public function onExtensionResponse(resObj:Object):void {

if (resObj.params.dataObj._cmd==”logOK”) {

moveToThelobby();

return;
}
if (resObj.params.dataObj._cmd==”logKO”) {
login_MC.msg_txt.text=”Login is Failed”;
return;
}
}

private function moveToThelobby():void {
login_MC.visible=false;
chatWindow_mc.visible=true;
chatWindow_mc.pmWindow_mc.visible=false;
smartFoxObject.getRoomList();

chatWindow_mc.send_btn.addEventListener(MouseEvent.CLICK,btnSend_click);
chatWindow_mc.logout_btn.addEventListener(MouseEvent.CLICK,btnLogOut_click)
chatWindow_mc.userList.addEventListener(ListEvent.ITEM_CLICK, selectUserForPrivateMessage);
}

public function onRoomListUpdate(evt:SFSEvent):void {
updateRoomList(evt);
smartFoxObject.autoJoin();
}

public function onJoinRoom(evt:SFSEvent):void {

updateUserList(evt);
}

public function onJoinRoomError(evt:SFSEvent):void {

}

public function onUserCountChange(evt:SFSEvent):void {
updateUserList(evt);
}

public function onUserEnterRoom(evt:SFSEvent):void {
updateUserList(evt);
}
public function onUserLeaveRoom(evt:SFSEvent):void {
updateUserList(evt);
}

public function onPrivateMessage(evt:SFSEvent):void {
chatWindow_mc.chat_txt.htmlText+=”<b>[ “+evt.params.sender.getName()+” ]:</b> “+evt.params.message;
chatWindow_mc.scrollPanel.scrollTarget=chatWindow_mc.chat_txt;
}

public function onPublicMessage(evt:SFSEvent):void {

chatWindow_mc.chat_txt.htmlText+=”<b>[ “+evt.params.sender.getName()+” ]:</b> “+evt.params.message;
chatWindow_mc.scrollPanel.scrollTarget=chatWindow_mc.chat_txt;

}

public function onLogout(evt:SFSEvent):void {
login_MC.visible=true;
chatWindow_mc.visible=false;

}

private function sendPrivateMessage(evt:MouseEvent):void {
var message:String= chatWindow_mc.pmWindow_mc.msg_txt.text;
if (message.length>0&&selectedUserId>0) {
smartFoxObject.sendPrivateMessage(message, selectedUserId);
chatWindow_mc.pmWindow_mc.visible=false;
selectedUserId=-1;
}
}

private function removePmWindow(evt:MouseEvent):void {
}

private function updateRoomList(evt:Object):void {
chatWindow_mc.roomList.removeAll();

var roomList:Object=evt.params.roomList;

for (var j:String in roomList) {

var objRoom=roomList[j];
chatWindow_mc.roomList.addItem( {label:objRoom.getName(),data: objRoom.getId() });
chatWindow_mc.roomList.invalidateList();
}
}

private function updateUserList(evt:Object):void {

var objUser:Object=evt.params.user;
var objRoom:Object=evt.params.room;
var userList:Object=objRoom.getUserList();
var roomId=objRoom.getId();
chatWindow_mc.userList.removeAll();

for (var j:String in userList) {
objUser=userList[j];
chatWindow_mc.userList.addItem( {label:objUser.getName(), data: roomId });
chatWindow_mc.userList.invalidateList();
}
// Clear text area
chatWindow_mc.chat_txt.htmlText = “”;
chatWindow_mc.chat_txt.multiline=true;
chatWindow_mc.chat_txt.wordWrap=true;
chatWindow_mc.chat_txt.htmlText+=”<font color=’#cc0000′>>> Room [ “+objUser.getName()+” ] joined</font>”;

}

private function btnSend_click(evt:MouseEvent):void {
smartFoxObject.sendPublicMessage(evt.target.parent.msg_text.text);
evt.target.parent.msg_text.text=””;
}

private function btnLogOut_click(evt:MouseEvent):void {
smartFoxObject.logout()
}

private function selectUserForPrivateMessage(evt:ListEvent):void {

if (chatWindow_mc.userList.selectedItem!=null) {
selectedUserId=chatWindow_mc.userList.selectedItem.data;
if (selectedUserId!=smartFoxObject.myUserId) {
chatWindow_mc.pmWindow_mc.visible=true;
chatWindow_mc.pmWindow_mc.cancil_btn.addEventListener(MouseEvent.CLICK, removePmWindow);
chatWindow_mc.pmWindow_mc.ok_btn.addEventListener(MouseEvent.CLICK, sendPrivateMessage);

}
}

}

}
}

[/xml]

Now lets discuss in detail about these additional  functions and code in our next chapter 🙂

Tags: , , , , , , , , , , , , , , , ,