2015-02-24 78 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