聊天页面使用jQuery的$ .ajax和PHP没有及时更新

时间:2016-06-02 22:04:47

标签: php jquery ajax performance server-response

我正在为我的某个网站的成员构建一个聊天页面。一切都适合我,除了聊天的自动更新。聊天页面应该每5秒检查一次新消息,并且操纵回调显示该部分正常工作;但是,$ .ajax引用的PHP文件给了我两个截然不同的行为。因为ajax在我执行的每个测试中都能正常工作,所以我会在这里排除它,但如果有人认为有必要,我可以添加它。这是我带注释的PHP:

$new_chat = //new chat message, collected from $.ajax and sanitized
$new_sender = //username of sender, collected from $.ajax and sanitized
$new_time = date('m-d-y');
$most_recent_chat = //unique id# of most recent chat message, collected from $.ajax and sanitized

//This block makes sure that there is a most recent chat; if not, (i.e., the
//page was just loaded so there are no chats on the page) it manually sets one.
//I'm not having any problems with this block.

if (!isset($most_recent_chat)) {
    $query = "SELECT MAX(id) AS 'Most Recent' FROM `chat_global`";
    $check_last_chat = new mysqli($host,$user,$pass,$game_db);

    $check_last_chat->query($query);

    if(!$result = $check_last_chat->query($query)) {
        die();
    }

    while ($row=$result->fetch_assoc()) {
        $most_recent = $row['Most Recent'];
    }
    $most_recent_chat = $most_recent-100;
    $result->free();
    $check_last_chat->close();
}

//Send any new chats to DB

if(isset($new_chat)) {
        //First query inserts new chats into the chat table
        $query = "INSERT INTO `chat_global` (message,sender,time) VALUES ('".$new_chat."','".$new_sender."','".$new_time."');";

        $add_new_chat = new mysqli($host,$user,$pass,$game_db);

        $add_new_chat->query($query);
        $add_new_chat->close();

        //Second query returns all new chats in reference
        //to the most recent chat on the user's browser page

        $query2 = "SELECT * FROM `chat_global` WHERE id>'$most_recent_chat';";

        $most_recent_chats = new mysqli($host,$user,$pass,$game_db);

        if(!$result = $most_recent_chats->query($query2)) {
            die();
        }

        while($row = $result->fetch_assoc()) {
            echo '<div class="chat-item" data-chat-id="' . $row['id'] . '">';
            echo '<p class="chat-message"><strong>' . $row['sender'] . ': </strong>' . $row['message'] . '</p>';
            echo '<p class="chat-time">' . $row['time'] . '</p></div>';
        }

        $result->free();
        $most_recent_chats->close();
} else {
    //Query 2 from above is repeated; basically, skips adding new message
    //and simply retrieves any other new messages
    $query2 = "SELECT * FROM `chat_global` WHERE id>'$most_recent_chat';";

    $most_recent_chats = new mysqli($host,$user,$pass,$game_db);

    if(!$result = $most_recent_chats->query($query2)) {
        die();
    }

    while($row = $result->fetch_assoc()) {
        echo '<div class="chat-item" data-chat-id="' . $row['id'] . '">';
        echo '<p class="chat-message"><strong>' . $row['sender'] . ': </strong>' . $row['message'] . '</p>';
        echo '<p class="chat-time">' . $row['time'] . '</p></div>';
    }

    $result->free();
    $most_recent_chats->close();
}

if(isset($ new_chat))块很简单,并没有给我带来任何问题。基本上,如果有新消息,它会将新消息添加到聊天数据库,然后从浏览器的角度返回ID号高于最新消息的所有消息。此部分在1/4秒内工作并将其信息返回给浏览器。

此代码中的else块是我似乎遇到一些问题的地方。 else块是相同的,行的行,if块的后半部分(从$ query2下来)。它有效,但速度很慢; if块在平均1/4秒内从服务器加载,而else块(代码较少)平均需要90秒才能返回数据。无论用户是否发送了消息,$ .ajax调用都是相同的功能;唯一的区别是,如果用户发送了一条消息(并且$ .ajax调用因此是手动的),则引用if块,但是else块会被浏览器中重复的setTimeout(function,5000)行自动引用。 / p>

我99.9%确定setTimeout()工作正常,因为如果我手动设置我的$ .ajax调用来发送泛型$ new_chat(即“这是一条消息。”),该函数每次都有效;每5秒发送一次聊天,1/4秒后,我看到它会按照预期出现在我的聊天页面中。 (我的聊天页面完全由上面的PHP填充;即使从用户A发送的消息也必须发送到上述文件,处理并发送回用户A,这样用户就可以知道他们的消息已被发送。)

最重要的是,我完全不知道为什么会出现这种情况。无论是自动还是手动,$ .ajax都具有相同的功能; PHP是相同的代码,较慢的块也更短启动! $ .ajax调用在手动时非常快速地运行,并且如果消息与自动$ .ajax一起发送,它也可以非常快速地运行。我是AJAX和jQuery的新手,所以我觉得问题在于其中一种技术,但是我所处的位置根本没有任何意义。

0 个答案:

没有答案
相关问题