0

我有一个人们可以提交评论的网站。注释被写入服务器上的 COMMENTS 文件。然后,我可以从一个单独的页面将文件 ajax-load 到一个 Div 并查看评论。

我现在想要的是显示评论的 Div 是“实时的”。在我阅读 Div 时出现的任何新评论都会自动显示在底部。

所以看起来我需要某种持续打开的 ajax-load,其中调用的 PHP 脚本持续监控 COMMENTS 文件上的时间戳,如果时间戳发生变化,则将其再次复制到客户端。

我想知道是否有更好的方法可以做到这一点,或者其中一些可能已经完成。

感谢您的任何想法。

4

3 回答 3

3

您要么必须做一些非常耗费资源的事情,要么必须熟悉诸如 node.js 和 socket.io 之类的工具才能真正进行实时更新。

参考:
http ://www.nodejs.org
http://www.socket.io
http://net.tutsplus.com/tutorials/javascript-ajax/using-node-js-and-websockets-to-build-聊天服务/

于 2013-10-30T22:17:31.710 回答
0

If you can't / don't want to install node.js on your server and implement sockets as @ReQwire suggests, you can reduce the burden on the user slightly by polling a separate resource, something like /comments?article=articleUID&since=timestamp (with time stamp being determined by the last timestamp supplied by the server — otherwise you're liable to disparities in time between server and the user's time config), and then append whatever's returned: this service would then look at your comments file for that article and only return those comments received after timestamp. This reduces the packet weight, such that you will mostly receive an empty response, or at least only new content.

于 2013-10-30T22:41:41.660 回答
0

您可以设置一个计时器事件来检查新评论并在更新时发布...

timer = setInterval(function() 
    { $.ajax( "getComments.php" )
           .done(function(data) {
                 ("#comments").innerHTML = data;
           })
    }, seconds*1000);
于 2013-10-30T22:15:55.653 回答