Archive for the ‘SmartFox Server’ Category

Advanced Chat Application-Part 2

November 9th, 2010 by admin | No Comments | Filed in Advanced Chat Application

lets move to the few additional functions where we are going to create new room , join Selected room etc ..


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.onRoomAdded, onRoomAdded)
smartFoxObject.addEventListener(SFSEvent.onRoomDeleted, onRoomDeleted)
smartFoxObject.addEventListener(SFSEvent.onCreateRoomError, onCreateRoomError)

smartFoxObject.addEventListener(SFSEvent.onLogout, onLogout)
}

as above mentioned we have added three additional sfs events where on room creation , deletion and on any room creation error dispatched the functions .. now lets discuss the code bit more ..




public function onRoomAdded(evt:SFSEvent):void {

var room:Room = evt.params.room
chatWindow_mc.roomList.addItem( {label:room.getName(),data: room.getId() });
chatWindow_mc.roomList.invalidateList();
}

public function onRoomDeleted(evt:SFSEvent):void {
updateRoomList(evt);
}

public function onCreateRoomError(evt:SFSEvent):void {
trace("error "+evt.params.error);
}


onRoomAdded : we need to update the room list and add the current room.

onRoomDeleted:we need to update the room list and delete the current room.

onCreateRoomError: here we will be able to see the error if on creation of room we will get any error.

Now how we are going to create the new room ? :)




private function btnNewRoom_click(evt:MouseEvent):void {
chatWindow_mc.createRoomWindow_mc.visible = true
chatWindow_mc.createRoomWindow_mc.ok_btn.addEventListener(MouseEvent.CLICK,handleCreateRoom)
}

private function handleCreateRoom(evt:MouseEvent):void {
chatWindow_mc.createRoomWindow_mc.visible = false
var roomName:String = chatWindow_mc.createRoomWindow_mc.room_name.text

if (roomName.length > 0)
{

var roomObj:Object = {}
roomObj.name = roomName
smartFoxObject.createRoom(roomObj)

}

}


for that we are going to use above mentioned function. Now to join a specific room :




private function JoinSelectedRoom(evt:ListEvent):void {

if(chatWindow_mc.roomList.selectedItem != null){
var roomId:int = int(chatWindow_mc.roomList.selectedItem.data)
smartFoxObject.joinRoom(roomId)
}
}


So now our advanced chat is almost completed .. why almost ? :) becuase there are many more actions  which can be included in advanced chat

for example adding password protected room, defining the max number of user , making buddy etc… :)

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

Advanced Chat Application-Part 1

November 7th, 2010 by admin | No Comments | Filed in Advanced Chat Application, SmartFox Server

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 :


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);

}
}

}

}
}


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

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

Simple Chat Application:Part- 5

October 30th, 2010 by admin | No Comments | Filed in Simple Chat Application : Flash CS4

In handleSmartFoxEvents() function we have called  following functions

//Here we are going to handle all the smartfoxEvents  for now here I am showing  6 function later

We will need to add more.




private function handleSmartFoxEvents():void{

smartFoxObject.addEventListener (SFSEvent.onExtensionResponse, onExtensionResponse);

smartFoxObject.addEventListener(SFSEvent.onRoomListUpdate, onRoomListUpdate)

smartFoxObject.addEventListener(SFSEvent.onUserCountChange, onUserCountChange)

smartFoxObject.addEventListener(SFSEvent.onJoinRoom, onJoinRoom)

smartFoxObject.addEventListener(SFSEvent.onJoinRoomError, onJoinRoomError)

smartFoxObject.addEventListener(SFSEvent.onPublicMessage, onPublicMessage)

smartFoxObject.addEventListener(SFSEvent.onPrivateMessage, onPrivateMessage)

smartFoxObject.addEventListener(SFSEvent.onUserEnterRoom, onUserEnterRoom)

smartFoxObject.addEventListener(SFSEvent.onUserLeaveRoom, onUserLeaveRoom)

smartFoxObject.addEventListener(SFSEvent.onLogout, onLogout)

}


Now lets discuss it in detail :

onExtensionResponse() : this  will give us all server responses defined in the server side. Dispatched when a command/response from a server-side extension is received.

onRoomListUpdate(): this event  is dispatched when the list of rooms of  the current zone is received.
If the default login mechanism provided by SmartFoxServer is used, then this event is dispatched right after a successful login.

onUserCountChange(): this event is dispatched when the number of users and/or spectators changes in a room of the current zone. This event allows to keep track in real-time of the status of all the zone rooms in terms of users and spectators. If any user joins or leave the room it gets updated automatically.

onJoinRoom (): this event is dispatched when a user joins any room  successfully

onJoinRoomError(): while joining a room any error occurs, this event is dispatched. This error could happen, for example, if the user is trying to join a room which is currently full  it will throw an error.In our application we would like to trace the error  so we will get using evt.params.error

onPublicMessage(): this event is dispatched when a user  want to send a public message for all the users.

onPrivateMessage ():this event is dispatched when a user  want to send a public message for all the users.

onUserEnterRoom(): this event is dispatched when another user joins the current room.

onUserLeaveRoom(): this event is dispatched  when a user leaves the current room. This  event is also dispatched when a user gets disconnected from the server.

onLogout (): Dispatched when the user logs out successfully. After a successful logout the user is still connected to the server, but he/she has to login again into a zone, in order to be able to interact with the server. When a user will want to logout from the screen he will need to click on the logout button. On clicking log out button this function will be called.

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

Simple Chat Application:Part 2

October 18th, 2010 by admin | No Comments | Filed in Simple Chat Application : Flash CS4, SmartFox Server

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 admin | 1 Comment | Filed in SmartFox Server

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 admin | 2 Comments | Filed in SmartFox Server

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