Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/79.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 这是一次真正的长时间投票吗?_Php_Jquery_Mysql_Long Polling - Fatal编程技术网

Php 这是一次真正的长时间投票吗?

Php 这是一次真正的长时间投票吗?,php,jquery,mysql,long-polling,Php,Jquery,Mysql,Long Polling,经过多次试验,我成功地保持了与数据库的连续服务器连接。 现在,如果数据库中有新的消息,请编码keet cheking并显示消息 Plz回顾和讲述 如果此代码中使用了真正的长轮询技术?如果没有,请建议我错在哪里(偏离长轮询)以及如何实现真正的长轮询 目前,我收到了这些错误。但是它仍然保持着与数据库的连续连接 每次只提取一条消息而不是全部消息。(我使用了每个循环,但它会停止长轮询) 每隔10/15秒,就会出现令牌错误(解析错误(语法错误=意外令牌)) Php文件在这里 $last_msg_id=

经过多次试验,我成功地保持了与数据库的连续服务器连接。 现在,如果数据库中有新的消息,请编码keet cheking并显示消息

Plz回顾和讲述 如果此代码中使用了真正的长轮询技术?如果没有,请建议我错在哪里(偏离长轮询)以及如何实现真正的长轮询

目前,我收到了这些错误。但是它仍然保持着与数据库的连续连接

  • 每次只提取一条消息而不是全部消息。(我使用了每个循环,但它会停止长轮询)
  • 每隔10/15秒,就会出现令牌错误(解析错误(语法错误=意外令牌)

  • Php文件在这里

     $last_msg_id=$_POST['last_msg_id'];
    $last_msg_id_db=1;
    
    while($last_msg_id>$last_msg_id_db){
        usleep(10000);
        clearstatcache();
    
        $sql=mysqli_query($db3->connection,"SELECT * FROM chat_com where id>'$last_msg_id' ORDER by id ASC");
    
        $sql_m=mysqli_query($db3->connection,"SELECT max(id) as maxid  FROM chat_com");
        $row_m=mysqli_fetch_array($sql_m);
        $last_msg_id_db=$row_m['maxid'];
    
        while($row=mysqli_fetch_array($sql)){
            $textt=$row['mesg'];
    
            $last_msg_id_db=$last_msg_id_db;
            $response=array();
            $response['msg']=$textt;
            $response['last_msg_id_db']=$last_msg_id_db;
        }
    }
    
    echo json_encode($response);
    

    轮询要比简单的一段时间难一点:因为一般来说,您输出到浏览器的所有内容在完成时都会被解释。你的例子很清楚:

    success:function(data) {
        var json = data;
        $("#commidwin").append(json['msg']);
        last_msg_id = json["last_msg_id_db"];
        setTimeout("load_msgs()", 1000);
    },
    
    jQuery将等待响应完成,以构建
    数据
    变量,然后调用成功回调

    创建长轮询的一种方法是使用任务和跟随者:

    • 任务是“无限”循环,它只显示捕获和触发事件,放在“框”中

    • follower是每X秒进行一次ajax调用,它查看任务填充的“框”,并立即在页面内执行操作

    下面是一个长轮询示例,没有跟随者,只有一个停止轮询的事件(发布),但您会明白:

    <?php
    
    // For this demo
    if (file_exists('poll.txt') == false)
    {
        file_put_contents('poll.txt', '');
    }
    
    // If this variable is set, a long-polling is starting...    
    if (isset($_GET['poll']))
    {
    
        // Don't forget to change the default time limit
        set_time_limit(120);
    
        date_default_timezone_set('Europe/Paris');
        $time = time();
    
        // We loop until you click on the "release" button...
        $poll = true;
        $number_of_tries = 1;
        while ($poll)
        {
            // Here we simulate a request (last mtime of file could be a creation/update_date field on a base)
            clearstatcache();
            $mtime = filemtime('poll.txt');
    
            if ($mtime > $time)
            {
                $result = htmlentities(file_get_contents('poll.txt'));
                $poll = false;
            }
    
            // Of course, else your polling will kill your resources!
            $number_of_tries++;
            sleep(1);
        }
    
        // Outputs result
        echo "Number of tries : {$number_of_tries}<br/>{$result}";
        die();
    }
    
    // Here we catch the release form
    if (isset($_GET['release']))
    {
        $data = '';
        if (isset($_GET['data']))
        {
            $data = $_GET['data'];
        }
        file_put_contents('poll.txt', $data);
        die();
    }
    ?>
    
    <!-- click this button to begin long-polling -->
    <input id="poll" type="button" value="Click me to start polling" />
    
    <br/><br/>
    
    Give me some text here :
    <br/>
    <input id="data" type="text" />
    <br/>
    
    <!-- click this button to release long-polling -->
    <input id="release" type="button" value="Click me to release polling" disabled="disabled" />
    
    <br/><br/>
    
    Result after releasing polling :
    <div id="result"></div>
    
    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>
    <script type="text/javascript">
    
        // Script to launch polling
        $('#poll').click(function() {
            $('#poll').attr('disabled', 'disabled');
            $('#release').removeAttr('disabled');
            $.ajax({
                url: 'poll.php',
                data: {
                    poll: 'yes' // sets our $_GET['poll']
                },
                success: function(data) {
                    $('#result').html(data);
                    $('#poll').removeAttr('disabled');
                    $('#release').attr('disabled', 'disabled');
                }
            });
        });
    
        // Script to release polling
        $('#release').click(function() {
            $.ajax({
                url: 'poll.php',
                data: {
                    release: 'yes', // sets our $_GET['release']
                    data: $('#data').val() // sets our $_GET['data']
                }
            });
        });
    
    </script>
    

    如果您将error@jgauffin我已经更新了error name.plz review。这是一个真正的长轮询和可扩展(最多10000个用户?)
    
    <?php
    
    // For this demo
    if (file_exists('poll.txt') == false)
    {
        file_put_contents('poll.txt', '');
    }
    
    // If this variable is set, a long-polling is starting...    
    if (isset($_GET['poll']))
    {
    
        // Don't forget to change the default time limit
        set_time_limit(120);
    
        date_default_timezone_set('Europe/Paris');
        $time = time();
    
        // We loop until you click on the "release" button...
        $poll = true;
        $number_of_tries = 1;
        while ($poll)
        {
            // Here we simulate a request (last mtime of file could be a creation/update_date field on a base)
            clearstatcache();
            $mtime = filemtime('poll.txt');
    
            if ($mtime > $time)
            {
                $result = htmlentities(file_get_contents('poll.txt'));
                $poll = false;
            }
    
            // Of course, else your polling will kill your resources!
            $number_of_tries++;
            sleep(1);
        }
    
        // Outputs result
        echo "Number of tries : {$number_of_tries}<br/>{$result}";
        die();
    }
    
    // Here we catch the release form
    if (isset($_GET['release']))
    {
        $data = '';
        if (isset($_GET['data']))
        {
            $data = $_GET['data'];
        }
        file_put_contents('poll.txt', $data);
        die();
    }
    ?>
    
    <!-- click this button to begin long-polling -->
    <input id="poll" type="button" value="Click me to start polling" />
    
    <br/><br/>
    
    Give me some text here :
    <br/>
    <input id="data" type="text" />
    <br/>
    
    <!-- click this button to release long-polling -->
    <input id="release" type="button" value="Click me to release polling" disabled="disabled" />
    
    <br/><br/>
    
    Result after releasing polling :
    <div id="result"></div>
    
    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>
    <script type="text/javascript">
    
        // Script to launch polling
        $('#poll').click(function() {
            $('#poll').attr('disabled', 'disabled');
            $('#release').removeAttr('disabled');
            $.ajax({
                url: 'poll.php',
                data: {
                    poll: 'yes' // sets our $_GET['poll']
                },
                success: function(data) {
                    $('#result').html(data);
                    $('#poll').removeAttr('disabled');
                    $('#release').attr('disabled', 'disabled');
                }
            });
        });
    
        // Script to release polling
        $('#release').click(function() {
            $.ajax({
                url: 'poll.php',
                data: {
                    release: 'yes', // sets our $_GET['release']
                    data: $('#data').val() // sets our $_GET['data']
                }
            });
        });
    
    </script>