简体   繁体   English

在我的手机上检测互联网连接类型

[英]detect internet connection type on my mobile

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. 我需要在他们的手机上向我的客户发送视频,但是每个客户端以不同的方式连接到互联网,连接速度慢,而且我有2个版本的视频,所以我可以发送小的一个慢速连接和大到快。

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. 如3G,WIFI ......等

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. 只有当平台公开某些特定的API时,您才能这样做,但它不是暴露给JavaScript的常规浏览器API的一部分。 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 completely 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 . 有媒体服务器就是这样做的,例如Red5WowzaAdobe Media Server

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. 您可以使用此代码来检测客户端Internet速度。 There's no way you could detect connection type. 你无法检测连接类型。 A 256+ kb file would be sufficient to detect the speed. 一个256+ kb的文件足以检测速度。

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). 原始问题不区分(A)下载和(B)上传的连接速度(是的,他需要向最终用户发送视频,但可能是用户刚刚上传的视频)。 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. 对于上传速度,您可以考虑在隐藏表单字段中嵌入base64编码的图像以隐藏表单。 Also make a hidden iframe and target the hidden form into the hidden iframe. 还可以创建一个隐藏的iframe,并将隐藏的表单定位到隐藏的iframe中。 Submit the hidden form via javascript, storing the current time in a JS variable. 通过javascript提交隐藏的表单,将当前时间存储在JS变量中。 On the resulting page, when onload fires, communicate via javascript between the iframe and its parent window, sending the time when the onload event fired. 在结果页面上,当onload触发时,通过iframe与其父窗口之间的javascript进行通信,发送onload事件触发时的时间。 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. 您需要让iframe中的文档与包含iframe的窗口中的文档位于同一个域中,以便遵守浏览器的同源策略。

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

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