从 setTimeout AJAX 调用转为超时长轮询
我创建了一个使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
长轮询不是唯一的客户端解决方案。长轮询要求服务器保持调用直到发生变化。这意味着您将为每个登录的客户端耗尽一个并发连接。这可能是某些限制并发的服务提供商的问题,因此在收到 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.