Friday 7 January 2011

SFS1X: Simple Extension

Hi. In this tutorial I'll explain how to make the most Simple Extension, most known as "Hello World" Extension.

Importat Note: This is a Zone Level Extension, so it's attached to a Zone. There are the Room Level Extensions too, but the only difference is that they're attached to a Room instead.

An Extension, as the name says, is a way to extend the server functionalities. It has many uses like handling the Login proccess (by verifying the credentials in a database), handling a game (like the score of each player in the game) and any other things.

So let's begin the tutorial!

First you need to create a new ActionScript File. Then you only need to put there this code:
function init()
{
 trace("Hello World!")
}

function destroy(){
 trace("Bye World!")
}

function handleRequest(cmd, params, user, fromRoom){
 trace("Request Received from: "+user.getName())
}

function handleInternalEvent(evt){
 trace("Event received: "+evt.name)
}
As you can see, the Extension has only this functions (plus the handleRequest function) to handle the server events. 

The init function handles the init event that is fired when the extension is inited (after the Zone is inited). In this case, we only display the "Hello World!" message, that can be checked in the console, after the correspondent Zone is inited.

The destroy function handles the destroy event that is fired when the extension is destroyed. In this case, we only display the "Bye World!" message, that can be checked in the console.

The handleInternalEvent function handles all the internal events that are fired, like userJoin, userExit, etc. In this case we only display the name of the event received. 

The handleRequest function handles the request event sent by the client (with the sendXtMessage). 

Now you only need to deploy the Extension. To do that, you need to copy you ActionScript File to the [SmartFoxServer Installation Folder]\Server\sfsExtensions folder, then you need to add your Extension to the Zone where you want to place it. For that you need to enter the following code, in the <Extensions> block that is in your Zone block. So it will stay like this:

<Zone name="Your Zone" blablablabla>
        bla
        bla
        bla
        <Extensions>
                <extension name="MostSimpleExtension"  className="MostSimpleExtension.as" type="script" />
        </Extensions>
</Zone>

The extension configuration has the following parameters:

name - Is a name that will be used to refer to the Extension. Used in the sendXtMessage(extensionName, blablabla)
className - Is the name of the ActionScript File of the Extension.
type - Is the type of the extension. As it is an ActionScript extension, its type is script. If it was a Java Extension, its type would be java.

After this, you only need to start your Server.

And that's all. I hope you enjoyed this tutorial. If you have any questions, fell free to ask me (by commenting or by dropping me a pm in the sfs forums).

No comments:

Post a Comment