定期刷新或轮询

发布于 2024-08-09 15:44:20 字数 931 浏览 4 评论 0 原文

我正在尝试通过 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 下检查时,没有发生或正在进行任何请求,但我知道他们也在使用定期刷新......他们是如何做到这一点的?

I am trying to use periodic refresh(ajax)/polling on my site by XMLHttp(XHR) to check if a user has a new message on the database every 10 seconds, then if there is inform him/her by creating a div dynamically like this:

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);
}

Is this a reliable or stable way to check message specifically since when I look under firebug, a lot of request is going on to my database? Can this method make my database down because of too much request? Is there another way to do this since when I login to facebook and check under firebug, no request is happening or going on but I know they are using periodic refresh too... how do they do that?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

剩余の解释 2024-08-16 15:44:20

您可以每 10 秒检查一次新数据,但您需要执行影响较小的检查,而不是检查数据库。

我要做的是修改数据库更新过程,以便当它对某些数据进行更改时,它还会更新文件上的时间戳以显示最近发生的更改。

如果您想要比“数据库中某处发生更改”更好的粒度,您可以按用户名(或其他一些标识符)对其进行细分。然后,要更新的文件将是可能对更新感兴趣的每个用户的用户名。

因此,当您的脚本询问服务器是否有比时间 t 更新的用户 X 的任何信息时,服务器端脚本可以将文件的时间戳与时间参数进行比较,看看是否有任何信息,而不是进行数据库查询数据库中的新内容。

在更新数据库的过程中,添加代码(大致)执行以下

foreach username interested in this update
{
    touch the file \updates\username
}

操作: 然后查看是否有新数据的函数类似于:

function NewDataForUser (string username, time t)
{
     timestamp ts = GetLastUpdateTime("\updates\username");
     return (ts > t);
}

一旦发现有新数据,您就可以执行完整的数据库查询并获取您需要的任何信息。

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:

foreach username interested in this update
{
    touch the file \updates\username
}

Then your function to see if there is new data looks something like:

function NewDataForUser (string username, time t)
{
     timestamp ts = GetLastUpdateTime("\updates\username");
     return (ts > t);
}

Once you find that there is new data, you can then do a full blown DB query and get whatever information you need.

ˇ宁静的妩媚 2024-08-16 15:44:20

我让 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.

℉服软 2024-08-16 15:44:20

在大多数情况下,这是非常不可靠的,并且可能对服务器造成太大的负担。

也许您应该看看推送界面: 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.

轻许诺言 2024-08-16 15:44:20

我怀疑 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).

听,心雨的声音 2024-08-16 15:44:20

这不是更好的方法。因为即使没有真正的更新,您最终也会每 10 秒查询一次服务器。

您可以模拟服务器推送(反向 AJAXCOMET)方法。这将彻底减少服务器的工作量,并且如果服务器端有更新,则只更新客户端。

根据维基百科

反向Ajax指的是Ajax设计
使用长寿命 HTTP 的模式
连接以实现低延迟
Web 服务器之间的通信
一个浏览器。基本上这是一种方式
从客户端向服务器发送数据以及
一种推送服务器数据的机制
返回浏览器。

有关更多信息,请查看我对 类似问题

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

Reverse Ajax refers to an Ajax design
pattern that uses long-lived HTTP
connections to enable low-latency
communication between a web server and
a browser. Basically it is a way of
sending data from client to server and
a mechanism for pushing server data
back to the browser.

For more info, check out my other response to the similar question

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文