服务器端PHP长轮询

时间:2012-09-14 16:35:26

标签: php javascript jquery ajax long-polling

我已经提出了许多问题并尝试了许多不同的事情(编写和阅读教程)关于网络上的长轮询,但在尝试长时间轮询时没有关于编写服务器端php的可靠结果。我有我的客户端和服务器端代码设置..只有我有几个问题。

  1. 该通话不会从2周前调用的最后一个插入ID中调用新记录
  2. 我尝试过很多不同的事情,这些结果并不是我想要的。

    所有我需要的是登录的用户查看自从Feed中的上一篇文章以来朋友发布的任何新帖子。 ajax选择Feed中最后一个工作正常的ID。发送到我的PHP。我希望我的php然后决定数据库中是否有一个新的帖子,其ID高于它发送的当前ID。使用JSON将其发回并将其插入到Feed中。 如果它根本没有任何东西,那么就不要发布任何内容。但是它的发布内容并没有发布任何新结果。所以它错了。 我想让呼叫持续大约一分钟也停止对我的服务器的大量呼叫。

    我怀疑这里有很多人都有打电话给服务器的经验,所以真的可以找到我搞砸了的地方。我认为它本身就是PHP代码,因为我不习惯使用JSON和WHILE循环。

    这就是我所拥有的

    AJAX

      $calls = "SELECT  FROM streamdata ORDER BY streamitem_id DESC LIMIT 1";
        $chants = mysqli_query($mysqli, $calls) or die(mysqli_error($mysqli));
    
        $streamitem_catch = mysqli_fetch_array($chants);
        ?>
        <script type="text/javascript" charset="utf-8">
    
        function wait() {
            var streamitem_id =<? echo $streamitem_catch['streamitem_id']; ?>;
        $.ajax({
            type: "POST",
            url: "testingajaxfeed.php?streamitem_id=" + streamitem_id,
            async: true,
            cache: false,
        dataType: "json",
        data: { streamitem_id: streamitem_id }, 
            success: function (response){
                   $("#homestatusid").prepend("<div id='divider-"+response['streamitem_id']+"'><div class='userinfo'><a href='/profile.php?username="+response['username']+"'><img class='stream_profileimage' style='border:none;padding:0px;display:inline;' border=\"0\" src=\"imgs/cropped"+response['id']+".jpg\" onerror='this.src=\"img/no_profile_img.jpeg\"' width=\"40\" height=\"40\" ></a><div class'delete' style='cursor:pointer;position:relative;top:0px;float:right;padding-right:5px;' onclick=\"delete_('"+response['streamitem_id']+"');\">X</div><a href='/profile.php?username="+response['username']+"'>"+response['first']+" "+ response['middle']+" "+response['last']+"</a><span class='subtleLink'> said</span><br/><a class='subtleLink' style='font-weight:normal;'>"+response['streamitem_timestamp']+"</a><hr>"+response['streamitem_content']+"<div style='height:20px;' class='post_contextoptions'><div id='streamcomment'><a style='cursor:pointer;' id='commenttoggle_"+response['streamitem_id']+"' onclick=\"toggle_comments('comment_holder_"+response['streamitem_id']+"');clearTimeout(streamloop);swapcommentlabel(this.id);\">Write a comment...</a></div><div id='streamlike'><a title='Like "+response['first']+" "+ response['middle']+" "+response['last']+"s status' id='likecontext_"+response['streamitem_id']+"' style='cursor:pointer;' onClick=\"likestatus("+response['streamitem_id']+",this.id);\"><div style='width:50px;' id='likesprint"+response['streamitem_id']+"'>Like</a></div><div style='width:50px;' id='likesprint"+response['streamitem_id']+"'><a title='See who likes "+response['first']+" "+ response['middle']+" "+response['last']+"s status' href='include/likes.php?streamitem_id="+response['streamitem_id']+"' /></a></div></div></form></div><div id='streamdislike'><a id='dislikecontext_"+response['streamitem_id']+"' style='cursor:pointer;' onClick=\"dislikestatus("+response['streamitem_id']+",this.id);\"><div style='width:70px;' id='dislikesprint"+response['streamitem_id']+"'>Dislike</a></div><div style='width:70px;' id='dislikesprint"+response['streamitem_id']+"'></div></div></form><div class='stream_comment_holder' style='display:none;' id='comment_holder_"+response['streamitem_id']+"'><div id='comment_list_"+response['streamitem_id']+"'></div><div class='stream_comment_inputarea'><form id='mycommentform' method='POST'  class='form_statusinput'>\
        <input type='hidden'  name='streamidcontent' id='streamidcontent' value='"+response['streamitem_id']+"'>\
        <input type='input' name='commentingcontents' id='commentingcontents' placeholder='Say something' autocomplete='off'>\
        <input type='submit' id='button' value='Feed'><br/></div></div>");
        {
        return false;
        setTimeout('wait()',1000);
         }
        error: function wait(XMLHttpRequest, textStatus, errorThrown)
        { 
        alert("error: " + textStatus + "(" + errorThrown + ")");   
        setTimeout('wait()',1000);
        }
        }
        });
        }
    
    $(document).ready(function(){
    
        wait();
    });
    
    
        </script>
    

    PHP

    if (isset($_POST['streamitem_id'])) {
    $lastID = $_POST['streamitem_id'];
    
    if(empty($lastID)) {
     die('timeout');
    }
    else {
    $following_string = $_SESSION['id'];
    $result="SELECT d.*, c.*, u.*
      FROM streamdata          AS d
      JOIN streamdata_comments AS c ON d.streamitem_id = c.comment_streamitem
      JOIN users               AS u ON u.id = c.comment_poster
     WHERE c.comment_poster = '$following_string'
       AND d.streamitem_id < '$lastID'
       AND (d.streamitem_target  = '$following_string' OR
           d.streamitem_creator = '$following_string')
       OR  d.streamitem_creator IN $friendlist 
       AND d.streamitem_target IN $friendlist
       ORDER BY d.streamitem_id DESC LIMIT 1";
    $result = mysqli_query($mysqli, $result) or die(mysqli_error($mysqli));
     while($row = mysqli_fetch_array($result))
        {
            $last_msg_content = $row['streamitem_content']; 
            $last_msg_id = $row['streamitem_id'];
            $last_msg_timestamp = $row['streamitem_timestamp'];
            $last_msg_comment_id = $row['comment_id'];
            $last_msg_comment_content = $row['comment_content'];
            $last_msg_comment_poster = $row['comment_poster'];
            $last_msg_comment_datetime = $row['comment_datetime'];
            $last_msg_comment_streamitem = $row['comment_streamitem'];
            $last_msg_username = $row['username'];
            $last_msg_user_id = $row['id'];
            $last_msg_first = $row['first'];
            $last_msg_middle = $row['middle'];
            $last_msg_last = $row['last'];
        }}}
    
    
    $response = array();
    $response['streamitem_content'] = $last_msg_content;
    $response['streamitem_id'] = $last_msg_id;
    $response['streamitem_timestamp'] = Agotime($last_msg_timestamp);
    $response['comment_id'] = $last_msg_comment_id;
    $response['comment_content'] = $last_msg_comment_content;
    $response['comment_poster'] = $last_msg_comment_poster;
    $response['comment_datetime'] = Agotime($last_msg_comment_datetime);
    $response['comment_streamitem'] = $last_msg_comment_streamitem;
    $response['username'] = $last_msg_username;
    $response['id'] = $last_msg_user_id;
    $response['first'] = $last_msg_first;
    $response['middle'] = $last_msg_middle;
    $response['last'] = $last_msg_last;
    
    echo json_encode($response);
    
    ?>
    

0 个答案:

没有答案
相关问题