Menu:

Sponsor

Discover Master of Alchemy, our first iPad/iPhone and iPod touch game!

Follow Me

 

Forum's topics

Latest Files

Archives

Top Rated

Categories

Photo Gallery


Flash Media Server and AMFPHP

What we will see in this tutorial

I will show you a simple and practical example on how to combine Flash Communication Server with Flash Remoting, using AMFPHP. In contrast with other tutorials, we will make remoting connection and remoting calls from flash comm and not from the swf client file.

Requisites to run this tutorial:

The example shown in this tutorial will do two simple things:

  • A flash client will connect to a flash comm application
  • User should login in order to access the chat room (or register if not yet registered)

I will use screenshots to explain the tutorial steps, because i haven't enabled flash comm on this server.

Download files used in this tutorial

1. What is Flash Communication Server MX

Macromedia Flash Communication Server MX is a server based platform for creating and deploying compelling, interactive web-based audio/video applications such as video on demand, live event broadcasts, webcam chat, and real-time collaboration applications.
This means you can easily create multiplayer games, chat room and other multi user application using Flash.

 

2. Create the SQL table

In order to store the information about user login and password we will use a simple MySQL table.
Open your preferred mysql client administration tool (for example phpMyAdmin), select a DB and run this script:

  --
  -- Table structure for table `userlogin`
  --
  CREATE TABLE `userlogin` (
  `userid` int(10) unsigned NOT NULL auto_increment,
  `username` char(30) NOT NULL default '',
  `userpassword` char(32) NOT NULL default '',
  PRIMARY KEY (`userid`),
  UNIQUE KEY `userid` (`userid`),
  UNIQUE KEY `username_1` (`username`),
  KEY `userid_2` (`userid`)
  ) TYPE=MyISAM;

Now that you've created MySQL table it's time to create the AMFPHP class which will operate on this table.

 

3. The AMFPHP class

If you haven't already installed amfphp it's time to do that. Go to amfphp home page and download the package. If need help for installation you can refer to this tutorial which explain amfphp basics.

Now, create a new php class file "flashcommtest.php" and save it into your amfphp services folder. This is the script:


<?php

class flashcommtest
{
    
/** mysql var access */
    
var $db_host 'localhost';
    var 
$db_name 'test';
    var 
$db_user 'root';
    var 
$db_pwd  '';
    
    
/** class constructor */
    
function flashcommtest(){
        
// create the connection to DB
        
$this->connection  = @mysql_connect($this->db_host$this->db_user$this->db_pwd);
        
// select the database "test"
        
@mysql_select_db$this->db_name );
        
// define the method table for defining flash remoting available methods 
        
$this->methodTable = array();
        
$this->methodTable["isValidUser"] = array(
                                    
"description" => "validate user in mysql table",
                                    
"access" => "remote",
                                    );
        
$this->methodTable["addUser"] = array(
                                    
"description" => "Add user in mysql user table",
                                    
"access" => "remote",
                                    );
    }
    
    
/** check if username exists into the table */
    
function isValidUser($username$password){
        if(
$this->connection){
            
$query sprintf("SELECT * FROM userlogin WHERE username = '%s' and userpassword = '%s'"$username$password);
            
$result mysql_query($query);
            if(
mysql_num_rows($result) > 0){
                return 
true;
            } else {
                return 
false;
            }
        } else {
             return 
false;
         }
    }
    
    
/** register a new user */
    
function addUser($username$password){
        if(
$this->connection){
            
$query sprintf("INSERT INTO userlogin set username = '%s', userpassword = '%s'"$username$password);
            
$result = @mysql_query($query);
            return 
$result;
            if(
$result){
                return 
true;
            } else {
                return 
false;
            }
        } else {
            return 
false;
        }
    }
}
?>

This class script contains 2 simple methods used by amfphp.
isValidUser, which checks if username/password corresponds to a valid user in the table.
addUser, which will register a new username or returns an error in case of username duplication.
Both the methods return a boolean value.
I will not explain in detail the amfphp class structure because if you are familiar with amfphp you will indeed understand this simple structure, otherwise i suggest you first to read the amfphp installation tutorial and then come back here.
Usually you would use this class using a flash client which connects to the amfphp with the mx.remoting classes. This time however, we will connect directly from the flash comm application, which is a little different, but not too much.