Ajax重载

时间:2011-11-03 13:45:31

标签: ajax jquery

我有一个使用jQuery构建shoutbox的网站。 我使用$ .ajax()请求来更新shoutbox。我每个人都会进 5秒钟找到任何新的呼喊。

几天后,我收到了来自我的网络托管公司的电子邮件 我的网站上的脚本负载很重。

有没有更好的方法来构建一个重负载更安全的shoutbox?

这是我目前的剧本:

$.ajax({
        type: "POST",
        url: "update-shouts.php",
        data: "",
        dataType: "json",
        success: function(data) {
            switch(data.code)
            {
            case 200:
                //NEW CONTENT, HANDLE IT
                var shouts = data.shouts;
                var html = "";

                for(var i = 0; i < shouts.length; i++){
                    var t = data.shouts[i].datetime.split(/[- :]/);
                    var d = new Date(t[0], t[1]-1, t[2], t[3], t[4], t[5]);
                    var hours = d.getHours();
                    var minutes = d.getMinutes();

                    var strDate = d.toLocaleDateString();

                    var now = new Date();
                    var strNow = now.toLocaleDateString();

                    if(hours < 10){
                        hours = "0"+hours;
                    }   

                    if(minutes < 10){
                        minutes = "0"+minutes;
                    }   

                    var str = hours + ":" + minutes;
                    if(strNow != strDate){
                        str = d.getDate() + "-" + d.getMonth() + " " + str;
                    }               

                    html += 
                        '<li style="clear: both;">' +
                            '<div style="float: left; width: 70px; overflow: hidden; white-space: nowrap;text-align: right;">' +
                                str +
                            '</div>' +
                            '<div style="float: left; width: 90px; overflow: hidden; font-weight: bold; color: #01A89C; text-align: right;">' +
                                shouts[i]["username"] +
                            '</div>' +
                            '<div style="float: right; width: 475px; text-align: left;">' +
                                shouts[i]["content"] + 
                            '</div>' +
                        '</li>' ;
                }

                $('ul#shouts li:first').before(html);
                break;
            case 201:
                //NO NEW CONTENT, NOTHING TO DO;
                break;
            default:
                //ERROR, NOTHING TO DO;
            }
        }
    });

update-shouts.php只有1.7KB,现在是:

<?php

require_once 'config.incl.php'; 
if(!is_ingelogd()){
    header("Location: login.php");
    exit;
}

$return_arr = array();

$code   = "400";
$name   = "wrong number of fields";
$shouts = "";

$qUser  = $db->query(
            "SELECT shout_id " .
            "FROM users " .
            "WHERE UPPER(username) = '" . mysql_real_escape_string(strtoupper($_SESSION['username'])) . "' " .
            "AND token = '" . mysql_real_escape_string($_SESSION['token']) . "' LIMIT 1"
        );

$shout_id = 1;
while($user = mysql_fetch_assoc($qUser)){
    $shout_id = $user["shout_id"];
    break;
}

$result = $db->query(
        "SELECT s.id, s.datetime, user_id, u.username, s.content " .
        "FROM shouts s JOIN users u ON(s.user_id = u.id) " .
        "WHERE s.id > " . $shout_id . " " .
        "ORDER BY s.id DESC");

$num = mysql_num_rows($result);

for($i = 0; $i < $num; ++$i) {
        $shouts[$i]["id"]       = mysql_result($result,$i,"s.id");
    $shouts[$i]["datetime"]     = mysql_result($result,$i,"s.datetime");
    $shouts[$i]["user_id"]      = mysql_result($result,$i,"user_id");
    $shouts[$i]["username"]     = mysql_result($result,$i,"u.username");
    $shouts[$i]["content"]      = stripslashes(mysql_result($result,$i,"s.content"));
}

$code = 201;
$name = "no new content";

if($num>0){
    $code = 200;
    $name = "OK";

    $db->query(
        "UPDATE users " .
        "SET shout_id = " . $shouts[0]["id"] . " " .
        "WHERE UPPER(username) = '" . mysql_real_escape_string(strtoupper($_SESSION['username'])) . "' " .
        "AND token = '" . mysql_real_escape_string($_SESSION['token']) . "'"
    );
}

$db->disconnect();

$return_arr["code"]     = $code;
$return_arr["name"]     = $name;
$return_arr["shouts"]   = $shouts;

echo json_encode($return_arr);
?>

我已经为每个用户使用了一个shout_id来获取该用户看不见的喊叫声。

谢谢!

0 个答案:

没有答案