Priya
Priya

Reputation: 471

How to handle internet connection down in GWT application

I handle a website which is designed in GWT and I want to check if internet connection goes down in between accessing the website. If internet is down I want to give message as cannot connect to server or something like Gmail handles it.

Can anybody suggest what will be the best way to handle this?

Upvotes: 3

Views: 1322

Answers (1)

Jason Hall
Jason Hall

Reputation: 20920

This is what the onFailure(Throwable t) method on AsyncCallback is for. This method is called when the RPC fails for any reason, including (but not limited to) loss of connection.

Since the Throwable that gets passed into onFailure() can be anything, the pattern used in the docs is:

public void onFailure(Throwable caught) {
  // Convenient way to find out which exception was thrown.
  try {
    throw caught;
  } catch (IncompatibleRemoteServiceException e) {
    // this client is not compatible with the server; cleanup and refresh the 
    // browser
  } catch (InvocationException e) {
    // the call didn't complete cleanly
  // other Throwables may be caught here...
  } catch (Throwable e) {
    // last resort -- a very unexpected exception
  }
}

Specifically, a lack of internet connection will cause an InvocationException to be passed to onFailure()

Upvotes: 2

Related Questions