Bandwidth utility using javascript

后端 未结 3 781
天涯浪人
天涯浪人 2020-12-09 06:09

IS there any utility to measure bandwidth from my server to the client or any standard APIS.Need this for web application

3条回答
  •  眼角桃花
    2020-12-09 07:04

        // Network connection - https://github.com/daniellmb/downlinkmax
    var connectionSpeed = function() 
    {
        // Deal with vendor prefixes
        var defaultSpeed = false,
            navigator = window.navigator,
            connection = navigator.connection || navigator.mozConnection || navigator.webkitConnection || null;
        if( ! connection )
            return defaultSpeed;
    
        // assume W3C Editor's Draft 09 October 2014
        if( 'downlinkMax' in connection )
        {
            var downlinkMax = connection.downlinkMax;
            if( ! downlinkMax )
                return defaultSpeed;
            if( ! isFinite(downlinkMax) )
                return defaultSpeed;
            return downlinkMax;
        }
        // assume W3C Working Draft 29 November 2012
        if( 'bandwidth' in connection )
        {
            var bandwidth = connection.bandwidth;
            if( ! bandwidth )
                return defaultSpeed;
            if( isNaN(speed) )
                return defaultSpeed;
            // standardize connection.bandwidth value by converting megabytes per second (MB/s) to megabits per second (Mbit/s)
            return bandwidth * 8;
        }
        // assume W3C Working Draft 07 June 2011
        switch( connection.type )
        {
            // convert connection.type value to approximate downlink values
            // speed estimate is based on the median downlink value for common devices in megabits per second (Mbit/s)
            case 'none':
                return 0;
            case '2g':
                return 0.134;
            case 'bluetooth':
            case 'cellular':
                return 2;
            case '3g':
                return 8.95;
            case '4g':
                return 100;
            case 'ethernet':
                return 550;
            case 'wifi':
                return 600;            
        }
        return defaultSpeed;
    };
    

提交回复
热议问题