draxq02664 2019-01-01 00:25
浏览 155

根据我的代码在服务器端实现长轮询

I already asked about this subject on how can I convert my basic polling script into a long polling structure and a user from that post said there is two parts to long polling. The client side structure and the server side

structure and he suggested I reask this question but this time referencing more on how I can implement long polling on the server side. Since he already provided the long polling structure example in the client side by JavaScript. So how can I implement long polling on the server side for the file called

check-for-new-records.php?

Here is the rest of my code to better understand what I mean.

index.php

<style>
#label{
    margin: 0;
    color: red;
    font-weight: bold;
  }
</style>

<script>

document.addEventListener('DOMContentLoaded',function(){

/**********************************************************************
Check for a new record amount in the data base
**********************************************************************/    

//Client side structure of long-polling

function checkForNewRecords() {

var xhr = new XMLHttpRequest();

xhr.onreadystatechange = function() {

    if (xhr.readyState == 4) {
    document.querySelector('#output').innerHTML = xhr.responseText;

    setTimeout(checkForNewRecords, 0);
  }
}

xhr.open('POST', 'check-for-new-records.php');
xhr.send();

}

checkForNewRecords()

});

</script>

<p id='label'>Total records in the database in real time in basic polling</p>

<div id='output'></div>

check-for-new-records.php

<?php

//Long polling in the server side code... How???

$db_servername= 'localhost';
$db_username='jd';
$db_password= '1234';
$db_name= 'test';

$db_connect= new mysqli($db_servername,$db_username,$db_password,$db_name);

$db_query= "SELECT COUNT(id) AS id FROM example";

$db_result= $db_connect->query($db_query);
$db_row= $db_result->fetch_object();

$total_records= $db_row->id;

?>

<style>
#total-records{
margin-top: 0;
}
</style>

<p id='total-records'><?php echo $total_records; ?></p>
  • 写回答

1条回答 默认 最新

  • dsgffz2579 2019-01-01 08:11
    关注

    Quick Hack .. something like this ?

    call with

    xhr.open('POST', 'check-for-new-records.php?lastcount=100');
    

    check-for-new-records.php

    <?php
    
    //Long polling in the server side code... How???
    
    $db_servername= 'localhost';
    $db_username='jd';
    $db_password= '1234';
    $db_name= 'test';
    
    $last = (int)$_GET['lastcount'];    // get last number of records
    
    $db_connect= new mysqli($db_servername,$db_username,$db_password,$db_name);
    
    $db_query= "SELECT COUNT(id) AS id FROM example";
    
    $i = 0;
    while ( $i < 6) {   // safety - 1 minute
        $db_result= $db_connect->query($db_query);
        $db_row= $db_result->fetch_object();
    
        $total_records= $db_row->id;
    
        if ( $total_records > $last) {
            break;
    
        }
    
        sleep( 10);
        $i++;
    
    }
    
    ?>
    
    <style>
    #total-records{
    margin-top: 0;
    }
    </style>
    
    <p id='total-records'><?php echo $total_records; ?></p>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器