6.1 Introduction to Server Side Extensions
The idea behind a server side extension is very simple:
it allows developers to extend the server functionalities allowing them to
create all kinds of new features and behaviors.
SmartFoxServer PRO comes with a standard set of features
that cover all the basic tasks of a multi user server: handling applications,
user connections, room
creation and destruction, broadcasting user messages etc..
With these built-in features you can already create a good number of multiplayer
applications, but when things get more sophisticated, it is necessary to have
new behaviors at hand. Server side extensions are an easy and powerful tool
for developers to implement simple to very complex application logic on
the server side.
» An Example
If you have already been using SmartFoxServer Lite
or Basic you should
know the SmartFoxTris example, essentially it is a multiplayer
tic-tac-toe game.
Using the server-side variables we can keep the game logic all on the client
without worrying of the server side.
Let's say we now want to make it more complex: we'd like to add registered
user support with a high score table, user profile and user registration. With
the SFS Extensions you can implement all the registration/login/high
score handling on the server side pretty easily.
You can connect to your existing database, retrieve and modify user data, save
high scores and even send html-formatted confirmation emails!
» How do Extensions work?
Extensions can be written using Java or Actionscript
1.0 (Javascript/ECMA-262).
Using the provided Actionscript framework is the easiest
and more productive way of creating your own custom server logic, and these
articles will be focused on the Actionscript framework.
Basically an extension consists of a simple .as file (just like you would do in Flash) saved in the sfsExtensions/ folder, which is the root directory for all Actionscript extensions. You can also create your own folder structure starting from the root, to better organize your .as files.
The next step is to tell the server that you have created an extension for a certain Zone or Room (more on this in the next chapters) and activate it, by modifying the config.xml. Below follows an example of the XML used to load an extension.
<Extensions> <extension name="myExt" className="myExtension.as" type="script" /> </Extensions>
var gameRoom:Object = new Object() gameRoom.name = name gameRoom.password = pwd gameRoom.maxUsers = 2 gameRoom.maxSpectators = spec gameRoom.isGame = true gameRoom.isTemp = true xt = {} xt.name = "tris" xt.script = "sfsTris.as" gameRoom.extension = xt smartfox.createRoom(gameRoom)
server.sendXtMessage("myExt", "helloWorld", obj)
smartfox.onExtensionResponse = function(resObj:Object, type:String) { var cmd:String = resObj._cmd if (cmd == "myCommand") { // Handle the response here } }
doc index |