定期刷新或轮询
我正在尝试通过 XMLHttp(XHR) 在我的网站上使用定期刷新(ajax)/轮询来检查用户是否每 10 秒在数据库上有一条新消息,然后如果有则通过动态创建 div 来通知他/她这
function shownotice() {
var divnotice = document.createElement("div");
var closelink = document.createElement("a");
closelink.onclick = this.close;
closelink.href = "#";
closelink.className = "close";
closelink.appendChild(document.createTextNode("close"));
divnotice.appendChild(closelink);
divnotice.className = "notifier";
divnotice.setAttribute("align", "center");
document.body.appendChild(divnotice);
divnotice.style.top = document.body.scrollTop + "px";
divnotice.style.left = document.body.scrollLeft + "px";
divnotice.style.display = "block";
request(divnotice);
}
是一种可靠或稳定的方法来检查消息,特别是因为当我在 firebug 下查看时,大量请求正在发送到我的数据库?此方法是否会因请求过多而导致我的数据库关闭?有没有其他方法可以做到这一点,因为当我登录 Facebook 并在 Firebug 下检查时,没有发生或正在进行任何请求,但我知道他们也在使用定期刷新......他们是如何做到这一点的?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您可以每 10 秒检查一次新数据,但您需要执行影响较小的检查,而不是检查数据库。
我要做的是修改数据库更新过程,以便当它对某些数据进行更改时,它还会更新文件上的时间戳以显示最近发生的更改。
如果您想要比“数据库中某处发生更改”更好的粒度,您可以按用户名(或其他一些标识符)对其进行细分。然后,要更新的文件将是可能对更新感兴趣的每个用户的用户名。
因此,当您的脚本询问服务器是否有比时间 t 更新的用户 X 的任何信息时,服务器端脚本可以将文件的时间戳与时间参数进行比较,看看是否有任何信息,而不是进行数据库查询数据库中的新内容。
在更新数据库的过程中,添加代码(大致)执行以下
操作: 然后查看是否有新数据的函数类似于:
一旦发现有新数据,您就可以执行完整的数据库查询并获取您需要的任何信息。
You can check for new data every 10 seconds, but instead of checking the db, you need to do a lower impact check.
What I would do is modify the db update process so that when it makes a change to some data, it also updates the timestamp on a file to show that there is a recent change.
If you want better granularity than "something changed somewhere in the db" you can break it down by username (or some other identifier). The file(s) to be updated would then be the username for each user who might be interested in the update.
So, when you script asks the server if there is any information for user X newer than time t, instead of making a DB query, the server side script can just compare the timestamp of a file with the time parameter and see if there is anything new in the database.
In the process that is updating the DB, add code that (roughly) does:
Then your function to see if there is new data looks something like:
Once you find that there is new data, you can then do a full blown DB query and get whatever information you need.
我让 facebook 打开并运行 firebug,我大约每分钟看到一次请求,这对我来说似乎已经足够了。
Comet 使用的另一种方法是发出请求并将其保持打开状态,服务器将数据传送到客户端而不完成响应。这是一个 hack,并且违反了 HTTP 的所有原则:)。但它确实有效。
I left facebook open with firebug running and I'm seeing requests about once a minute, which seems like plenty to me.
The other approach, used by Comet, is to make a request and leave it open, with the server dribbling out data to the client without completing the response. This is a hack, and violates every principle of what HTTP is all about :). But it does work.
在大多数情况下,这是非常不可靠的,并且可能对服务器造成太大的负担。
也许您应该看看推送界面: http://en.wikipedia.org/wiki/Push_technology
我听说 Comet 是最具可扩展性的解决方案。
This is quite unreliable and probably far too taxing on the server in most cases.
Perhaps you should have a look into a push interface: http://en.wikipedia.org/wiki/Push_technology
I've heard Comet is the most scalable solution.
我怀疑 Facebook 使用 Flash 电影(他们总是下载一个名为 SoundPlayerHater.swf 的电影),用它来与服务器进行一些通信。这不会被 Firebug 捕获(不过可能会被 Fiddler 捕获)。
I suspect Facebook uses a Flash movie (they always download one called SoundPlayerHater.swf) which they use to do some comms with their servers. This does not get caught by Firebug (might be by Fiddler though).
这不是更好的方法。因为即使没有真正的更新,您最终也会每 10 秒查询一次服务器。
您可以模拟服务器推送(反向 AJAX 或 COMET)方法。这将彻底减少服务器的工作量,并且如果服务器端有更新,则只更新客户端。
根据维基百科
有关更多信息,请查看我对 类似问题
This is not a better approach. Because you ended up querying your server in every 10 seconds even there is no real updates.
Instead of this polling approach, you can simulate the server push (reverrse AJAX or COMET) approach. This will compeletly reduce the server workload and only the client is updated if there is an update in server side.
As per wikipedia
For more info, check out my other response to the similar question