异步开始和结束时的 resty-GWT 自定义回调



我使用resty gwt进行所有服务器通信。我想要一些指标来显示操作正在进行中。

我认为 2 个缺点:

    进度
  • 条,将显示进度百分比;
  • 动画,将在操作过程中显示,但没有任何性能。

我假设我需要添加带有回调的自定义过滤器。我想触发以下事件:RestyGwtComunicationStartRestyGwtComunicationEnd,或回调以触发onComunicationStartedonComunicationEnded .我想在一个地方声明这一点,RestyGWT 调度程序配置。另外,如果有错误,我想获取错误。

但我不知道从哪里开始。文档中没有关于它的消息。

我可以向你寻求帮助吗?我该怎么做?

因此,如果您想知道请求已发送,则由您在GWT应用程序中进行处理。您可以在触发请求时发送事件。您有多种方法可以执行此操作。

查看文档 https://resty-gwt.github.io/documentation/restygwt-user-guide.html 中的请求调度程序

然后,如果要获取进度信息,因为HTTP调用是同步的。所以没有办法轻易做到这一点。

我一直在这样做的方式如下:

1) 创建一个第一次调用以使用 POST 在后端启动处理,这将返回您的处理 ID

2) 然后在您的处理 ID 上执行 GET 操作,该 ID 将返回进度。一旦进度为 100%,它将返回结果的 ID

3) 使用结果 ID 获取结果

(您最终可以将 2 和 3 混合在一起,并在同一个 DTO 中的进度为 100% 时返回结果)

另一种选择是通过将信息从后端推送到前端(html5 websocket)来替换2)

有人已经把它作为对 resty 的拉取请求。猜猜你可以试一试:

https://github.com/resty-gwt/resty-gwt/pull/151

不幸的是,

官方文档中没有描述"调度程序/回调过滤器"功能。但我可以建议下一个解决方案(此代码应放置在模块的入口点实现中):

public void onModuleLoad() {
    //...
    //used to show busy indicator before send HTTP request        
    DispatcherFilter busyIndicatorDispatcherFilter = new DispatcherFilter() {
        @Override
        public boolean filter(Method method, RequestBuilder builder) {
            BusyIndicator.show();
            return true;
        }
    };
    //used to show busy indicator after HTTP response recieved
    CallbackFilter busyIndicatorCallbackFilter = new CallbackFilter() {
        @Override
        public RequestCallback filter(Method method, Response response, RequestCallback callback) {
            BusyIndicator.hide();
            return callback;
        }
    };
    //registering FilterawareDispatcher (and busy indicator filters) as default Dispatcher
    Defaults.setDispatcher(new DefaultFilterawareDispatcher(
            busyIndicatorDispatcherFilter,
            new DefaultDispatcherFilter(new DefaultCallbackFactory(busyIndicatorCallbackFilter))));
    //...
}

不幸的是,我没有得到足够的答案,所以我开发了自己的解决方案。

起初,我将 Resty 配置RestyGwtConfig添加到我的模块配置中

public class ClientModule extends AbstractPresenterModule {
    @Override
    protected void configure() {
        bind(RestyGwtConfig.class).asEagerSingleton();
        install(new DefaultModule.Builder()
        .defaultPlace(Routing.HOME.url)
        .errorPlace(Routing.ERROR.url)
        .unauthorizedPlace(Routing.LOGIN.url)
        .tokenFormatter(RouteTokenFormatter.class).build());
        install(new AppModule());
        install(new GinFactoryModuleBuilder().build(AssistedInjectionFactory.class));
        bind(ResourceLoader.class).asEagerSingleton();
    }
}

然后我已经为我所有的 resty gwt 通信请求设置了自定义 distpatcher。

import org.fusesource.restygwt.client.Defaults;
import org.fusesource.restygwt.client.Resource;
import pl.korbeldaniel.cms.shared.ServiceRouting;
import com.google.gwt.core.client.GWT;
import com.google.inject.Inject;
public class RestyGwtConfig {
    @Inject
    public RestyGwtConfig(RestyDispatcher dispatcher) {
        Defaults.setDispatcher(dispatcher);
    }
}

然后我添加了自定义过滤器 ( ProgressIndicatorFilter ) 来处理通信的开始结束回调

import org.fusesource.restygwt.client.Method;
import org.fusesource.restygwt.client.dispatcher.DefaultFilterawareDispatcher;
import com.google.gwt.http.client.Request;
import com.google.gwt.http.client.RequestBuilder;
import com.google.gwt.http.client.RequestException;
import com.google.inject.Inject;
public class RestyDispatcher extends DefaultFilterawareDispatcher {
    @Inject
    public RestyDispatcher(ProgressIndicatorFilter progressIndicatorFilter) {
        addFilter(progressIndicatorFilter);
    }
}

在过滤器类方法中重写filter我添加了一个事件触发器(eventBus.fireEvent(new IndicatorEvent("Rest-Gwt Comunication started"));)和注册的回调,这是整个代码:

import org.fusesource.restygwt.client.Method;
import org.fusesource.restygwt.client.dispatcher.DispatcherFilter;
import pl.korbeldaniel.cms.client.template.progressIndicator.IndicatorEvent;
import com.google.gwt.http.client.RequestBuilder;
import com.google.inject.Inject;
import com.google.web.bindery.event.shared.EventBus;
class ProgressIndicatorFilter implements DispatcherFilter {
    private AssistedInjectionFactory factory;
    private EventBus eventBus;
    @Inject
    public ProgressIndicatorFilter(AssistedInjectionFactory factory, EventBus eventBus) {
        this.factory = factory;
        this.eventBus = eventBus;
    }
    @Override
    public boolean filter(Method method, RequestBuilder builder) {
        builder.setCallback(factory.createProgressIndicatorCallback(method));
        eventBus.fireEvent(new IndicatorEvent("Resty-Gwt Comunication started"));
        return true;
    }
}

注册回调不能直接完成,例如

new ProgressIndicatorDispatcherCallback()

因为我使用依赖注入。所以我创建了一个工厂来协助注射,如下所示:

public interface AssistedInjectionFactory {
    ProgressIndicatorDispatcherCallback createProgressIndicatorCallback(Method method);
}

在这里和这里 您可以找到更多辅助注射信息。

下面是回调代码:

class ProgressIndicatorDispatcherCallback implements RequestCallback {
    private RequestCallback requestCallback;
    private EventBus eventBus;
    @Inject
    public ProgressIndicatorDispatcherCallback(@Assisted Method method, EventBus eventBus) {
        this.requestCallback = method.builder.getCallback();
        this.eventBus = eventBus;
    }
    @Override
    public void onResponseReceived(Request request, Response response) {
        endComunicationFireIvent();
        requestCallback.onResponseReceived(request, response);
    }
    @Override
    public void onError(Request request, Throwable exception) {
        endComunicationFireIvent();
        requestCallback.onError(request, exception);
    }
    private void endComunicationFireIvent() {
        eventBus.fireEvent(new IndicatorEvent("Rest-Gwt Comunication ended"));
    }
}

最新更新