实施php长轮询

时间:2017-11-23 10:30:22

标签: php long-polling

我想在我的php脚本中使用php-long轮询,以便在管理员从后端更改任何内容时立即显示数据。

我浏览了解决方案,但找不到任何解决方案。

我从github下载了php-long-polling- master。在那里,我有一个文件server.php。

代码就像这样

<?php

/**
 * Server-side file.
 * This file is an infinitive loop. Seriously.
 * It gets the file data.txt's last-changed timestamp, checks if this is larger than the timestamp of the
 * AJAX-submitted timestamp (time of last ajax request), and if so, it sends back a JSON with the data from
 * data.txt (and a timestamp). If not, it waits for one seconds and then start the next while step.
 *
 * Note: This returns a JSON, containing the content of data.txt and the timestamp of the last data.txt change.
 * This timestamp is used by the client's JavaScript for the next request, so THIS server-side script here only
 * serves new content after the last file change. Sounds weird, but try it out, you'll get into it really fast!
 */

// set php runtime to unlimited
set_time_limit(0);

// where does the data come from ? In real world this would be a SQL query or something
$data_source_file = 'data.txt';

// main loop
while (true) {

    // if ajax request has send a timestamp, then $last_ajax_call = timestamp, else $last_ajax_call = null
    $last_ajax_call = isset($_GET['timestamp']) ? (int)$_GET['timestamp'] : null;

    // PHP caches file data, like requesting the size of a file, by default. clearstatcache() clears that cache
    clearstatcache();
    // get timestamp of when file has been changed the last time
    $last_change_in_data_file = filemtime($data_source_file);

    // if no timestamp delivered via ajax or data.txt has been changed SINCE last ajax timestamp
    if ($last_ajax_call == null || $last_change_in_data_file > $last_ajax_call) {

        // get content of data.txt
        $data = file_get_contents($data_source_file);

        // put data.txt's content and timestamp of last data.txt change into array
        $result = array(
            'data_from_file' => $data,
            'timestamp' => $last_change_in_data_file
        );

        // encode to JSON, render the result (for AJAX)
        $json = json_encode($result);
        echo $json;

        // leave this loop step
        break;

    } else {
        // wait for 1 sec (not very sexy as this blocks the PHP/Apache process, but that's how it goes)
        sleep( 1 );
        continue;
    }
}

如果我们在data.txt中更改了任何内容,它会实时显示。 但我没有得到如何以及在哪里可以添加查询或如何将其包含在我的文件中。

在server.php中,就像

一样
// where does the data come from ? In real world this would be a SQL query or something
    $data_source_file = 'data.txt';

但是我在哪里以及如何添加sql查询?

有人可以帮助我如何开始或如何添加SQL查询

****我的代码*****

经过多次尝试,我可以这样做

在server.php中,我确实喜欢这个

 <?php

/**
 * Server-side file.
 * This file is an infinitive loop. Seriously.
 * It gets the file data.txt's last-changed timestamp, checks if this is larger than the timestamp of the
 * AJAX-submitted timestamp (time of last ajax request), and if so, it sends back a JSON with the data from
 * data.txt (and a timestamp). If not, it waits for one seconds and then start the next while step.
 *
 * Note: This returns a JSON, containing the content of data.txt and the timestamp of the last data.txt change.
 * This timestamp is used by the client's JavaScript for the next request, so THIS server-side script here only
 * serves new content after the last file change. Sounds weird, but try it out, you'll get into it really fast!
 */

// set php runtime to unlimited
set_time_limit(0);

// where does the data come from ? In real world this would be a SQL query or something
$data_source_file = 'data.txt';

$servername = "localhost";
$username = "*****";
$password = "******";
$dbname = "mydb";

// Create connection
        $conn = mysqli_connect($servername, $username, $password , $dbname);
        // Check connection
        if (!$conn) {
            die("Connection failed: " . mysqli_connect_error());
        }


// main loop
while (true) {

    // if ajax request has send a timestamp, then $last_ajax_call = timestamp, else $last_ajax_call = null
    $last_ajax_call = isset($_GET['timestamp']) ? (int)$_GET['timestamp'] : null;

    // PHP caches file data, like requesting the size of a file, by default. clearstatcache() clears that cache
   // clearstatcache();
    // get timestamp of when file has been changed the last time
   // $last_change_in_data_file = filemtime($data_source_file);

         // Create database
        $sql = "SELECT max(tax_id) FROM taxes";
        $result = mysqli_query($conn, $sql);

        $last_change_in_data_file = mysqli_fetch_array($result, MYSQLI_NUM)[0]; 
    // if no timestamp delivered via ajax or data.txt has been changed SINCE last ajax timestamp
    if ($last_ajax_call == null || $last_change_in_data_file > $last_ajax_call) {

        // get content of data.txt
//        $data = file_get_contents($data_source_file);


        // Create database
        $sql = "SELECT tax_id, name FROM taxes";
        $result = mysqli_query($conn, $sql);

        if (mysqli_num_rows($result) > 0) {
            // output data of each row
            while($row = mysqli_fetch_assoc($result)) {
                $data .=  "id: " . $row["tax_id"]. " - Name: " . $row["name"]. "<br>";
                $last_change_in_data_file = $row["id"];
            }
        }
       // mysqli_close($conn);

        // put data.txt's content and timestamp of last data.txt change into array
        $result = array(
            'data_from_file' => $data,
            'timestamp' => $last_change_in_data_file
        );
        mysqli_close($conn);
        // encode to JSON, render the result (for AJAX)
        $json = json_encode($result);
        echo $json;

        // leave this loop step
        break;

    } else {
        // wait for 1 sec (not very sexy as this blocks the PHP/Apache process, but that's how it goes)
        sleep( 1 );
        continue;
    }
} 

但我想根据特定的ID获取数据。我的sql语句必须是这样的

$sql = "SELECT tax_id, name FROM taxes WHERE grp_id=".$id.""; 

所以我没有从我应该将id传递到此页面的地方

有人可以帮助我吗?

我的情景:

我正在尝试实时显示数据。我的税表包含一列is_ok。当管理员为特定组按一个OK按钮时,tax列将更新为该组的is_okay = 1。该组的所有在线用户都会收到管理员已按下按钮并等待您批准的通知。然后用户将批准.....

所以为此我试图将管理员页面中的grp_id传递给税表。但是在点击“OK”之后按钮管理员应该获得所有在同一页面中完成的用户的立即批准通知。所以我面临着问题

0 个答案:

没有答案
相关问题