其他主流浏览器的数字是多少?
此外,是否有方法可以绕过这些限制,而不需要让我的用户修改他们的浏览器设置?例如,jsonp请求的数量是否有限制(使用脚本标记注入而不是XmlHttpRequest对象)?
背景:我的用户可以从网页向服务器发起XmlHttpRequest请求,要求服务器在远程主机上运行ssh命令。如果远程主机宕机,则ssh命令需要几分钟才能失败,最终阻止我的用户执行任何进一步的命令。
一个增加并发连接数的技巧是从不同的子域名托管您的图像。这些将被视为单独的请求,每个域名将受到并发最大限制。
IE6、IE7 - 限制为2。如果您拥有宽带,则IE8为6,如果是拨号连接,则为2。
Browserscope 的网络测试结果将为您提供流行浏览器的每个主机名的连接数和最大连接数。该数据是通过对“野外”用户进行测试而收集的,因此始终保持最新。
对于IE6 / IE7,可以通过修改注册表来调整并发请求的数量。以下是将其设置为每个四个的方法。
[HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Internet Settings]
"MaxConnectionsPerServer"=dword:00000004
"MaxConnectionsPer1_0Server"=dword:00000004
我编写了一个单文件AJAX测试器。享受它吧!
只是因为我与我的托管提供商遇到了问题。
<?php /*
Author: Luis Siquot
Purpose: Check ajax performance and errors
License: GPL
site5: Please don't drop json requests (nor delay)!!!!
*/
$r = (int)$_GET['r'];
$w = (int)$_GET['w'];
if($r) {
sleep($w);
echo json_encode($_GET);
die ();
} //else
?><head>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script type="text/javascript">
var _settimer;
var _timer;
var _waiting;
$(function(){
clearTable();
$('#boton').bind('click', donow);
})
function donow(){
var w;
var estim = 0;
_waiting = $('#total')[0].value * 1;
clearTable();
for(var r=1;r<=_waiting;r++){
w = Math.floor(Math.random()*6)+2;
estim += w;
dodebug({r:r, w:w});
$.ajax({url: '<?php echo $_SERVER['SCRIPT_NAME']; ?>',
data: {r:r, w:w},
dataType: 'json', // 'html',
type: 'GET',
success: function(CBdata, status) {
CBdebug(CBdata);
}
});
}
doStat(estim);
timer(estim+10);
}
function doStat(what){
$('#stat').replaceWith(
'<table border="0" id="stat"><tr><td>Request Time Sum=<th>'+what+
'<td> /2=<th>'+Math.ceil(what/2)+
'<td> /3=<th>'+Math.ceil(what/3)+
'<td> /4=<th>'+Math.ceil(what/4)+
'<td> /6=<th>'+Math.ceil(what/6)+
'<td> /8=<th>'+Math.ceil(what/8)+
'<td> (seconds)</table>'
);
}
function timer(what){
if(what) {_timer = 0; _settimer = what;}
if(_waiting==0) {
$('#showTimer')[0].innerHTML = 'completed in <b>' + _timer + ' seconds</b> (aprox)';
return ;
}
if(_timer<_settimer){
$('#showTimer')[0].innerHTML = _timer;
setTimeout("timer()",1000);
_timer++;
return;
}
$('#showTimer')[0].innerHTML = '<b>don\'t wait any more!!!</b>';
}
function CBdebug(what){
_waiting--;
$('#req'+what.r)[0].innerHTML = 'x';
}
function dodebug(what){
var tt = '<tr><td>' + what.r + '<td>' + what.w + '<td id=req' + what.r + '> '
$('#debug').append(tt);
}
function clearTable(){
$('#debug').replaceWith('<table border="1" id="debug"><tr><td>Request #<td>Wait Time<td>Done</table>');
}
</script>
</head>
<body>
<center>
<input type="button" value="start" id="boton">
<input type="text" value="80" id="total" size="2"> concurrent json requests
<table id="stat"><tr><td> </table>
Elapsed Time: <span id="showTimer"></span>
<table id="debug"></table>
</center>
</body>
编辑:我写了自己的测试代码,在stackoverflow上测试过,结果良好。告诉我chrome/FF可以实现6。
var change = 0;
var simultanius = 0;
var que = 20; // number of tests
Array(que).join(0).split(0).forEach(function(a,i){
var xhr = new XMLHttpRequest;
xhr.open("GET", "/?"+i); // cacheBust
xhr.onreadystatechange = function() {
if(xhr.readyState == 2){
change++;
simultanius = Math.max(simultanius, change);
}
if(xhr.readyState == 4){
change--;
que--;
if(!que){
console.log(simultanius);
}
}
};
xhr.send();
});
它适用于大多数可以在不同时间触发readystate更改事件的网站(也称为刷新)。
我注意到在我的node.js服务器上,我必须输出至少1025个字节才能触发事件/刷新。否则,当请求完成时,事件将同时触发所有三个状态,因此这是我的后端:
var app = require('express')();
app.get("/", function(req,res) {
res.write(Array(1025).join("a"));
setTimeout(function() {
res.end("a");
},500);
});
app.listen(80);
我注意到,如果您同时使用xhr和fetch api,现在可以最多有2倍的请求。
var change = 0;
var simultanius = 0;
var que = 30; // number of tests
Array(que).join(0).split(0).forEach(function(a,i){
fetch("/?b"+i).then(r => {
change++;
simultanius = Math.max(simultanius, change);
return r.text()
}).then(r => {
change--;
que--;
if(!que){
console.log(simultanius);
}
});
});
Array(que).join(0).split(0).forEach(function(a,i){
var xhr = new XMLHttpRequest;
xhr.open("GET", "/?a"+i); // cacheBust
xhr.onreadystatechange = function() {
if(xhr.readyState == 2){
change++;
simultanius = Math.max(simultanius, change);
}
if(xhr.readyState == 4){
change--;
que--;
if(!que){
document.body.innerHTML = simultanius;
}
}
};
xhr.send();
});
使用VPN仍会影响IE 9性能
我们之前报道过,在使用VPN连接时,IE8最大并发连接数被缩减。即使浏览器流量不经过该连接也会发生这种情况。
不幸的是,IE9也受到VPN连接的影响:
我相信浏览器对于同一域名的并发http请求有一个最大数量限制,这个数量大约在4-8个请求之间,具体取决于用户的设置和浏览器。
你可以将请求设置到不同的域名上,这可能是可行的,也可能不可行。雅虎的工程师在这方面做了很多研究,你可以阅读相关内容(这里)。请记住,每添加一个新域名都需要进行DNS查找。YSlow的工程师建议使用2到4个域名来实现并行请求和DNS查找之间的良好平衡,尽管这主要关注页面加载时间,而不是后续的AJAX请求。
我可以问一下为什么你想发起这么多请求吗?浏览器限制同一域名的请求次数是有充分理由的。如果可能的话,最好将请求捆绑在一起。
升级到http 2.0的好处
使用http 2.0,每个主机的最大连接数几乎是无限制的:HTTP/2是否提高了每个主机的连接限制?