从 setTimeout AJAX 调用转为超时长轮询

发布于 2024-12-28 15:56:52 字数 1453 浏览 0 评论 0原文

我创建了一个使用 timeOut 每 15 秒自动刷新 DIV 的网站。 这工作得相当可靠,但在实际更新 DIV 之前可能需要长达 5 分钟的时间,这意味着脚本将在发生更改之前执行调用20 次。非常浪费昂贵的带宽和服务器性能:)

我已经阅读了很多关于进行长轮询的文章,并且我一直在尝试。

我的AJAX调用是:

intval = window.setTimeout(function() {
  $.ajax({
    type: 'GET',
    cache: false,
    url: 'url',
    beforeSend: function() { $('#timerimg').attr('src', 'img/icons/loading.gif'); },
    success: function(data) { $('#ajaxcontent').html(data); },
    complete: function() { $('#timerimg').attr('src', 'img/icons/stop.gif'); }
  });
}, 15000);

这个函数被放置在一直刷新的页面内,这使得超时函数不断重复。

现在我尝试遵循 http:// /techoctave.com/c7/posts/60-simple-long-polling-example-with-javascript-and-jquery 创建一个简单的长轮询函数

(function poll() { 
  $.ajax({ 
    type: 'GET',
    cache: false,
    url: 'url',
    success: function(data) { $('#ajaxcontent').html(data); },
    complete: poll,
    timeout: 30000
  });
})();

如果我将 url 设置为hi.txt 并使其写入 Hello World! 然后它不断完成并进行新的民意调查。这显然意味着大量同时进行的民意调查。

我该如何纠正这个问题?

AND:

自动刷新DIV是一个大型图表,其中包含来自巨大SQL-Server查询的计算。所以也许它应该只检查 SQL 查询后 .getRows() (与原始相比)是否有最轻微的变化?

我正在使用 jQuery 和 ASP-Classic 完成所有工作。

I have created a site which auto refreshes a DIV every 15 seconds using a timeOut.
This works pretty solid, but it can take up to 5 minutes before the DIV is actually updated, which means the script will do the call 20 times before a change happens. Pretty good waste of costly bandwidth and server performance :)

I have been reading a lot about doing Long Polling instead and I've been giving it a go.

My AJAX call was:

intval = window.setTimeout(function() {
  $.ajax({
    type: 'GET',
    cache: false,
    url: 'url',
    beforeSend: function() { $('#timerimg').attr('src', 'img/icons/loading.gif'); },
    success: function(data) { $('#ajaxcontent').html(data); },
    complete: function() { $('#timerimg').attr('src', 'img/icons/stop.gif'); }
  });
}, 15000);

This function was placed inside the page being refreshed all the time, which made the timeout-function to being kept repeated.

Now I have tried to follow http://techoctave.com/c7/posts/60-simple-long-polling-example-with-javascript-and-jquery to create a simple Long Polling function

(function poll() { 
  $.ajax({ 
    type: 'GET',
    cache: false,
    url: 'url',
    success: function(data) { $('#ajaxcontent').html(data); },
    complete: poll,
    timeout: 30000
  });
})();

If I am setting the url to hi.txt and making that write Hello World! then it finished constantly and doing a new poll. Which obviously means A LOT of simultaneous polls.

How do I correct this?

AND:

The autorefreshing DIV is a large chart with calculations from a huge SQL-Server query. So maybe it should just check if there has been the slightest change in the .getRows() (compared to the original) after the SQL query?

I am doing the whole thing in jQuery and ASP-Classic.

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

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

发布评论

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

评论(1

远昼 2025-01-04 15:56:52

长轮询不是唯一的客户端解决方案。长轮询要求服务器保持调用直到发生变化。这意味着您将为每个登录的客户端耗尽一个并发连接。这可能是某些限制并发的服务提供商的问题,因此在收到 503 之前请检查您的 ISP 策略。

此外,您正在处理多个并发连接问题在这里。服务器应该跟踪最后一个请求是否需要更新,或者只是向您发送回缓存的响应。如果您使用长轮询,那么服务器将保持您的连接,直到它发生变化。

以前您使用的是间隔轮询。我建议继续该路线(使用服务器端缓存),而不是将一半的 IE 带宽(最多 2 个并发)用于不频繁的更新。如果情况没有改变,只需让服务器对您的询问返回“错误”响应即可。

Long polling is not a client only solution. Long polling requires the server to hold the call until something changes. This means that you will be using up one of your concurrent connections for EVERY client logged in. This can be a problem with some service providers that limit concurrency, so check your ISP policy before you get a 503.

Also you are addressing more than one issue here. The server should keep track of whether or not the last request needs to be updated, or to just send you back the cached response. If you are using long polling, then the server will hold your connection until it has changed.

Previously you were using interval polling. I would suggest continuing that route (with server side caching) instead of dedicating half of your IE bandwidth (2 concurrent max) for an infrequent update. Just have the server kick back a "false" response to your inquiry if things haven't changed.

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