像facebook这样的直播更新系统

时间:2014-02-16 14:00:23

标签: php jquery ajax

我真的被困在这个,我让系统工作,但它实际上每秒刷新整个更新流,这是我的ajax请求,

setInterval(function(){
          $.ajax({type:"GET",url:"read_new_post.php?account_id="+currentuser,success:function(response){

                            $(".new_update").html(response);        
            }
        });
        },1000);

这是我的php脚本,我认为echo部分作为响应返回

if($i==0)
            {
                echo "<div class='new_update'></div>";
            }
        echo "
          <div id='post' postid=".$posts[$i]["postid"].">
                    <div id='poster_img'>&nbsp;&nbsp;<img src='images/icons/Actions-list-add-user-icon.png' name='profile_img_for_post' width='40' id='profile_img_for_post' /><span id='poster_name'>".$poster_details[0]["firstname"]." ".$poster_details[0]["lastname"]."</span></div>
                    <div id='content'><br />
                    <span id='content_container'>".nl2br($posts[$i]["post_content"])."</span>
                    <br />
                    </div>
                    <div id='attributes'>
                        <span id='totallikes'><span id='like_dot' ></span>
<span id='text_tlikes'>10</span></span>
                      <span id='like'> </span>
                        <span id='share'><span id='share_txt'>SHARE</span><span id='share_circle_one'></span><span id='share_circle_two'></span></span>
                         <span id='comment' class='cmnt_cls'><img src='images/comment.png' width='32' height='32' /></span>
                         <span id='post_time'>".$posts[$i]["post_date"]."</span>
                         <br />
                         <br>
                         <br>
                            <br />
                            <br />
                      <div id='total_comments' class='total_comment_slider'>

                      </div>
                      <div id='comments'>
                            <img src='images/image.jpg' width='30' height='30' />
                        <span id='cmntbox'>
                        <textarea name='your_comment' placeholder='Say Something'></textarea>

                        <input type='submit' value='Comment' id='comment_submit' /></span>
                      </div>

            </div>
                </div>";

我的问题是,有什么方法可以将返回的ajax数据从php页面添加到div“center”,这样我的更新流就不会刷新了吗?

1 个答案:

答案 0 :(得分:1)

您应该使用WebSockets动态获取数据。每1000ms发出一次ajax请求效率很低。这是一个插件,它使得实现WebSockets免费麻烦,它适用于许多环境php,nodejs,ruby等:

http://socket.io/