关闭长轮询连接,jQuery-ajax

发布于 2024-08-06 06:14:02 字数 915 浏览 2 评论 0原文

背景
我使用类似龙卷风的服务器,支持长轮询。用户访问的每个新网页都会向服务器设置一个长轮询,如下所示:

$.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. 第 1 页已加载并发出长轮询请求,现在在服务器端空闲。
  2. 我单击第 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).

  1. Page 1 is loaded and the long-poll request is made, now idling at server side.
  2. 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 技术交流群。

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

发布评论

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

评论(1

猫性小仙女 2024-08-13 06:14:03

对于长轮询连接,您需要确保在 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.

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