Flash Multiplayer Virtual World: Setting up SmartFoxServer with Third-party HTTP and Database Server
Your message has been sent.
This article has been saved to your account.
Go to my account
This article has been emailed to your Kindle.
Send this article
In the previous article of this three-part series by Makzan, author of Flash Multiplayer Virtual Worlds, we covered running SmartFoxServer Using Embedded Web Server and Database. SmartFoxServer is also able to work together with third-party web servers and databases, for example, Apache and MySQL.
Using the embedded HTTP and database server can be convenient for development. However, we will have SmartFoxServer running standalone while putting HTTP and database server in another standalone machine when the virtual world is released to public. We will learn how to set up a third-party HTTP and database server to cooperate with SmartFoxServer.
(For more resources on Flash, see here.)
We are going to download and install Apache and MySQL server package. These kinds of server package features have easy install that auto-configures most of the server settings. It will also install some essential tool for beginners to manage the server easily, such as GUI server administration panel.
Installing WAMP on Windows
WampServer is an open source HTTP and database server solution on Windows. WAMP stands for Windows, Apache, MySQL, and PHP package.
- Go to http://www.wampserver.com/en/download.php.
- Click on Download WampServer to download the installer.
- Run the installer with all default settings.
- The server is configured and ready.
The WampServer can run by launching from Start | Programs | WampServer | Start WampServer.
It will be in the task bar and the server management operation can be found by clicking the WampServer icon. We can start the server by putting the server online in the menu.
Installing MAMP on Mac OSX
Similar to WampServer, MAMP is the one package web server solution that stands for Mac, Apache, MySQL, and PHP package. The MAMP package can be downloaded at http://www.mamp.info/.
- Download the MAMP package from the official website.
- Double-click on the downloaded MAMP dmg file to mount it.
- Drag the MAMP folder into the Applications folder.
To run the MAMP server, go to Applications | MAMP and double-click on the MAMP.app.
Installing LAMP on Linux
As the same naming convention, the "L" stands for Linux here. Different Linux distributions use different ways to install applications. There may not be a oneclick install method on some Linux branch which requires us to install the Apache and MySQL individually. Some Linux may provide graphic user interface to install LAMP by just selecting it in the applications list. We will use Ubuntu to demonstrate the installation of LAMP.
- Launch terminal from Applications | Accessories | Terminal.
- Type following command to install LAMP.
sudo tasksel install lamp-server
- The installer will progress and configure different modules.
- A dialog will prompt several times asking for a new MySQL root password. You can set your own MySQL password, while in the example we will leave the root password blank.
After the completion of the installation, the MySQL server is set up as service in the system. It runs automatically and we do not need to manually launch it to use it.
Connecting SmartFoxServer and MySQL server
SmartFoxServer is a Java application and Java database connection driver is needed to connect from SmartFoxServer to MySQL database.
Downloading JDBC Driver for MySQL
JDBC is a Java database connection driver that we need to establish connections between the Java-based SmartFoxServer and the MySQL server. The JDBC driver for MySQL is called Connector/J. We are going to install it to enable MySQL connection from SmartFoxServer.
- Go to http://dev.mysql.com/downloads/connector/j/5.1.html in web browser.
- Download the Platform Independent Zip Archive.
- It may ask you to log in to MySQL.com account. Click on No thanks, just take me to the downloads! to bypass the login step.
- Choose a mirror to download by clicking on HTTP.
Setting up the JDBC driver
The MySQL Java connector comes with a bunch of files. We only need two among them.
- Extract the mysql-connector-java-5.1.10.zip file to a temporary folder.
- Open the folder and find the mysql-connector-java-5.1.10-bin.jar file.
- Copy that jar file into SmartFoxServer installation directory | jre | lib | ext.
- Go into the src directory of the extracted directory and copy the org directory to SmartFoxServer installation directory | jre | lib | ext.
Configuring the server settings
The configuration file of SmartFoxServer is an XML file that allows us to configure many server settings. It can configure the initial zone or room creation, server address, admin authorization, value tuning for performance, and a lot more. We are going to set the database connection for testing our setup in this article (core settings are out of scope of this article).
The configuration file is called config.xml and is located in the SmartFoxServer installation directory under the Server directory.
Configuring MySQL server connection in SmartFoxServer
- Open the config.xml in your favorite text editor.
- Go to line 203 of the config.xml. This line should be within the structure of a Zone tag with name as dbZone.
- Change the lines 203-218 from the config.xml:
Example connecting to MySQL
<TestSQL><![CDATA[SELECT COUNT(*) FROM contacts]]></TestSQL>
- Replace the code in lines 203-218 with the following code:
The new setting activates the DatabaseManager and configures the JDBC driver to the MySQL connector that we just downloaded.
We also changed the user name and password of the connection to the database to "root" and empty password.
We will use the empty password through out the development process but it is strongly recommended to set your own database user password.
There is a TestSQL setting where we can write a simple database query so that the SmartFoxServer will try to run it to test if the database connection is correct. As we have not created any new databases for the virtual world, we will test the database connection by querying the current server time.
Restarting the server
We’ve just set up the connection between SmartFoxServer and third-party database. It is time to test the new setting by restarting the SmartFoxServer.
To stop the SmartFoxServer in Windows and Linux, press Ctrl + C. To stop it in Mac OS X, click on the Cancel button in the SmartFoxServer log window.
There is a log that appears as usual after we start up the server again. It is important to check the log carefully every time the config.xml is changed. The logfile can provide details of any errors that occur when it tries to load the configure file.
For example, if we configure the database connection just now but forget to activate the DatabaseManager, the server will start up correctly. Then you may spend a lot of time debugging why the database connection is not working until you find that the DatabaseManager is not active at all. This happened to me several times while I was developing my first flash virtual world.
If the server is running with the new database connection settings, the following lines will be appearing in the log. There can be different database manager settings for each zone. When checking the log, we should be aware which zone the log is referring to. We are configuring the database manager of dbZone zone now.
DB Manager Activated ( org.gjt.mm.mysql.Driver )
If we forget to activate the DatabaseManager, we will not see the DB Manager Activated wording. Instead, the following message may appear in the log:
DB Manager is not active in this Zone!
Moreover, if the SmartFoxServer faces some fatal error on start up, it will terminate itself with more detailed error logs. The following lines are an example for error logs that appear when the MySQL connector file is missing:
Can’t load db driver: org.gjt.mm.mysql.Driver
[ Servre ] > DbManager could not retrive a connection. Java.sql.SQLException: Configuration file not found
DbManagerException: The Test SQL statement failed! Please check your configuration.
These lines state that the testing SQL failed to run, which we just set to test the connection. It also describes what exception has caused this error to help the debugging.
eBook Price: £18.99
Book Price: £30.99
(For more resources on Flash, see here.)
The easiest way to run the examples is to browse the examples through the embedded web server.
- Start the SmartFoxServer if it is not running.
- Browse to http://localhost:8080 in the web browser.
- It will load the default page of the embedded web server with SmartFoxServer logo.
- Click the Install Examples if this is the first time while running the examples.
- Click the Browse Examples after the installed examples.
- A list of examples appear and you click on them to have some ideas on what SmartFoxServer can basically do.
Loading a simple chat application
Let’s test the Simple Chat in Tutorials (AS 3.0) category.
After loading the simple chat, a progress bar appears with the text connecting to server. If the example failed to connect the server, it will freeze at that screen. If it is connected to the server, a login box appears and asks for the login name.
If you failed to see the login box, there is something wrong. It may be a wrong setting in the configuration file, it may be some other issue that failed to start up the SmartFoxServer, or it may be some issue between the testing browser and the server, for example, firewall port blocking.
As we are testing a multiuser application, we can actually launch many instances of the chat applications to test the real-time chatting.
- Open several new browsers.
- Type in the same address of the chat application in the new browsers. The default URL is http://localhost:8080/Examples/AS3/SmartFoxChat/bin/.
- Log in to the chat application with different names for different browsers.
- Test the application by sending messages in different browsers. The messages will all appear (almost) at the same time in all browsers.
Let’s take a look on how the messages are sent between different chat application clients.
- One of the users types in "Hi" in the chat application.
- The user clicks the Send button to send the "Hi" message.
- The message is sent from the chat application to SmartFoxServer.
- SmartFoxServer decodes the message and broadcasts this messages to all connected clients in the same room.
- All clients who receive the "Hi" message display it on the chat application.
To further understand what SmartFoxServer can do, we may not only test the simple chat example but also test other examples that are provided by SmartFoxServer.
Setting up the administration panel
SmartFoxServer Pro comes with an administration panel to provide a nice graphic interface to monitor and configure the running server. We will often use the administration panel during the whole development process.
The administration panel is located in SmartFoxServer installation directory | Admin | AdminTool.swf. Another way to access it in Windows is to launch from start menu by clicking Start | Programs | SmartFoxServerPro_1.6.6 | Admin | AdminTool.
Logging in to administration panel
The default login name is sfs_admin and the default password is sfs_pass. Leave the server IP address and port unchanged, and log in to the administration panel.
There will be a prompt dialog saying that using the default password is not secure. The password can be changed in the same configuration file that we used to set up the database connection.
After we are logged in, we have a glance at all server statistics and settings. It will show statistics including data traffic, memory usage, established connections count, and dropped messages count.
It allows many operations for a server moderator to use, for example, a moderator can monitor and manage zones and rooms, send broadcast messages to specific users, kick unusual or idle users, monitor logfiles, or even change the configuration and extension files on the fly when the server is running.
It is strongly recommended to change the password of the admin panel even in development stage. The default password raises a security issue that someone can log in to the admin panel and retrieve all the server configuration and server extension source code, including all database passwords stored inside.
Locating the Flash Client API
API is Application Programming Interface that allows other software to interact with it. Flash Client API for SmartFoxServer is the interface that allows Flash to connect to the SmartFoxServer and interact with it.
The following code snippet shows how Flash uses the client API to interact with SmartFoxServer.
Create an instance of client API object:
var client = new SmartFoxClient();
Connect the client to the SmartFoxServer with specific IP address and port number:
The connection between the Flash client and the SmartFoxServer is established now.
Next, we log in the user to the zone simpleChat with the username John.
Send public messages to all users in the same room.
The Flash Client API is located in SmartFoxServer installation directory | Flash API. There are four directories inside it. They are ActionScript 2.0, ActionScript 3.0, RedBoxClient_AS2, and RedBoxClient_AS3. RedBoxClient is the client API for connecting the media streaming Red5 component.
In this three-part article series we have discussed how to install the SmartFoxServer and set up a web server and database. We have also understood the whole idea of how a server requirement changed from development stage to deployment stage. Then we ran an example as well as administration panel and began to have some concept of what SmartFoxServer can basically do.
- Flash Multiplayer Virtual World with SmartFoxServer Using Embedded Web Server and Database [Article]
- Developing and Deploying Virtual World Environment for Flash Multiplayer [Article]
- Ordering the Buildings in Flash Virtual Worlds [Article]
eBook Price: £18.99
Book Price: £30.99
About the Author :
Makzan is the founder of 42games limited. He started designing games since he was a child. He likes to see how the well-designed interactions in his games can trigger the emotions and influent the players. He believes the games should let player shares joyful time with friends. Therefore his favorite game type is multiplayer casual games.
Makzan also wrote a book named Flash Multiplayer Virtual World. It is about developing a virtual world to play with friends in real time with Adobe Flash and socket server.