diff options
author | Johannes Dahlström <johannesd@vaadin.com> | 2012-08-22 13:59:26 +0300 |
---|---|---|
committer | Johannes Dahlström <johannesd@vaadin.com> | 2012-08-22 14:30:53 +0300 |
commit | 7a8fd7b5c0223ef6820b4e5cdbe1c05578b17d4a (patch) | |
tree | 1a575d46b1d6ab45a19892f11ed5808f3728dd48 /client | |
parent | 20b403146e96b7201583e9244bc90c5a1c2191f1 (diff) | |
download | vaadin-framework-7a8fd7b5c0223ef6820b4e5cdbe1c05578b17d4a.tar.gz vaadin-framework-7a8fd7b5c0223ef6820b4e5cdbe1c05578b17d4a.zip |
Sending and receiving heartbeat requests (#9265)
Diffstat (limited to 'client')
-rw-r--r-- | client/src/com/vaadin/terminal/gwt/client/ApplicationConnection.java | 70 | ||||
-rw-r--r-- | client/src/com/vaadin/terminal/gwt/client/ui/root/RootConnector.java | 39 |
2 files changed, 70 insertions, 39 deletions
diff --git a/client/src/com/vaadin/terminal/gwt/client/ApplicationConnection.java b/client/src/com/vaadin/terminal/gwt/client/ApplicationConnection.java index a8852fe9fa..d2deb70190 100644 --- a/client/src/com/vaadin/terminal/gwt/client/ApplicationConnection.java +++ b/client/src/com/vaadin/terminal/gwt/client/ApplicationConnection.java @@ -230,6 +230,8 @@ public class ApplicationConnection { rootConnector.init(cnf.getRootPanelId(), this); showLoadingIndicator(); + + scheduleHeartbeat(); } /** @@ -2520,4 +2522,72 @@ public class ApplicationConnection { public SerializerMap getSerializerMap() { return serializerMap; } + + /** + * Schedules a heartbeat request. + * + * @see #sendHeartbeat() + */ + private void scheduleHeartbeat() { + final int interval = 1000 * getConfiguration().getHeartbeatInterval(); + if (interval > 0) { + new Timer() { + @Override + public void run() { + sendHeartbeat(); + } + }.schedule(interval); + } + } + + /** + * Sends a heartbeat request to the server. + * <p> + * Heartbeat requests are used to inform the server that the client-side is + * still alive. If the client page is closed or the connection lost, the + * server will eventually close the inactive Root. + * <p> + * <b>TODO</b>: Improved error handling, like in doUidlRequest(). + * + * @see #scheduleHeartbeat() + * @see com.vaadin.ui.Root#heartbeat() + */ + private void sendHeartbeat() { + final String uri = addGetParameters( + translateVaadinUri(ApplicationConstants.APP_PROTOCOL_PREFIX + + ApplicationConstants.HEARTBEAT_REQUEST_PATH), + ApplicationConstants.ROOT_ID_PARAMETER + "=" + + getConfiguration().getRootId()); + + final RequestBuilder rb = new RequestBuilder(RequestBuilder.POST, uri); + + final RequestCallback callback = new RequestCallback() { + + @Override + public void onResponseReceived(Request request, Response response) { + int status = response.getStatusCode(); + if (status == Response.SC_OK) { + // TODO Permit retry in some error situations + scheduleHeartbeat(); + } else { + VConsole.error("Heartbeat request failed with status code " + + status); + } + } + + @Override + public void onError(Request request, Throwable exception) { + VConsole.error("Heartbeat request resulted in exception"); + VConsole.error(exception); + } + }; + + rb.setCallback(callback); + + try { + rb.send(); + } catch (RequestException re) { + callback.onError(null, re); + } + } } diff --git a/client/src/com/vaadin/terminal/gwt/client/ui/root/RootConnector.java b/client/src/com/vaadin/terminal/gwt/client/ui/root/RootConnector.java index 0a0e0e5082..39702e6ba0 100644 --- a/client/src/com/vaadin/terminal/gwt/client/ui/root/RootConnector.java +++ b/client/src/com/vaadin/terminal/gwt/client/ui/root/RootConnector.java @@ -23,16 +23,10 @@ import com.google.gwt.core.client.Scheduler; import com.google.gwt.dom.client.NativeEvent; import com.google.gwt.dom.client.Style; import com.google.gwt.dom.client.Style.Position; -import com.google.gwt.http.client.Request; -import com.google.gwt.http.client.RequestBuilder; -import com.google.gwt.http.client.RequestCallback; -import com.google.gwt.http.client.RequestException; -import com.google.gwt.http.client.Response; import com.google.gwt.user.client.Command; import com.google.gwt.user.client.DOM; import com.google.gwt.user.client.Event; import com.google.gwt.user.client.History; -import com.google.gwt.user.client.Timer; import com.google.gwt.user.client.Window; import com.google.gwt.user.client.ui.RootPanel; import com.google.gwt.user.client.ui.Widget; @@ -90,14 +84,6 @@ public class RootConnector extends AbstractComponentContainerConnector com.google.gwt.user.client.Window.setTitle(title); } }); - final int heartbeatInterval = getState().getHeartbeatInterval(); - new Timer() { - @Override - public void run() { - sendHeartbeat(); - schedule(heartbeatInterval); - } - }.schedule(heartbeatInterval); } @Override @@ -455,29 +441,4 @@ public class RootConnector extends AbstractComponentContainerConnector } }); } - - private void sendHeartbeat() { - RequestBuilder rb = new RequestBuilder(RequestBuilder.POST, "url"); - - rb.setCallback(new RequestCallback() { - - @Override - public void onResponseReceived(Request request, Response response) { - // TODO Auto-generated method stub - - } - - @Override - public void onError(Request request, Throwable exception) { - // TODO Auto-generated method stub - - } - }); - - try { - rb.send(); - } catch (RequestException re) { - - } - } } |