navigator.connection 在 HTML5 中不返回值

本文关键字:返回值 HTML5 connection navigator | 更新日期: 2023-09-27 17:55:52

我在javascript函数中使用navigator.connection获取值时遇到问题。我有两个函数正在尝试返回数据,并且都抛出错误说System.SystemException。

//Connection Type
    function getConnectionType() {
        //var connectionType = navigator.connection;
        var connection = navigator.connection || navigator.mozConnection || navigator.webkitConnection;
        var connectionType = connection.type
        window.external.notify("COT" + connectionType.toString());
    }
//Bandwidth
function getBandwidth() {
        //var online = navigator.onLine;
        var connection = navigator.connection || navigator.mozConnection || navigator.webkitConnection;
        var bandwidth = connection.bandwidth
        //window.external.notify("BAN" + bandwidth.toString() + " Mbps");
        window.external.notify("BAN" + bandwidth.toFixed(2) + " Mbps");
    }

我在 C# 中使用 InvokeScript 调用此函数

private void RunPerformanceTestButton_Click(object sender, RoutedEventArgs e)
    {
        //Bandwidth
        object bandwidth = Browser.InvokeScript("getBandwidth"); //Error
        //Connection Type
        object connectionType = Browser.InvokeScript("getConnectionType"); //Error
    }
private void Browser_ScriptNotify(object sender, NotifyEventArgs e)
    {
        string value = null;
        string statistic = null;
        statistic = e.Value.Substring(0, 3);
        value = e.Value.Substring(3);     
        switch (statistic)
        {
            //Bandwidth
            case "BAN":
                BANResultTextBlock.Text = value;
                break;
            //Connection type
            case "COT":
                COTResultTextBlock.Text = value;
                break;
            default:
                return;
        }
    }

错误

System.SystemException 未由用户代码
处理 HResult=-2146233087 消息=发生未知错误。错误: 80020101. Source=Microsoft.Phone.Interop StackTrace: at Microsoft.Phone.Controls.NativeMethods.ValidateHResult(Int32 hr) at Microsoft.Phone.Controls.WebBrowserInterop.InvokeScript(String 脚本名称,字符串[] 参数) at Microsoft.Phone.Controls.WebBrowser.InvokeScript(String scriptName) at Network_Performance_Test.MainPage.RunPerformanceTestButton_Click(Object sender, RoutedEventArgs e) at System.Windows.Controls.Primitives.ButtonBase.OnClick() at System.Windows.Controls.Button.OnClick() at System.Windows.Controls.Primitives.ButtonBase.OnMouseLeftButtonUp(MouseButtonEventArgs e) at System.Windows.Controls.Control.OnMouseLeftButtonUp(Control ctrl, EventArgs e) 在MS。Internal.JoltHelper.FireEvent(IntPtr unmanagedObj, IntPtr unmanagedObjArgs, Int32 argsTypeIndex, Int32 actualArgsTypeIndex, 字符串事件名称) 内部异常:

我相信问题出在navigation.connection.我使用相同的实现来返回window.performance.timing数据,并且效果很好。有什么想法吗?我真的需要这些数据来用于性能和增强目的。请注意,这一切都是在 Windows Phone 8 中的 WebBrowser 控件中完成的。

navigator.connection 在 HTML5 中不返回值

引用

http://www.sitepoint.com/use-network-information-api-improve-responsive-websites/声明At the time of writing, the Network Information API has little browser support and could change.