简体   繁体   English

Javascript - 检测Internet速度/带宽

[英]Javascript - Detect Internet Speed/Bandwidth

Using below script to detect internet speed of a system connected to a network. 使用以下脚本检测连接到网络的系统的Internet速度。 Reference javascript to detect internet speed 参考javascript来检测互联网速度

However, speed results on both https://fast.com/ and http://www.speedtest.net/ are different. 但是, https://fast.com/http://www.speedtest.net/上的速度结果是不同的。

var imageAddr = "http://www.kenrockwell.com/contax/images/g2/examples/31120037-5mb.jpg"; 
var downloadSize = 4995374; //bytes

function ShowProgressMessage(msg) {
    if (console) {
        if (typeof msg == "string") {
            console.log(msg);
        } else {
            for (var i = 0; i < msg.length; i++) {
                console.log(msg[i]);
            }
        }
    }

    var oProgress = document.getElementById("progress");
    if (oProgress) {
        var actualHTML = (typeof msg == "string") ? msg : msg.join("<br />");
        oProgress.innerHTML = actualHTML;
    }
}

function InitiateSpeedDetection() {
    ShowProgressMessage("Loading the image, please wait...");
    window.setTimeout(MeasureConnectionSpeed, 1);
};    

if (window.addEventListener) {
    window.addEventListener('load', InitiateSpeedDetection, false);
} else if (window.attachEvent) {
    window.attachEvent('onload', InitiateSpeedDetection);
}

function MeasureConnectionSpeed() {
    var startTime, endTime;
    var download = new Image();
    download.onload = function () {
        endTime = (new Date()).getTime();
        showResults();
    }

    download.onerror = function (err, msg) {
        ShowProgressMessage("Invalid image, or error downloading");
    }

    startTime = (new Date()).getTime();
    var cacheBuster = "?nnn=" + startTime;
    download.src = imageAddr + cacheBuster;

    function showResults() {
        var duration = (endTime - startTime) / 1000;
        var bitsLoaded = downloadSize * 8;
        var speedBps = (bitsLoaded / duration).toFixed(2);
        var speedKbps = (speedBps / 1024).toFixed(2);
        var speedMbps = (speedKbps / 1024).toFixed(2);
        ShowProgressMessage([
            "Your connection speed is:", 
            speedBps + " bps", 
            speedKbps + " kbps", 
            speedMbps + " Mbps"
        ]);
    }
}

The point here is to change video quality run time based on user's internet connection speed. 这里的要点是根据用户的互联网连接速度改变视频质量运行时间。 I need to fetch network speed, pass it to server and based on that video quality will be changed. 我需要获取网络速度,将其传递给服务器,并根据视频质量进行更改。

How do I achieve the same ? 我如何实现同样的目标?

Your network speed will vary based on the bandwidth available. 您的网络速度将根据可用带宽而有所不同。 So you need to run the speed test at particular intervals, say for every 10 or 15 secs. 因此,您需要以特定的时间间隔运行速度测试,例如每10或15秒。 But even if you are able to run these tests without compromising the performance and the strain you put on the browser. 但即使您能够在不影响浏览器性能和压力的情况下运行这些测试。 Your next objective will be to change the quality of the video to a lower quality, which again will be a head ache because it's not going to be a simple video src switch. 你的下一个目标是将视频的质量改为较低的质量,这也将是一个头痛,因为它不会是一个简单的视频src开关。

Adaptive bit rate streaming is the one you are looking for. 自适应比特率流是您正在寻找的。 There are some media servers who provide that functionality. 有些媒体服务器提供该功能。 If you are looking for something more of a open source to understand and playaround, then you can try Google Shaka Player. 如果您正在寻找更多的开源来理解和解释,那么您可以尝试Google Shaka Player。 https://github.com/google/shaka-player . https://github.com/google/shaka-player

One more thing to note is Adaptive bit rate streaming won't work in IE without any plugin help. 还有一点需要注意的是,在没有任何插件帮助的情况下,自适应比特率流将无法在IE中运行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM