Pages

Saturday, May 12, 2012

Connect SmartFoxServer 2X in as3



A connection to SFS2X is performed in two steps.
  1. A physical TCP connection is opened to the server
  2. An "handshake" is performed between client and server exchanging a certain number of parameters
During the handshake the server verifies that the client API version is supported and sends back a number of settings that the client stores locally.
The following snippet of ActionScript 3 code is derived from the Connector example available in the examples package (similar examples are provided for the other languages).

public class Connector extends Sprite
{  
    private var sfs:SmartFox
     
    public function Connector()
    {
        // Create an instance of the SmartFox class
        sfs = new SmartFox()
     
        // Turn on the debug feature
        sfs.debug = true
         
        // Add SFS2X event listeners
        sfs.addEventListener(SFSEvent.CONNECTION, onConnection)
        sfs.addEventListener(SFSEvent.CONNECTION_LOST, onConnectionLost)
        sfs.addEventListener(SFSEvent.CONFIG_LOAD_SUCCESS, onConfigLoadSuccess)
        sfs.addEventListener(SFSEvent.CONFIG_LOAD_FAILURE, onConfigLoadFailure)
         
        // Connect button listener
        bt_connect.addEventListener(MouseEvent.CLICK, onBtConnectClick)
 
        dTrace("SmartFox API: " + sfs.version)
        dTrace("Click the CONNECT button to start...")
    }  
     
    private function onBtConnectClick(evt:Event):void
    {
        // Load the default configuration file, config.xml
        sfs.loadConfig()
    }
 
    //:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
    // SFS2X event handlers
    //:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
     
    private function onConnection(evt:SFSEvent):void
    {
        if (evt.params.success)
        {
            dTrace("Connection Success!")
        }
        else
        {
            dTrace("Connection Failure: " + evt.params.errorMessage)
        }
    }
     
    private function onConnectionLost(evt:SFSEvent):void
    {
        dTrace("Connection was lost. Reason: " + evt.params.reason)
    }
     
    private function onConfigLoadSuccess(evt:SFSEvent):void
    {
        dTrace("Config load success!")
        dTrace("Server settings: " + sfs.config.host + ":" + sfs.config.port)
    }
     
    private function onConfigLoadFailure(evt:SFSEvent):void
    {
        dTrace("Config load failure!!!")
    }
 
    //:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
    // Utility methods
    //:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
 
    private function dTrace(msg:String):void
    {
        ta_debug.text += "-- " + msg + "\n";
    }  
}

The code starts by creating the SmartFox object, which is the main API class. We then proceed by registering the server events that we want to listen for using the SFSEvent class.
The next step is loading an external .xml configuration file that contains the details of our connection. This is a convenient way of keeping the connection parameters separate from the code so that they can be changed at any time without having to recompile the code. If you are familiar with SmartFoxServer 1.x you will find this system very similar to its predecessor.

0 comments:

Post a Comment