dongying6659 2013-09-05 15:19
浏览 138

关于Ajax php长轮询的问题任何真正的解决方案?

I´m trying to learn how long polling work. I have read many tutorials out there and found most of them use jQuery. For me using jQuery is nice because it make thing easier. But to talk about learning javascript basic. Using jQuery is not the first choice for me to think about.

I have made my own lomg polling and have some question about it.

in javascript ( I will not have whole script because I guess people understand it very basic)

function callme() {

var tmstmp= new Date().getTime(); // sending time stamp now to php
var url="test2.php?user_id=298&tmstmp="+tmstmp;
var params="user_id=298&tmstmp="+tmstmp;

xmlHttp.onreadystatechange=function () {

 if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){

if (xmlHttp.responseText!=""){






  $stmt = $conn->prepare('SELECT * FROM user where recipient=:recipient ORDER BY cr_id desc');
  $stmt->bindParam(':recipient', $user_id);


if ($currentmodif <= $tmstmp) { ///**HERE IS THE QUESTION !!
 $currentmodif =$r['database_timestamp'];

echo $r['message'].'-->'.$tmstmp;

My question is :

Actually in php

  • if ($currentmodif <= $tmstmp) Must be While ($currentmodif <= $tmstmp)

According to the tutorials I found. But it´s not work if I use while Until I change it to be (>) WHY ?

  • If I will keep on using if like this instead of using While is it wrong to do ?

    Do you have any suggestion about this script I made ?

After a while (let´s say some minutes) I got message Service unavailable bla bla bla

I really wonder how the other poeple do the long polling without problem, Example facebook or other chat comet.

Thank you for any answer.
  • 写回答

1条回答 默认 最新

  • dqb78642 2013-09-05 15:52


    Basically my answer was to switch from long polling to short polling. This was wrong of me. I am a bad answerer.

    clarification lives here, along with some pros/cons and possibly a reason for your error:
    Scaling a chat app - short polling vs. long polling (AJAX, PHP)


    First glance: I think the issue you're running into is that you're polling every second, but telling the server to sleep for a second. if you change that if to a while then you'll be telling it to sleep indefinitely, until a change has been made to the db and the $currentmodif is updated. What you want to do is send a response back to the js, not wait for a change to be made before responding.

    instead try this logic:

    • send the timestamp through as you're doing, but if the database hasn't been updated, just return an empty response or a code you can recognize as "not updated" and do nothing.
    • get rid of that "sleep", theres no need to wait to return a response.
    • suggestion: in the javascript, use a setInterval instead of a recursive setTimeout, and keep a reference to that interval around so you can clearInterval later and stop the polling.
    • I'm 99% positive that facebook and other services use websockets instead of polling (at least for any browsers that support it). you may want to investigate this.
    本回答被题主选为最佳回答 , 对您是否有帮助呢?



  • ¥20 对D盘进行分盘之前没有将visual studio2022卸载掉,现在该如何下载回来
  • ¥15 加一个K2暂停音乐的功能
  • ¥15 完成虚拟机环境配置,还有安装kettle
  • ¥15 2024年全国大学生数据分析大赛A题:直播带货与电商产品的大数据分析 问题5. 请设计一份优惠券的投放策略,需要考虑优惠券的数量、优惠券的金额、投放时间段和投放商品种类等因素。求具体的python代码
  • ¥15 有人会搭建生鲜配送自营+平台的管理系统吗
  • ¥15 用matlab写代码
  • ¥30 motoradmin系统的多对多配置
  • ¥15 求组态王串口自定义通信配置方法或代码?
  • ¥15 实验 :UML2.0 结构建模
  • ¥20 用vivado写数字逻辑实验报告撰写,FPGA实验