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


Basic Flash MX/MySQL chat

Here is the last part of our AS code for the second frame:


//    ---------------------
//    get date
//    ---------------------
function get_date()
{
    var d = new Date();
    return d.getTime();
}
//    ---------------------
//    add the check key
//    ENTER function
//
//    in this way also when
//    press the 'enter' key
//    message is sent
//    ---------------------
k = new Object();
k.onKeyDown = function()
{
    if (Key.getCode() == Key.ENTER && myMSG.text != undefined && myMSG.text != "")
    {
        function_send(myMSG.text);
    }
};
Key.addListener(k);
//    ----------------------
//    load new messages
//    with setInterval
//    ----------------------
function read_from_php()
{
    if (!_root.isLoaded)
    {
        return;
    }
    var ln = new LoadVars();
    ln.uniqid = uniqid;
    ln.load(read_mysql + "?" + ln.toString());
    ln.onLoad = function()
    {
        //    ---------------------
        //    retreive new messages
        //    ---------------------
        var i = Number(this.__total);
        while (this['__date' + i] != undefined)
        {
                history.htmlText += "<P ALIGN='LEFT'>[" 
                + formatDate(this['__date' + i]) + "] - <B>" 
                + this['__user' + i] + "</B>: " + (this['__message' + i]) + "</P>";
                history.scroll = history.maxscroll;
            i--;
        }
        //    -------------------
        //    who's online
        //    -------------------
        var i = 1;
        whosonline.removeAll();
        whosonline.addItem("-- who's online ? --");
        while (this['__online' + i] != undefined)
        {
            whosonline.addItem(this['__online' + i]);
            i++;
        }
    };
}
qload = setInterval(read_from_php, interval);
load_first();

The get_date() function simply is needed when we pass the date in UNIX time stamp to PHP.
Then, the listener k will listen for user key input. This is needed for instance when the user writes her/his message and press ENTER key instead of click the SEND button.
So, after we've created the new object k, we have to add this function: k.onKeyDown = function()
that checks the value of myMSG text.
The last step before our listener will work is to assign it to a Flash Object. In this case, we need to add it to the Key object: Key.addListener(k);



The last function in our AS code is the read_from_php() function which is very similar to the load_first() function that we analyzed above. The difference is; that does not pass the variable first_run to PHP and so PHP recognizes that this is not a new client and he has to return to him *only* new messages, if they exist. Then, rebuild the whosonline list box in according with variables passed by PHP.

Last thing:
qload = setInterval(read_from_php, interval);
load_first();


These two lines do the job in the sense that; at first we create a new setInterval() object that is based on the value of interval variable which we defined in the first Flash frame.
The last line invokes the first function that we've analyzed: load_first(), which will ask to PHP for the chat history.
As you can see the function load_first() is called once, instead of read_from_php() which is called with an interval of 3.5 secs forever.


Note: I used an interval of 3500 ms (3.5 seconds) and I think it is already too much little time... It may cause a bandwidth lost if you have too many users and it depends also from your server band. Don't use an interval less than 1 seconds.
It would be better also to add a mysql that would delete old messages and users to allow mysql works faster in those tables

I don't inserted a specific query for table maintenance, but you can insert these few lines at the end of the file read_mysql.php

// ----------------------
// optimize table flash_chat
// we delete old records
// ----------------------
$query = mysql_query("SELECT chat_data FROM $table ORDER BY chat_data DESC LIMIT $first_run, 1") or die();
if(@$to_delete = mysql_fetch_assoc($query)){
    $del = $to_delete['chat_data'];
    $del_query = mysql_query("DELETE FROM $table WHERE chat_data < $del") or die();
}
// ----------------------
// delete old users
// ----------------------
$query = mysql_query("SELECT user_date FROM $user WHERE user_date < ($time_start - ($active*4)) ORDER BY user_date DESC LIMIT 1") or die();
if(@$to_delete = mysql_fetch_assoc($query)){
    $del = $to_delete['user_date'];
    $del_query = mysql_query("DELETE FROM $user WHERE user_date < $del");
}

It checks into table flash_chat if there are more than 50 messages and if they are, it removes them. In the table flash_chat_useronline it check which are the old users who have as last updated data a value that is 4 times older than the actual time and remove them.
I can suggest you to insert also these lines to your php file in order to take under control the size of mysql tables.

 

5. Files preview & download

download .zip file with files used in this tutorial