How to communicate two modules in GWT

2019-08-19 05:09发布

问题:

I have created two module name module1 and module2 in gwt application. I want to pass message from module1 to module2 and module2 to module1 simultaneously after some seconds. I write the following code, but it gives me error unable to find module1.gwt.xml in classpath.

        public void onModuleLoad() {
                mainBus.fireEvent(new PingEvent("-----Simulation Started-----"));
        }

        module1

        public void onModuleLoad() 
            {
                GWTEventBus.mainBus.addHandler(PingEvent.TYPE, new PingEventHandler(){
                    public void onEvent(PingEvent event) {
                        System.out.print("Inside Ping --> ");
                        new Timer(){
                            public void run() {
                                GWTEventBus.mainBus.fireEvent(new PongEvent("Pong fired..."));
                            }
                        }.schedule(1000);
                    }
                });


            }
        module2
        public void onModuleLoad() 
            {
                //final SimpleEventBus mainBus = new SimpleEventBus();
                GWTEventBus.mainBus.addHandler(PongEvent.TYPE, new PongEventHandler(){
                    public void onEvent(PongEvent event) {
                        System.out.print("Inside Pong1 --> ");
                        new Timer(){
                            public void run() {
                                GWTEventBus.mainBus.fireEvent(new PingEvent("Ping fired..."));
                            }
                        }.schedule(1000);
                    }
                });


            }

    plz help me.

回答1:

If you are trying to have two separated modules (*.nocache.js files) included in the same webpage, you can not pass messages unless you use JS.

Use JSNI to export some method from module1 so as it is available in javascript, then call this method from module2 using JSNI as well.

package my.package.module1;
public class MyClass1 implements EntryPoint {
  public void onModuleLoad() {
    exportMyJavaMethod();
  }
  public static String myJavaMethod(String message) {
    // Do whatever with the message received (create an event, etc.)
    return "Hello " + message;
  }
  private native static exportMyJavaMethod() /*-{
    $wnd.myJavaMethod = @my.package.module1.MyClass1::myJavaMethod;
  }-*/;
}


package my.package.module2;
public class MyClass2 implements EntryPoint {
  public void onModuleLoad() {
    String ret = callMyJavaMethod("foo");
  }
  private native static callMyJavaMethod(String s) /*-{
    return $wnd.myJavaMethod(s);
  }-*/;
}

Note that using JSNI, you have to pass messages based on primitive types (see documentation)

BTW: I'd rather use gwtexporter to export methods and classes I want available in JS, and gwtquery to call JS methods instead of using JSNI.



回答2:

Your app can only have one entry point, but you can have your main module inherit multiple other gwt apps. I would suggest looking into module inheritance. You can inherit a module in your .gwt.xml file, and that module will be loaded, and its onModuleLoad method will be called automatically.

https://developers.google.com/web-toolkit/doc/latest/DevGuideOrganizingProjects#DevGuideModules