PHP MYSQL JQuery Long Polling - 没有按预期工作

时间:2015-04-18 16:12:13

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

我的长轮询实施无效。一直很难理解在哪里调试代码。

要点

  • 没有错误
  • 长轮询随机工作(仅响应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 );
                        }

                    }
                }
            );
        }

2 个答案:

答案 0 :(得分:1)

感谢所有帮助人员!我询问了很多人,并找到了一些很好的地方来调试代码。

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

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

答案 1 :(得分:0)

你可以做的事情是:

  

1。)打开Chrome开发者工具,然后单击“网络”选项卡并清除所有内容。然后点击提交。查看网络选项卡,查看正在发布的内容以及未发布的内容。然后从那里进行相应调整。

     

2。)回显你的php脚本中的不同步骤,并使用Network选项卡执行相同的操作,然后单击" results"区域,看看有什么回应,以及它是否符合预期。

从那里,你应该能够调试发生了什么,并找出它出错的地方。