在优先级AJAX请求的情况下,如何处理浏览器对每个域的并行请求的限制? [英] How to deal with browser's limit of parallel requests per domain in case of a priority AJAX request?

查看:210
本文介绍了在优先级AJAX请求的情况下,如何处理浏览器对每个域的并行请求的限制?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

想象一下如下情况:

我们的网站已经触发了大约20个请求(甚至更多)。这些可以是任何类型的请求 - 我们不知道如何再次触发它们。在此网站上,所有请求都以同一网址为目标。请求可以有订阅的事件监听器。

There are about 20 requests (or even more) which has been triggered by our website. These can be any kind of requests - we don't know how to trigger them again. On this website, all the requests target the same url. The requests can have subscribed event listeners.

如果使用Chrome,则会发送前6个请求,其他请求将在队列中等待发送(因为每个域的并行请求限制。)

In case of using Chrome, the first 6 requests are sent and the others are waiting in a queue to be sent (because of the parallel request limit per domain).

此时网页会触发一个非常重要的请求(称之为VIR),该请求具有更高的优先级,然后发送到服务器,然后发送前20个请求。其他请求(及其事件监听器)也很重要,所以我们不能只是中止它们立即发送VIR。

At this moment the webpage triggers a very important request (lets call it "VIR") which has a higher priority to be sent to the server then the previous 20 requests. The other requests (and their event listeners) are also kind of important so we can't just abort them to send the VIR immediately.

我们需要一个解决方案获取所有待处理的请求(6个已发送+ 14个队列),中止它们,然后发送VIR,然后再发送其他人,并使用之前附加的相同事件监听器。

We need a solution to get all the pending requests (6 sent + 14 in the queue), abort them, then send the VIR, and then send the others again with the same event listeners attached that they had before.

如果没有其他(开箱即用)解决方案,2个基本问题是:

In case of no other (out of the box) solution, the 2 basic questions are:


  • 是否可以引用所有待处理的请求(包括队列)?

  • 是否可以中止xhr然后再次发送(通过以某种方式克隆它们,或者我不做不知道吗?

还有一个相关问题:


  • 我记得每个主机名的并行请求限制是浏览器的限制,而不是仅限当前选项卡。是否有一个神奇的解决方案来处理它?如果是的话,我真的想写吗? :)

请注意所有请求都必须发送相同的域即可。 (意味着使用VIR定位不同的域不是此处的选项)。
但是使用websocket或http / 2可以解决基本问题,这些不是当前问题的选项。

Be aware of that all the requests have to be sent the same domain. (Means that targeting a different domain with the VIR is not an option here). However using websocket or http/2 could solve the basic problem, those are not options in this current question.

我很感激任何想法!
Thx提前!

I appreciate any idea on this! Thx in advance!

pm:是的,这是一个javascript问题:)

pm.: And yes, it's a javascript question :)

推荐答案

在中止请求,再次调用open和send方法,并且事件侦听器保持连接后,您实际上可以继续使用相同的 XmlHttpRequest 实例。通过重载xhr open / send方法更新了代码段以保存网址和有效负载:

You can actually keep using the same XmlHttpRequest instance after aborting the request, calling the open and send methods again, and the event listeners remain attached. Updated the snippet with overloading the xhr open/send method to save the url and payload:

var pending_requests = [], 
    XHRBase = {
      open: XMLHttpRequest.prototype.open, 
      send: XMLHttpRequest.prototype.send
    };

XMLHttpRequest.prototype.open = function() {

  pending_requests.push(xhr._data = {xhr: this, open: arguments});
  XHRBase.open.apply(this, arguments);
  // add event listener to pop on finish
}

XMLHttpRequest.prototype.send = function() {
  xhr._data.send = arguments;
  XHRBase.send.apply(this, arguments);
}

function priority_call(params, callback) {
  pending_requests.forEach((req) => req.xhr.abort());
  // do vip xhr
  pending_requests.forEach((req) => {
    XHRBase.open.apply(req.xhr, req.open); 
    XHRBase.send.apply(req.xhr, req.send);
  })
}

这篇关于在优先级AJAX请求的情况下,如何处理浏览器对每个域的并行请求的限制?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆