关闭长轮询连接,jQuery-ajax
背景
我使用类似龙卷风的服务器,支持长轮询。用户访问的每个新网页都会向服务器设置一个长轮询,如下所示:
$.ajax({
type: 'GET',
url: "/mylongpollurl/",
dataType: 'application/json',
success: function(json) {
// I do stuff here
},
error: function(xhr, errText, ex) {
// If timeout I send a new long-poll request
}
});
问题
现在,我将依靠从 Fiddler 获得的数据来监视从我的浏览器(目前为 FF)发出的所有请求。
- 第 1 页已加载并发出长轮询请求,现在在服务器端空闲。
- 我单击第 2 页的链接,该页面已加载并设置长轮询请求,但第 1 页的长轮询请求仍在服务器端空闲(根据 Fiddler)。
这意味着当我在页面上单击时,我将堆叠所有长轮询调用,从而最终在服务器上产生大量活动连接(或者它们可能共享连接?)
我的想法
- 因为它是一个类似 Tornado 的服务器(使用 epoll),所以它可以处理相当多的连接。但我认为这个事实是不可利用的。我的意思是,在这种情况下,我不希望服务器超时(如果客户端消失)。
- 我知道那些独立页面更好地使用公共头,并且仅通过 ajax 调用交换内容,但我们今天使用的这种设计不是我的决定...
- 解决此问题的最佳方法可能是重用连接(我认为很难关闭)或在浏览器离开页面(单击另一个页面)后立即关闭连接。
谢谢
--MyGGaN
Background
I use a Tornado-like server with support for long-polls. Each new web page a user comes to sets up a long poll to the server like this:
$.ajax({
type: 'GET',
url: "/mylongpollurl/",
dataType: 'application/json',
success: function(json) {
// I do stuff here
},
error: function(xhr, errText, ex) {
// If timeout I send a new long-poll request
}
});
Problem
I will now rely on data that I get from Fiddler monitoring all requests made from my browser (FF at the moment).
- Page 1 is loaded and the long-poll request is made, now idling at server side.
- I click a link to page 2 and that page is loaded and setting up a long poll request, BUT the long poll request from page 1 is still idling at server side (according to Fiddler).
This means that I will stack all long poll calls when clicking around the page, thus end up with lots of active connections on the server (or are they maybe sharing connection?)
My thoughts
- As it's a Tornado-like server (using epoll) it can handle quite a lot of connections. But this fact is not to exploit in my opinion. What I mean is that I prefer not to have a timeout on the server for this case (were the client disappears).
- I know those stand alone pages better uses a common head and only swap content via ajax calls but this design we use today was not my call...
- The best way to solve this would probably be to have the connection reused (hard to pull off I think) or closed as soon as the browser leaves the page (you click to another page).
Thanks
-- MyGGaN
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
对于长轮询连接,您需要确保在 Fiddler 中设置了“Streaming”选项。否则,Fiddler 将保持连接打开状态,无限期地等待响应完成。
通常,当您从一个页面导航到另一个页面时,客户端应该断开打开的长轮询连接,从而有效地关闭连接。我说应该是因为这并不总是能正常工作,例如,当您关闭 IE 中的弹出窗口时。
For long-polling connections, you need to ensure that you have the "Streaming" option set inside Fiddler. Otherwise, Fiddler will keep the connection open waiting indefinitely for the response to finish.
Normally, when you navigate from page to page, the client should tear down the open long-poll connection, effectively closing the connection. I say should because this doesn't always work properly, for instance, when you close a popup window in IE.