admin管理员组

文章数量:1330564

I need to send video to my clients on their mobile but each client connect to internet in different way there is slow connection and there is fast and i have 2 version from video so i can send the small one to slow connection and the large to fast.

setInterval(function(){
    if(navigator.onLine){
        $("body").html("Connected.");
    }else{
        $("body").html("Not connected.");
    }
},1000);

the code above return if connected or not.

the question now is:

Is there way to detect internet connection type on my mobile? such as 3G, WIFI ... etc.

I need to send video to my clients on their mobile but each client connect to internet in different way there is slow connection and there is fast and i have 2 version from video so i can send the small one to slow connection and the large to fast.

setInterval(function(){
    if(navigator.onLine){
        $("body").html("Connected.");
    }else{
        $("body").html("Not connected.");
    }
},1000);

the code above return if connected or not.

the question now is:

Is there way to detect internet connection type on my mobile? such as 3G, WIFI ... etc.

Share Improve this question asked Dec 24, 2012 at 12:47 Ayman HusseinAyman Hussein 3,8577 gold badges31 silver badges48 bronze badges 3
  • Rather than getting the type of connection, you might be able to estimate the speed by requesting a file/image of a known size and timing the response. – MrWhite Commented Dec 24, 2012 at 12:53
  • @Rory McCrossan how can i do that because I am newbie on stackoverflow. – Ayman Hussein Commented Dec 25, 2012 at 7:33
  • @AymanHussein You need to revisit your old questions and click the green tick to the left of the answer which best answered your question – Rory McCrossan Commented Dec 25, 2012 at 10:18
Add a ment  | 

4 Answers 4

Reset to default 4

You can code something similar to this.

var imageAddr = "http://www.tranquilmusic.ca/images/cats/Cat2.JPG" + "?n=" + Math.random();
    var startTime, endTime;
    var downloadSize = 5616998;
    var download = new Image();
    download.onload = function () {
        endTime = (new Date()).getTime();
        showResults();
    }
    startTime = (new Date()).getTime();
    download.src = imageAddr;

    function showResults() {
        var duration = (endTime - startTime) / 1000; //Math.round()
        var bitsLoaded = downloadSize * 8;
        var speedBps = (bitsLoaded / duration).toFixed(2);
        var speedKbps = (speedBps / 1024).toFixed(2);
        var speedMbps = (speedKbps / 1024).toFixed(2);
        alert("Your connection speed is: \n" + 
               speedBps + " bps\n"   + 
               speedKbps + " kbps\n" + 
               speedMbps + " Mbps\n" );
    }​

You can use this code to detect the client internet speed. There's no way you could detect connection type. A 256+ kb file would be sufficient to detect the speed.

You can only do that if the platform exposes some specific API, but it's not part of the regular browser API exposed to JavaScript. So the short answer is: no.

An alternative would be to make a request to a file of a known size and see how long it takes to transfer, from which you can deduce the speed of the connection, but this means extra traffic, so be sure to choose the file well (it should be small, so as to not generate too much mobile traffic, but not too small that you don't get any useful info from downloading it).

Unfortunately though, that might not tell you much, because, being mobile, the client is likely to switch from one connection to a pletely different one at any second. Even more, on the exact same connection, the time it takes to receive data from the server can be drastically different from one second to the next, depending on many factors that affect mobile clients. So if this is a video application, this really should be controlled by the server, and the server should decide to throttle down the quality of the video if it sees that too few packets are being sent. There are media servers that do just that, such as Red5, Wowza or Adobe Media Server.

One way is to request a file and see how long it takes to download. See this for an example.

If your clients are on a mobile device, every additional byte that your site downloads may cost them. Also, there are many factors that influence how long downloading a file will take, and the "connection type" is just one of them.

However, this method should give you a good approximation.

The original question doesn't differentiate between connection speed for (A) downloads and (B) uploads (yes, he needs to send video to the end user, but perhaps it's a video that the user just uploaded). The accepted answer is useful for measuring download speeds.

For upload speeds, you might consider embedding a base64-encoded image in a hidden form field for a hidden form. Also make a hidden iframe and target the hidden form into the hidden iframe. Submit the hidden form via javascript, storing the current time in a JS variable. On the resulting page, when onload fires, municate via javascript between the iframe and its parent window, sending the time when the onload event fired. Compare the start time with the end time, and divide by the amount of bytes transmitted.

You'll need to have the document in the iframe be on the same domain as the document in the window that contains the iframe, in order to adhere to the browser's same-origin policy.

本文标签: javascriptdetect internet connection type on my mobileStack Overflow