在基本长轮询示例上实现MySQL

时间:2017-06-04 01:54:22

标签: php mysql

我试图创建一个测试应用程序,在将新数据插入数据库后检索消息,它是一个基本的长期轮询",我跟着并使用了这个文件example,但是这个脚本只从名为" data.txt"的文件中获取数据,我怎样才能使用相同的逻辑并在插入新数据后立即从数据库中获取数据?

这里是php代码:

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;
    }
}

jquery代码:client.js

function getContent(timestamp)
{
    var queryString = {'timestamp' : timestamp};
    $.ajax(
        {
            type: 'GET',
            url: 'http://127.0.0.1/long_polling/server/server.php',
            data: queryString,
            success: function(data){
                // put result data into "obj"
                var obj = jQuery.parseJSON(data);
                // put the data_from_file into #response
                $('#response').html(obj.data_from_file);
                // call the function again, this time with the timestamp we just got from server.php
                getContent(obj.timestamp);
            }
        }
    );
}

// initialize jQuery
$(function() {
    getContent();
});

1 个答案:

答案 0 :(得分:0)

PHP:
<?php

set_time_limit(0);
$result = array();

while (true) {
    clearstatcache();

    // if you using database, don't forget fill the fields
    $pdo = new PDO("mysql:host=localhost;dbname=;", "", "");
    $query = $pdo->prepare("SELECT * FROM users");
    $query->execute();

    /**
     * Not necessary to use output from database.
     * If you want use data in text file, you can use this solution:
     *
     * $data = file_get_contents('data.php');
     * $result['data'] = $data;
     * echo $result['data'];
     *
     */
    
    // fields from database
    while ($fetch = $query->fetch()) {
        $result['id'] = $fetch['id'];
        $result['name'] = $fetch['name'];
        $result['message'] = $fetch['message'];
        echo "id: " . $result['id'] . "<br>";
        echo "name: " . $result['name'] . "<br>";
        echo "message: " . $result['message'] . "<br>";
    }

    break;
}