8.12 Tutorials: Smart Messenger
The sources of this example is found under the Examples/AS2/20_pro_smartMessenger folder. |
» Introduction
The Smart Messenger example implements some of the new features of the latest framework (1.4.0), demonstrating how to create a simple instant messenger application.
The demo shows how a client can browse a list of available users and add them to their "contact list". Once they've been added you will be able to see their statuses in realtime (offline, online, "be right back", "busy") and start mulitple one-on-one chat sessions.
The contact list is persistent, so each user will find their buddies when they will log in again at a later time.
» The 1.4.0 framework features
The example uses a couple of new features introduced in the 1.4.0 framework:
1. Limbo Rooms: a new attribute called "limbo" is available in the Room object. Limbo rooms can be created statically in the config.xml file or dynamically from a server side extension. Limbo rooms are special rooms that can contain hundreds or thousands of clients that don't need to interact directly with each other. This type of room can be useful when the client needs to interact with the server, without the need to know the list of all the other users, their variables, etc...
For example a "limbo room" can be used for a lobby where you can setup your avatar character, edit your personal info, check a list of available games running etc... In this case many hundreds clients can enter the same room and perform these actions without the need to know what other clients are doing in the same room.
When the limbo flag is turned on, the room suppresses certain events (new user arrived, user left, user count changed, room and user variables) and inhibits the ability to send public messages. This way the room can handle thousands of clients without generating too much traffic and saving bandwidth. If clients need to communicate with each other they can use private messages, which don't need to be broadcasted to all other users.
2. Buddy Variables: the ability to attach variables to each "buddy" in the buddy list allows to send more status informations about each item in a contact list. From version 1.4.0 it is possible to set additional properties to each buddy that gets transmitted back to each client who have the buddy in their list.
We used this feature in the application to handle the status changes of each client (online, busy, etc...)
» The Smart Messenger
The whole application uses one single room called "Main" with the limbo flag set to true. All the users will be automatically joined in this room, once their credentials have been checked.
In a real world application we should have used a database, but for learning purposes we kept everything simple by defining a user data structure directly inside the extension.
function initUserData() { userData.push( {nick:"jimi", age:20, location:"U.S.A", email:"jimi@mail.us", interest:"Rock Guitar"} ) userData.push( {nick:"blaise", age:30, location:"France", email:"blaise@mail.fr", interest:"Math and Physiscs"} ) userData.push( {nick:"wolfgang", age:17, location:"Germany", email:"wolf@mail.de", interest:"Orchestral Music"} ) userData.push( {nick:"pablo", age:20, location:"Spain", email:"pablo@mail.sp", interest:"Painting"} ) userData.push( {nick:"leonardo", age:20, location:"Italy", email:"leo@mail.it", interest:"Painting, architecture, science"} ) userData.push( {nick:"dante", age:20, location:"Italy", email:"dante@mail.it", interest:"Literature"} ) userData.push( {nick:"miles", age:20, location:"U.S.A.", email:"miles@mail.us", interest:"Jazz music, trumpet"} ) userData.push( {nick:"agatha", age:20, location:"U.K.", email:"agatha@mail.co.uk", interest:"Mystery, thrillers"} ) }
The userData object will be used as an "internal database" to check if a user name is valid.
In order to test the application you will need to login as one of the available users: jimi, blaise, wolfgang, pablo, leonardo, dante, miles, agatha.
Once inside you will see your "contact list" on the left side, and the main "member browser". The latter allows the client to navigate through the user profiles of all messenger members, and add them to their contact list. As soon as a user is added, its status will be updated in realtime. Also you will be able to change your current status using the three radio buttons in the bottom of the contact list.
From the list you can also select any user and start a private conversation or remove the contact.
» Conclusions
You will find the main source file in the it/gotoandplay/smartfoxserver/smartmessenger/Messenger.as class (starting from the example folder)
The server side extension is located in the sfsExtensions/messenger.as file.
doc index |