【问题标题】:GWT - how can client detect that it's javascript is out-of-sync if server is updatedGWT - 如果服务器更新,客户端如何检测到它的 javascript 不同步
【发布时间】:2024-04-29 06:15:01
【问题描述】:

我有一个 GWT 应用程序,用户可以在其中无限期地打开应用程序的基于浏览器的一侧。每隔一段时间,我们就会升级应用程序——如果用户在完成此操作后在浏览器中点击重新加载,那么一切都会好起来的。然而,通常发生的情况是,他们继续使用已经打开的应用程序版本,即升级前提供的版本,然后由于客户端 Javascript 不再与服务器上的内容同步而遇到与 RPC 相关的模糊错误。

GWT 是否有任何机制,您可以启用或合并到您的代码中,以应对这种情况。我不需要任何巧妙地处理这种情况,例如尝试重新加载应用程序并重新建立用户的当前状态,一个简单的对话框解释客户端和服务器不再同步并且需要重新加载 Web 应用程序就足够了。

【问题讨论】:

    标签: gwt browser asynchronous webserver gwt-rpc


    【解决方案1】:

    接口com.google.gwt.user.client.rpc.AsyncCallback<T> 的文档提供了有关如何执行此操作的提示。

       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
         } catch (ShapeException e) {
           // one of the 'throws' from the original method
         } catch (DbException e) {
           // one of the 'throws' from the original method
         } catch (Throwable e) {
           // last resort -- a very unexpected exception
         }
       }
    

    您很可能想要处理(弹出用户对话框)IncompatibleRemoteServiceException

    【讨论】:

      【解决方案2】:

      它被称为IncompatibleRemoteServiceException,Google 的Communicating with a Server 文档中提到了如何处理包括这个在内的异常。

      【讨论】:

      • 现在看到堆栈跟踪,我感到很尴尬。我通常跳到最低的原因,通常忽略包装异常。如果我查看了*异常,我会看到 IncompatibleRemoteServiceException,并且可能会从名称中得到提示——相反,我只关注包含的 SerializationException。