0

我正在为我的一个网站的成员建立一个聊天页面。除了聊天的自动更新外,一切对我来说都正常。聊天页面应该每 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 调用都是相同的函数;唯一的区别是如果用户发送了一条消息,则引用 if 块(因此 $.ajax 调用是手动的),但 else 块由浏览器中重复的 setTimeout(function,5000) 行自动引用。

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

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

4

0 回答 0