Archive for the ‘SmartFox Server’ Category

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: , , , , , , , , , , , , , , , ,

Simple Chat Application:Part 2

October 18th, 2010 by aabhaanjan

As discussed in our last chapter lets move to the code part more deeply 🙂

So now we need to load the config and establish the connection first  our code will look like this

ChatManager

Lets start with init() function where I am initializing the smartfoxserver’s object and loading the config.

loadConfigData():- Here I am loading the configData which is basically an XML file out side in the folder

containg ip, zone and port.

onConfigLoaded(): This function is called after successful loading of config XML. Inside this function I am calling two more functions named connectionEventsOfSFS() and handleConnectionToSFS().

connectionEventsOfSFS():- here basically I am enabling the SmartFoxServer’s event for onConnection established and on connection lost.

Tags: , , , , , , , , ,

Simple Chat Application : Flash CS4 (Action Script 3.0)

October 10th, 2010 by aabhaanjan

Now we are going to develop a Chat application 🙂

Step 1 :  Server side :

C:\Program Files\SmartFoxServerPRO_1.6.6\Server

Open the Program Files folder inside that folder you will get SmartFoxServer’s   Server folder where we have to edit the config or create your own config. I am modifying the already created config according to my requirement.

chatapp

Step 2:  Add zone for the application :

zoneChatconfig

Here I am defining the zone name as “Simple Chat” where uCountUpdate is  counter for the number of  user . In Rooms tag as I need only one room for chatting so I have used one room as “The  Hall” , in maxUsers tag I have specified it 50 you can modify it according to your requirement and stated it public so that everyone can join it. For this I am using the extension name “chat” and the file “chat.as”. Well the chat.as is not going to do much for now but in later when I will extend this tutorial it will help us to make a follow.

serverChatExtenstion

Step 3:  Start the server

Now we need to start the server where in console you will get trace and the created zone. Something similar to this will be shown:

chatserverConsole

Step 4: Client Side

Now the time is for client side adventure 🙂 . As I am going to create a chat application definitely I will need some specific screens.

connectionScreen

In the first layer of Fla  I have created the connection status movie clip named connectionStatus_MC where I have taken a dynamic text box statusMsg_txt which will show status of connection with server. On load automatically we will send the connection request to the server.

loginScreen

After connection we will need on more screen for  Login. So I have created this screen. I have named this login_mc. I have used one input text box loginName_txt where user will enter his name to join the chat and butt_login to send the request to the server.

chatwindow

Lets now design the Chat Screen here I have taken 3 sub panels. One is for Chat window chatWindow_mc, second is for user list (List component) user List_mc and third one is for the message window (msg_txt) and send button butt_send as message pad. One more button is there for butt_logOut if user wish to logout he can click on the Log Out button.

Okay .. so Now it is enough design work lets move towards the CODE  🙂

So I am starting with a document class ChatManager .  The initially it will look like this

ClientSideChatCode

will move forward with the code in next chapter 🙂

Custom Login : Flash CS4 (Action Script 3.0)

October 10th, 2010 by aabhaanjan

I would like to provide the tutorial using Flash and Flex both. The first I am starting with Flash CS4 using Action script 3.0.

Step 1 :  Server side :

C:\Program Files\SmartFoxServerPRO_1.6.6\Server

Open the Program Files folder inside that folder you will get SmartFoxServer’s   Server folder where we have to edit the config or create your own config. I am modifying the already created config according to my requirement.

1a

Step 2:  Add zone for the Custom login :

b

I have added a zone named “login” in the server side config where I am defining emptyNames as false value and customLogin will be on. As I need one room so I have created it “The Hall” where maximum number of users are  defined as 50 and it is a public room. For server side extension script I have named it login.

Step 3:  Server side Extension :

For “login.as” I would like to use the example extension given by SmartFoxServer for the server side.So Now our server side extension will look like this

c

dNow Start your server and it will add a new zone “login ”.

e

Step 4: Client Side

Lets create our first screen for establishing connection.

t

In the first layer of Fla I have created the connection status movie clip named connectionStatus_MC where I have taken a dynamic text box statusMsg_txt which will show status of connection with server. On load automatically we will send the connection request to the server.


f

So now we need a login Panel I have created this panel from where user can send login request to the server. There are two input boxes username_txt and pwd_txt for User name and Password. And butt_login to send the request to the server. Also a dynamic text box msg_txt for the message display.

So Now lets Start the Client Side Code.

customLogincustomLoginCode2customLoginCode3customLoginCode4

SmartFoxServer Chapter 12 – server side config –part-9

October 10th, 2010 by aabhaanjan

server side config -part-9

Zone : Specify the combination of rooms and extensions. In other words we can say that the for one application or game generally we define one zone.

Extensions : These are the server side file which we use for the application.