【问题标题】:PHP MYSQL JQuery Long Polling - Not working as expectedPHP MYSQL JQuery 长轮询 - 未按预期工作
【发布时间】:2015-06-25 12:22:55
【问题描述】:

我的长轮询实现不起作用。很难理解在哪里调试所述代码。

关键点

  • 没有错误
  • 长轮询随机工作(仅响应 MySQL 中的某些更改,没有明显的模式)
  • MySQL 正在正确更新
  • 我正在通过 Localhost WAMP 和具有两个不同会话的两个浏览器对此进行测试

PHP 部分 -

$path= $_SERVER[ 'DOCUMENT_ROOT']; 
$path .= "/config.php" ; 
require_once($path);

require_once(PHP_PATH . "/classes/user.php");

session_start(); 

require_once(PHP_PATH . "/functions/database.php");

// Return to Login if no Session
if(!isset($_SESSION['user'])){
    header("Location: /login");
    die();
}

$db = connectdatabase();

$timeout = 40;    

// if no post ids kill the script // Should never get here
if(!isset($_POST['post_ids'])){
    die();
}

if(!isset($_POST['timestamp'])){
    die();
}

$last_ajax_call = $_POST['timestamp'];
$post_ids = trim(strip_tags($_POST['post_ids']));
$id = $_SESSION['user']->getID();

// Check if there are posts from the last search that need to be updated with a comments or the like number has to be updated
$query = "SELECT posts.*, users.first_name, users.last_name, users.picture
        FROM posts
        LEFT JOIN users
        ON users.id = posts.user_id
        WHERE ((UNIX_TIMESTAMP(posts.date) > :last_ajax_call OR UNIX_TIMESTAMP(posts.last_modified) > :last_ajax_call) 
        AND posts.parent IN (:post_ids)) OR (posts.id IN (:post_ids) AND UNIX_TIMESTAMP(posts.last_modified) > :last_ajax_call)";

while ($timeout > 0) {
    $check_for_updates = $db->prepare($query);
    $check_for_updates->bindParam(':post_ids', $post_ids);
    $check_for_updates->bindParam(':last_ajax_call', $last_ajax_call);
    $check_for_updates->execute();
    $r = $check_for_updates->fetchAll();

    if(!empty($r)){
        // Get current date time in mysql format
        $unix_timestamp = time();

        // Cofigure result array to pass back
        $result = array(
            'timestamp' => $unix_timestamp,
            'updates' => $r
        );

        $json = json_encode($result);
        echo $json;
        return;
    } else {
        $timeout --;
        usleep( 250000 );
        clearstatcache();
    }
}
// you only get here if no data found
$unix_timestamp = time();

// Cofigure result array to pass back
$result = array(
    'timestamp' => $unix_timestamp
);

$json = json_encode($result);
echo $json;

JQuery Ajax -

function getUpdates(timestamp) {
            var post_ids = $("#newsfeed").find("#post_ids").attr('data-post-ids');
            var data = {'timestamp' : timestamp,
                        'post_ids' : post_ids};

            poll = $.ajax({
                    type: 'POST',
                    url: '/php/check_for_updates.php',
                    data: data,
                    async: true, /* If set to non-async, browser shows page as "Loading.."*/
                    cache: false,
                    success: function(data) {
                        try {
                            // put result data into "obj"
                            var obj = jQuery.parseJSON(data);
                            // put the data_from_file into #response
                            //$('#response').html(obj.data_from_file);
                            // repeat
                            console.log("SQL: " + obj['timestamp']);
                            setTimeout( function() {
                                // call the function again, this time with the timestamp we just got from server.php
                                getUpdates(obj['timestamp']);
                            }, 1000 );

                        } catch( e ) {
                            // repeat
                            // Get mysql formated date
                            var unix_timestamp = Math.floor(Date.now() / 1000);

                            console.log("JS:  " + unix_timestamp);
                            setTimeout( function() {
                                getUpdates(unix_timestamp);
                            }, 1000 );
                        }

                    }
                }
            );
        }

【问题讨论】:

    标签: php jquery mysql real-time long-polling


    【解决方案1】:

    感谢大家的帮助!我问了很多人,找到了很多可以调试代码的好地方。

    我终于在这里找到了答案——

    看起来我检查更新的 PHP 阻止了任何更新的发生,直到 PHP 停止检查更新。

    【讨论】:

      【解决方案2】:

      你可以做的几件事是:

      1.) 打开 Chrome 开发人员工具,然后单击网络选项卡并清除所有内容。然后点击提交。查看网络选项卡,查看发布的内容和未发布的内容。然后从那里进行相应的调整。

      2.) 在您的 php 脚本中回显不同的步骤,并使用“网络”选项卡执行相同的操作,然后单击“结果”区域,查看回显的内容以及是否符合预期。

      从那里,您应该能够调试正在发生的事情并找出问题所在。

      【讨论】:

      • 谢谢。你的权利。但我似乎找不到问题所在。过去几天我一直在尝试 Chrome Dev + Echo。试图找出错误值没有通过的地方。问题似乎是 MySQL 查询获取旧数据。因为我将通过 MySQL 更新数据,检查它是否已更新,但是我的 PHP 查询返回没有更新。然后我尝试检查 PhpMyAdmin 中的查询,它返回了正确的值。
      • 您已经回显了 $query,然后将其放入 phpmyadmin 并得到了预期的结果?
      • 是的。但同样的查询,在我的测试环境中并没有实时正确地执行。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-23
      • 2012-11-13
      • 2013-06-19
      相关资源
      最近更新 更多