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>
    
    评论

报告相同问题?

悬赏问题

  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 AT89C51控制8位八段数码管显示时钟。
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题