I am having issues getting values using the navigator.connection within javascript functions. I have two functions I am tryin to return data for, and both throw errors saying 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");
}
I am calling this functions using InvokeScript in C#
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;
}
}
Error
System.SystemException was unhandled by user code
HResult=-2146233087 Message=An unknown error has occurred. Error: 80020101. Source=Microsoft.Phone.Interop StackTrace: at Microsoft.Phone.Controls.NativeMethods.ValidateHResult(Int32 hr) at Microsoft.Phone.Controls.WebBrowserInterop.InvokeScript(String scriptName, String[] args) 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) at MS.Internal.JoltHelper.FireEvent(IntPtr unmanagedObj, IntPtr unmanagedObjArgs, Int32 argsTypeIndex, Int32 actualArgsTypeIndex, String eventName) InnerException:
I believe the problem is with navigation.connection
. I use this same implementation to return window.performance.timing
data and it works perfectly. Any ideas? I really need this data for performance and enhancement purposes. To note, this is all done in the WebBrowser control in Windows Phone 8.
参考http://www.sitepoint.com/use-network-information-api-improve-sensitive-websites/指出, At the time of writing, the Network Information API has little browser support and could change.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.