检查某人的带宽并根据它加载内容 [英] Checking someones bandwidth and loading content based on it

查看:118
本文介绍了检查某人的带宽并根据它加载内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看过很多问题没有回答这个问题,是否可以使用java脚本检查某些带宽并根据它加载特定内容?

I have seen a number of questions that don't answer this, is it possible to check someones bandwidth using java script and load specific content based on it?

BBC似乎在使用我的手机和不知名的时候给了我低质量的图像。

The BBC seem to give me low quality images when using my mobile and in the middle of nowhere.

这个很酷的服务看起来这个和它的CDN所以它可以是服务器端。

by the looks of this this cool service does this and its a CDN so it could be server side.

http:// www .resrc.it / docs /

有谁知道他们是怎么做到的?或者我如何使用asp.net或javascript或社区开源插件来做到这一点。

Does anyone know how they do it? or how I could do it using asp.net or javascript, or an community opensource plug in.

我认为可以使用 https://github.com/yahoo/boomerang/ 但不确定这是否是它的真正目的。

I think it may be possible with https://github.com/yahoo/boomerang/ but not sure this is its true purpose.

推荐答案

基本上你这样做:


  • 启动计时器

  • 通过ajax调用加载固定大小的文件,例如图像

  • 停止计时器

  • 取一些样本和计算平均坏宽度

  • Start a timer
  • Load an fixed size file e.g a image through an ajax call
  • Stop the timer
  • Take some samples and compute the average badwidth

像这样的Somethign可以工作:

Somethign like this could work:

//http://upload.wikimedia.org/wikipedia/commons/5/51/Google.png
//Size = 238 KB
function measureBW(cnt, cb) {
    var start = new Date().getTime();
    var bandwidth;
    var i = 0;
    (function rec() {
        var xmlHttp = new XMLHttpRequest();
        xmlHttp.open('GET', 'http://upload.wikimedia.org/wikipedia/commons/5/51/Google.png', true);

        xmlHttp.onreadystatechange = function () {
            if (xmlHttp.readyState == 4) {
                var x = new Date().getTime() - start;
                bw = Number(((238 / (x / 1000))));
                bandwidth = ((bandwidth || bw) + bw) / 2;
                i++;
              if (i < cnt) {
                start = new Date().getTime();rec();
              }
                else cb(bandwidth.toFixed(0));
            }
        };
        xmlHttp.send(null);
    })();

}

measureBW(10, function (e) {
    console.log(e);
});

不是 var xmlHttp = new XMLHttpRequest(); 不适用于所有浏览器,你应该检查UserAgent并使用正确的

Not that var xmlHttp = new XMLHttpRequest(); won't work on all browsers, you should check for the UserAgent and use the right one

当然它只是一个估计价值。

And of course its just an estimated value.

下面是 JSBin 示例

这篇关于检查某人的带宽并根据它加载内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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