[SmartFox] Connecting to the server
[ August 10, 2004 ] by Marco Lapi, a.k.a Lapo
Article 2: in this tutorial you will learn how to establish a connection from a Flash client to the SmartFoxServer Lite


In this first tutorial we will see how you load and use the SmartFoxClient object and how you establish a connection to the server.

Open the simpleConnect.fla file from the "Examples/simpleConnect" directory and have a look at the code the first frame on the timeline.

You may find two different lines based on what version of the API component you're using.
The following line is what you will find if you are using the Actionscript 1.0 API component (fMX and fMX2004):

#include "SmartFoxClient.as"

If you're using the Actionscript 2.0 API component (fMX2004 only) you'll use this line instead:

import it.gotoandplay.smartfoxserver.*

The first line will always be present in all your "SmartFoxServer" applications as it imports the SmartFoxClient class and
its relative helper classes.

stop()
ip = "127.0.0.1"
port = 9339
smartfox = new SmartFoxClient()
smartfox.onConnection = handleConnection
smartfox.connect(ip, port)

After we've stopped the timeline with a stop() command we can setup three simple variables that we will use to connect to the server: ip, port and zone.
If you're running the server locally (on the same computer where you run the examples) you can leave the default value 127.0.0.1 otherwise you should replace it with the ip address of the machine running SmartFoxServer Lite.

The default port number should always be 9339, if you have changed it for some reason then the port variable should reflect
this change.

The next 3 lines of code will do most of the "magic":

First, you create a new instance of the SmartFoxClient object, then we specify an event handler (more on this in a moment) for
the onConnect event and then we finally call the connect() method to establish a connection between client and server.

That's all! :-)

If you are a bit familiar with Flash and Actionscript the expression "event handler" should not sound new to you. In fact many of the built-in objects in Flash use events to handle situations that will occur at an undefined moment in the future.
For example when you load an external movieclip you don't know exactly when the whole clip will be available and you have
to write an event handler (a function) that will perform actions when the Flash onLoad event is fired.

SmartFoxServer uses events quite a lot because you can receive messages at any time from the server or from other users.
All you will have to do is write the appropriate functions to manage these different situations.

Back to the example of this tutorial we need to write a function that handles the connection event and takes the right action
whether the connection succedeed or not.

The following code is a simple example of connection handling:

//----------------------------------------------------------
// Handle connection response from server
//----------------------------------------------------------
function handleConnection(success)
{
if (success)
{
status_txt.text = "Connection succesfull!"
}
else
{
status_txt.text = "Can't connect!"
}
}

Upon reception of a onConnection message the handleConnection function is called.
Every time you receive this notification a boolean parameter is also sent saying if connection was successfull or not.

In a real-world example we could open a dialog box asking for username and password if the client could connect or show
an error message if it failed. This is exactly what we're going to do in the next tutorials, together with many other cool things! :-)

SmartFox can fire many different events based in the type of request it receives, some of them are:

onConnection
onLogin
onRoomListUpdate
onJoinRoom
onJoinRoomError
onPublicMessage
etc...

Anyway don't worry too much about them right now, as we'll analyze each one of them as we progress with more complex tutorials.

Always make sure to experiment with the provided code.

See you in the next article.

Lapo



    
 
 
Name: Marco Lapi, a.k.a Lapo
Location: Fossano, Italy
Age: 34
Flash experience: started out with Flash 4 back in 1999
Job: web designer/developer
Website: http://www.gotoandplay.it/
 
 
| Homepage | News | Games | Articles | Multiplayer Central | Reviews | Spotlight | Forums | Info | Links | Contact us | Advertise | Credits |

| www.smartfoxserver.com | www.gotoandplay.biz | www.openspace-engine.com |

gotoAndPlay() v 3.0.0 -- (c)2003-2008 gotoAndPlay() Team -- P.IVA 03121770048