2015-02-24 77 views
0

我们如何检查应用程序中是否有Internet连接,或者不在SmartGWT或GWT中?我必须确定是否有互联网连接,并且基于此将互联网连接的图标更改为绿色或红色,因此有没有办法在SmartGWT或GWT中实现?使用SmartGWT或GWT检查Internet连接?

+0

您连接到Web服务器?或者如果应用程序在公司网络上运行,则可以连接到网络外部? – udeleng 2015-02-25 06:52:32

+0

应用程序需要持续监视互联网访问是否可用,并基于此将图标修改为绿色或红色。简单地说,如果我连接到Wi-Fi,图标应该是绿色的,如果断开连接,图标应该是红色的。谢谢 – Sam 2015-02-25 18:04:45

回答

2

您可以创建您创建一个新的图像对象(新画面())和附加处理它的onload和onerror的性质的本地方法。您可以将图像的src属性指向您感觉指示为联机的某个网址。然后从您的onload和onerror方法,您可以调用GWT组件中的方法来更新连接指示符。在设置img.onload和img.onerror后,您需要设置img.src。

例如:

native void checkConnectivity(Example obj) /*-{ 
    var img = new Image(); 
    img.onload = function() {   
     [email protected]::online()(); 
    } 
    img.onerror = function() {      
     [email protected]::offline()(); 
    } 
    img.src = "http://exampleapp.com/test.gif"; 
}-*/; 
+0

这会持续监控互联网连接吗? – Sam 2015-02-25 21:52:22

+0

您将不得不使用GWT计时器来定期执行该方法。 – udeleng 2015-02-26 00:52:14

+0

似乎有问题,因为这不检查互联网连接。 – Sam 2015-02-26 14:20:47

0

我认为navigator.onLine将帮助您:

http://html5demos.com/offline

http://www.html5rocks.com/en/features/offline

您可能需要包装的调用到JSNI和还实现了事件监听。

public native Boolean isOnline()/*-{ 
    return window.onLine; 
}-*/; 
+0

我想检查是否有互联网访问,我想这会检查浏览器是在线还是离线模式。谢谢! – Sam 2015-02-25 18:31:20

+0

不,这也适用于互联网连接(至少铬) – 2015-02-25 19:15:10

+0

嗯,我需要这与所有浏览器btw兼容感谢您的建议! – Sam 2015-02-27 16:09:06

0

Udeleng的解决方案是最接近我来跨浏览器的方法。我一直在FF 18上测试它,但我相信它可以在任何浏览器中运行。

但是,请注意,当您拨打new Image()时,浏览器会预加载映像,将其缓存起来,以便后续对相同URL的调用不会进行HTTP调用。这会消除我们想要实现的效果,即从offlineonline状态的转换,但只要浏览器获得任何连接,图像就会被缓存,并且即使您拔下以太网电缆也会结束。

SOLUTION 添加一个状态参数的URL,并给它一个随机数,这个想法是每次浏览器欺骗,以为它是一种新的图像,该图像是从服务器获取,而不是缓存。

当然你需要添加计时器。这里是我的代码:

import com.google.gwt.core.shared.GWT; 
 
import com.google.gwt.user.client.Timer; 
 
import com.smartgwt.client.util.SC; 
 
import com.smartgwt.client.widgets.Img; 
 
import com.smartgwt.client.widgets.events.DrawEvent; 
 
import com.smartgwt.client.widgets.events.DrawHandler; 
 
public class WebConnChecker extends Img { 
 
\t private static final String ONLINE="online_stat.jpg"; 
 
\t private static final String OFFLINE="offline_stat.jpg"; 
 
\t private static final int INTERVAL=10000; 
 
\t private Timer timer; 
 

 
\t public WebConnChecker(){ 
 
\t \t setSize(16); 
 
\t \t timer=new Timer() { 
 
\t \t \t @Override 
 
\t \t \t public void run() { 
 
\t \t \t \t checkConnectivity(WebConnChecker.this); 
 
\t \t \t } 
 
\t \t }; 
 
\t \t addDrawHandler(new DrawHandler() { 
 
\t \t \t 
 
\t \t \t @Override 
 
\t \t \t public void onDraw(DrawEvent event) { 
 
\t \t \t \t timer.scheduleRepeating(INTERVAL); 
 

 
\t \t \t } 
 
\t \t }); \t \t 
 
\t } 
 
\t private void online(){ 
 
\t \t log("online detected"); 
 
\t \t this.setSrc(ONLINE); 
 
\t } 
 
\t private void offline(){ 
 
\t \t log("offline detected"); 
 
\t \t this.setSrc(OFFLINE); 
 
\t } 
 

 
\t private native void checkConnectivity(WebConnChecker checker) /*-{ 
 
    var img = new Image(); 
 
    img.onload = function() { 
 
    \t window.alert("online");   
 
     [email protected]::online()(); 
 
    } 
 
    img.onerror = function() { 
 
    \t window.alert("offline");     
 
     [email protected]::offline()(); 
 
    } 
 
    img.src = "http://cdn.sstatic.net/stackoverflow/img/apple-touch-icon.png?v=41f6e13ade69&state="+Math.floor(Math.random() * 100); 
 
}-*/; 
 
}

希望工程为

+0

感谢您的代码,但我已经想通了! – Sam 2015-03-13 19:15:25