有人可以引导我正确地进行实时通知吗
Can someone lead me down the right way to make a live notifications
例如,知道何时在Mysql中添加了新行
e.g Knowing when a new Row in Added in Mysql
了解php文件是否已更改???
know if a php file has changed ???
我应该怎么做?
推荐答案您可以使用setInterval()例行检查服务器是否有更新,或者可以对JavaScript使用长轮询. setInterval()的好处是它不会使连接在服务器上打开的时间过长,但是您可能在两次服务器调用之间的停机时间"内进行了更新.长轮询将为您提供近乎即时的更新,因为它会等待连接打开,直到收到新信息.但是显然,不利的一面是,您到处都有连接处于打开状态.
You could routinely check the server for updates using setInterval(), or you could employ long-polling with javascript. The benefit of setInterval() is that it doesn't keep connections opened on your server for too long, but you may have updates during the 'downtime' between server-calls. Long-polling will give you near-instant updates, as it waits with the connection opened until it receives new information. But obviously, the down side is that you've got connections staying opened all over the place.
setInterval(function(){ $.get("updates.php", {}, function(results){ if ($(results).length) { $("results").each(function(){ // do something with update messages }); } }); }, 30000); // Every 30 seconds.使用PHP/jQuery示例进行长轮询:
您可以在 blog.perplexedlabs/2009/05/04/php-jquery-ajax-javascript-long-polling/
更多推荐
实时通知jQuery
发布评论