INTRODUCTION
In this tutorial we will analyze the most important aspects of the server side programming using Actionscript with SmartFoxServer PRO.
It is required that you are already familiar with the basic client-side concepts found in the previous articles, so if you think something is not clear yet, we reccomend to review those topics before proceeding.
EXTENSIONS IN A NUTSHELL
Basically an extension has four different tasks to accomplish:
1) Initialize
2) Handle client requests
3) Handle internal server events
4) Destroy
These four tasks are handled respectively by four Actionscript functions called: init(), handleRequest(), handleInternalEvent(), destroy(). These four functions should always be declared in your extensions, even if they don't contain code. If only one of them is missing, SmartFoxServer PRO will throw an error and it will refuse to load that extension.
With that said the simplest extension possible would look like this:
// The simplest extension possible
function init()
{
// Initialization code goes here
}
function destroy()
{
// Here we release resources before the extension is destroyed
}
function handleRequest(cmd, params, user, fromRoom)
{
// Here we handle client requests
}
function handleInternalEvent(evtObj)
{
// Code for handling server events goes here
}
» The init() method is called as soon as the extension is loaded, so you can put all your initialization code there.
» The destroy() function is called when an extension is going to be destroyed by the server: for example if you are reloading it from the Administrator Tool or if the server is being restarted. It's very important to use this method to clear the setInterval(s) you have created previously.
» The handleRequest() function takes four parmaters:
cmd |
|
This is the name of the request (or command) sent by the client. Command names are strings and they identify the different actions that an extension can perform. Always try to keept these names as short as possibile to avoid wasting bandwidth.
Example: instead of calling your command "updatePlayers" try using something shorter like "upd", "updPl", etc. |
params |
|
This is an object containing data sent by the client. The object can contain numbers, strings, booleans, arrays and objects. |
user |
|
This is the User object representing the client that sent the request. |
fromRoom |
|
The numeric id of the room where the request is coming from. |
» The handleInternalEvent() is responsible for taking care of server events (user join, user exit, login, etc.).
The server calls this method in your extension by passing an object with a name property that identifies the type of event being sent. You can learn more about the types of event in the Server-side Actionscript API section of the SmartFoxServer documentation.
This is the list of the server events:
userJoin |
|
a user has joined the room / zone |
userExit |
|
a user has left the room |
userLost |
|
a user was disconnected |
newRoom |
|
a new room was created in the zone |
roomLost |
|
a room was destroyed in the zone |
loginRequest |
|
a user is sending a login request |
spectatorSwitched |
|
a spectator in a game room was turned into a player |
This is the basic architecture of an extension. The next articles of this "pro" series will guide you through the many possibilities of the server side extensions.
|