是否可以“复用”?来自同一页面的许多 AJAX 请求?

发布于 2024-08-04 05:22:45 字数 318 浏览 2 评论 0 原文

我有一个页面已经同时发出多个 AJAX 请求。他们使用Comet模型,因此总是有多个HTTP连接打开,等待来自服务器的更多数据。随着未来页面变得更加复杂,可能会有更多。我担心打开太多连接。

我想知道是否有一种方法可以“复用”它们,即。有一个连接,所有请求都通过该连接发送,并将消息路由到服务器和客户端上适当的处理程序函数。

到目前为止,我无法找到一种方法来做到这一点,因为一旦我发出 HTTP 请求并且它开始等待数据,我认为不可能发送更多请求数据。我可以在打开另一个连接以发送更多数据之前关闭该连接,但如果服务器恰好在此时发送响应怎么办?

我是否试图做一些我不应该做的事情?

I have a page that already makes multiple AJAX requests at the same time. They use the Comet model, so there are always multiple HTTP connections open, waiting for more data from the server. As the page gets more complex in the future there may be even more. I'm concerned about opening too many connections.

I wonder if there is a way to "multiplex" them, ie. have a single connection through which all requests are sent and route the messages to the appropriate handler functions on both the server and the client sides.

So far I can't figure out a way to do this, because once I make an HTTP request and it begins waiting for data I don't think it's possible to send more request data. I could close that connection before opening another one to send more data, but what if the server sends a response just then?

Am I trying to do something I shouldn't?

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

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

发布评论

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

评论(3

静若繁花 2024-08-11 05:22:46

我最终做了 djna 所描述的事情(至少我认为是同一件事):始终打开一个 Comet 风格的连接,持续侦听响应,但发送 AJAX 请求照常使用新连接。每个请求都会立即返回一个“作业 ID”(因此该连接的生命周期非常短),并且该“作业”的任何进一步数据都会通过 Comet 连接发送。这样在任何给定时间最多有 2 个连接,除非同时发生 2 个 AJAX 请求,这在我的情况下是不可能的。

当然,可能有多个客户端同时使用该页面,因此为了确定需要通过 Comet 连接发送哪些作业响应,我还有一个“comet 会话 ID”。该 ID 在页面加载时生成,并且在页面的生命周期内不会更改。在创建作业的正常 AJAX 请求之后,我发送另一个非常简单的 AJAX 请求,其中包含作业 ID 和会话 ID。这将该作业与会话关联起来,并告诉服务器将该作业的所有响应发送到与该会话 ID 关联的已建立的连接。

当然,我可以将会话 ID 作为初始请求的一部分,但这意味着服务器端的每个 AJAX 方法都需要了解它,我宁愿将其单独保留,作为基础设施的一部分。

I ended up doing what djna described (at least I think it's the same thing): have one Comet-style connection always open that keeps listening for responses, but send AJAX requests using new connections, as normal. Each request returns immediately with a "job ID" (so the connection is very short-lived) and any further data for that "job" is sent down the Comet connection. This way there are at most 2 connections at any given time, unless 2 AJAX requests happen at the same time, which is unlikely in my case.

Of course, there may be multiple clients using the page at once, so to figure out which job responses need to be sent down the comet connection I also have a "comet session ID". This ID is generated when the page loads and doesn't change for the lifetime of the page. After the normal AJAX request that creates a job I send another, very simple AJAX request with the job ID and session ID. This associates that job with the session and tells the server to send all responses for that job to the alread-established connection associated with that session ID.

I could have made the session ID a part of the initial request, of course, but this would mean that each AJAX method on the server-side would need to know about it and I'd rather keep it separate, as part of the infrastructure.

女皇必胜 2024-08-11 05:22:45

为什么您认为不可能打开多个连接?你尝试过吗?

我们使用的一种方法是理论上使用两个连接:一个用于 Comet,所有异步传递都在该连接上进行多路复用,因此许多不同的消息类型将到达那里。请求/响应发生在另一个上。实际上,我们只是编写了一个传统的 Ajax 应用程序,并附加了 Comet。

Why do you think that it's impossible to have more than one connection open? Did you try?

An approach we used was to use notionally two connections: one for Comet, all async delivery was multiplexed over that, so many different message kinds would arrive there. Request/response happened on the other In effect we just wrote a conventional Ajax app, with Comet as an extra.

昔梦 2024-08-11 05:22:45

浏览器开发者有一个协议——同一子域只能同时发送 2 个请求。这就是为什么多个 AJAX 请求很有用,但要记住队列。
另一方面,由于安全原因,JScript 调用仅限于单个子域。有一个小技巧可以跳过这个限制(例如参见 http://www.simple-talk.com/dotnet/asp.net/calling-cross-domain-web-services-in-ajax/ 部分“动态脚本标签破解” ”)

There is a agreement of browser developers - that only 2 simultaneous request can be sent to the same subdomain. That is why multiple AJAX requests are useful, but keep queue in mind.
On other hand JScript invocation restricted by security reason to single subdomain. There is small trick to leap over this restriction (see for example http://www.simple-talk.com/dotnet/asp.net/calling-cross-domain-web-services-in-ajax/ section "Dynamic Script Tag hack")

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