4.2 API Overview

The SmartFoxServer Client API are mainly based around the SmartFoxClient class.
The class allows you to invoke many methods to send server commands and requests. Also the SmartFoxClient class instance will be used to handle events sent by the server.

» Asynchronous programming and events

By working with Flash you should be already familiar with asynchronous programming. Basically each time you load variables, images or external movie clips, you have to wait for un undefined amount of time before the resource is loaded. When the loading is complete an event is fired and you can handle with a custom function called event handler.

This approach is the basis also for all multiuser / multiplayer applications as you will normally send server requests and then wait for a certain amount of time (usually a fraction of a second) before a response is sent back. In order to accomplish this you will have to write event handlers to deal with all the possible events sent by the server.

Here's a very simple example of how it works (Actionscript 2.0):

// Import the API classes
import it.gotoandplay.smartfoxserver.*
// Create an instance of the SmartFoxClient class var sfs:SmartFoxClient = new SmartFoxClient()
// Attempt a connection to the server sfs.connect("127.0.0.1", 9339) sfs.onConnection = function(success:Boolean):Void { if (success) { trace("Great! Connected successfully!") } else { trace("An error occurred, could not connect!") } }


The connect() method tries to connect to the server running on the IP address 127.0.0.1 and port 9339 (default)
It also handles the SmartFoxServer onConnection event that is fired when the connection attempt produces a response.

You will find the detailed list of properties, methods and events of the SmartFoxClient class in this section for both Actionscript 1.0 and 2.0

» Importing and using the SmartFoxClient class

In order to use the SmartFoxClient class you will need to tell Flash to include it in your current code. This is usually done by adding in the very first line of your code an Actionscript command, based on which Actionscript version you're using in your project.

For Flash MX and MX2004 using Actionscript 1.0 be sure to have this line as the very first one in your code:

#include "SmartFoxClient.as"

For Flash MX2004, Flash 8 using Actionscript 2.0 or Flex 2 using Actionscript 3.0, use this line instead:

import it.gotoandplay.smartfoxserver.*


« previous doc index