且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何处理客户端服务器PlayN游戏中的RPC?

更新时间:2023-02-02 19:48:08

对于Java和Android平台上的GWT RPC,目前正在试验使用gwt-syncproxy来为GWT RPC方法提供Java客户端访问,我在他们各自的目标平台上使用Guice,Gin和RoboGuice为实例化的Game对象注入适当的异步服务实例。



在PlayN项目的核心/ pom.xml中,我包括以下依赖关系坐标,以根据需要支持来自Gin / Guice / RoboGuice的DI:

 < dependency> 
< groupId> javax.inject< / groupId>
< artifactId> javax.inject< / artifactId>
< version> 1< / version>
< / dependence>然后,我将@Inject注释添加到具体游戏实现中的任何字段:
$ p>

b
$ b

  public class TestGame implements Game {

@Inject
TestServiceAsync _testService;

...

}

html / pom.xml,我包括Gin的依赖关系坐标:

 < dependency> 
< groupId> com.google.gwt.inject< / groupId>
< artifactId> gin< / artifactId>
< version> 1.5.0< / version>
< / dependence>我创建了TestGameGinjector和TestGameModule类:

TestGameGinjector.java

  @GinModules(TestGameModule.class)
public interface TestGameGinjector extends Ginjector {
TestGame getGame();
}

TestGameModule.java
$ b public class TestGameModule extends AbstractGinModule {
@Override
protected void configure(){
}
}

由于目前我只注入TestServiceAsync接口,我不需要任何在TestGameModule.configure()方法中的实现; Gin通过GWT.create()为我管理AsyncServices的实例化。



然后我将以下内容添加到TestGame.gwt.xml

 < inherits name ='com.google.gwt.inject.Inject'/> 

最后,我对TestGameHtml.java做了以下更改

  public class TestGameHtml extends HtmlGame {

private final TestGameGinjector _injector = GWT.create(TestGameGinjector.class);

@Override
public void start(){
HtmlPlatform platform = HtmlPlatform.register();
platform.assetManager()。setPathPrefix(test /);
PlayN.run(_injector.getGame());
}
}

这几乎涵盖了PlayN的HTML5平台。



对于Java平台,我将以下依赖关系坐标添加到java / pom.xml中:

 < dependency> 
< groupId> com.gdevelop.gwt.syncrpc< / groupId>
< artifactId> gwt-syncproxy< / artifactId>
< version> 0.4-SNAPSHOT< / version>
< / dependence>

< dependency>
< groupId> com.google.inject< / groupId>
< artifactId> guice< / artifactId>
< version> 3.0-rc2< / version>
< / dependence>

请注意,Google代码上的gwt-syncproxy项目不包含pom.xml。我有一个gwt-syncproxy分支的版本版本,可以通过git在 https:// bitbucket。 org / hatboyzero / gwt-syncproxy.git 。您应该能够克隆它,运行 mvn clean package install 将其导入您的本地Maven存储库。



无论如何,我创建了一个TestGameModule .java for Java平台如下:

  public class TestGameModule extends AbstractModule {

@Override
protected void configure(){
bind(TestServiceAsync.class).toProvider(TestServiceProvider.class);
}

public static class TestServiceProvider implements Provider< TestServiceAsync> {
public TestServiceAsync get(){
return(TestServiceAsync)SyncProxy.newProxyInstance(
TestServiceAsync.class,
Deployment.gwtWebPath(),// URL to webapp - http: //127.0.0.1:8888/testgame
test
);
}
}
}

我修改了TestGameJava.java如下:

  public class TestGameJava {

public static void main(String [] args){
Injector _injector = Guice.createInjector(new TestGameModule());

JavaPlatform platform = JavaPlatform.register();
platform.assetManager()。setPathPrefix(test / images);
PlayN.run(_injector.getInstance(TestGame.class));
}
}

我在Android平台上进行了类似的练习, RoboGuice - 没有深入的细节,相关的更改/代码段如下:



pom.xml依赖

 < dependency> 
< groupId> com.gdevelop.gwt.syncrpc< / groupId>
< artifactId> gwt-syncproxy< / artifactId>
< version> 0.4-SNAPSHOT< / version>
< / dependence>

< dependency>
< groupId> org.roboguice< / groupId>
< artifactId> roboguice< / artifactId>
< version> 1.1.2< / version>
< / dependence>

< dependency>
< groupId> com.google.inject< / groupId>
< artifactId> guice< / artifactId>
< version> 3.0-rc2< / version>
< classifier> no_aop< / classifier>
< / dependence>

TestGameApplication.java

  public class TestGameApplication extends RoboApplication {
@Override
protected void addApplicationModules(List< Module> modules){
modules.add(new TestGameModule ());
}
}

TestGameModule.java / p>

  public class TestGameModule extends AbstractModule {

@Override
protected void configure(){
bind(TestServiceAsync.class).toProvider(TestServiceProvider.class);
}

public static class TestServiceProvider implements Provider< TestServiceAsync> {
public TestServiceAsync get(){
return(TestServiceAsync)SyncProxy.newProxyInstance(
TestServiceAsync.class,
Deployment.gwtWebPath(),// URL to webapp - http: //127.0.0.1:8888/testgame
test
);
}
}
}


$ b TestGameActivity.java

  public class TestGameActivity extends GameActivity {

@Override
public void onCreate(Bundle savedInstanceState){
final Injector injector =((RoboApplication)getApplication())。getInjector();
injector.injectMembers(this);
super.onCreate(savedInstanceState);
}

@Override
public void main(){
platform()。assetManager()。setPathPrefix(test / images);
final Injector injector =((RoboApplication)getApplication())。getInjector();
PlayN.run(injector.getInstance(TestGame.class));
}
}

这是一个快速和脏的故事, / Guice / RoboGuice + GWT在我的项目中工作,我已经验证它的工作在Java和HTML平台精美。



无论如何,有GWT方法提供RPC调用多个PlayN平台:)。


I'd like to use PlayN to create a client/server card game, e.g. Hearts. While I'm mostly focusing on the HTML5 output, I'd ideally like to be output-platform-agnostic in case I decide to make an Android client in the future. How should I approach the RPC mechanism?

These are the options I've thought of:

  1. Use JSON for RPCs with get()/post() methods - write a servlet that accepts/returns JSON, and make all versions of client code use that. This seems doable, but I'm concerned about JSON's verbosity. Once I get Hearts working I'd like to move on to more complex games, and I'm worried that JSON will result in a lot of much-larger-than-necessary messages being passed back and forth between client and server. I don't actually know how to work with JSON in Java, but I assume this is doable. Are my assumptions in-line? How well does Java work with JSON?
  2. Continue using GWT-RPC. I can do this by taking an asynchronous service interface in my core (platform-agnostic) constructor, and in my HTML main() I pass in the GWT Async interface generated by GWT.create(MyService.class) (or at least a wrapper around it). I have no idea how well this would work for non-HTML versions though. Is it possible for me to use GWT-RPC from client-side Java code directly?
  3. Use some other form of RPC. Any suggestions?

For the GWT RPC on the Java and Android platforms, I'm currently experimenting with using gwt-syncproxy to provide Java client access to the GWT RPC methods, and I'm using Guice, Gin, and RoboGuice on their respective target platforms to inject the appropriate asynchronous service instances for the instantiated Game object.

In the core/pom.xml for a PlayN project, I include the following dependency coordinates to support DI from Gin/Guice/RoboGuice as needed:

<dependency>
  <groupId>javax.inject</groupId>
  <artifactId>javax.inject</artifactId>
  <version>1</version>
</dependency>

Then I add @Inject annotations to any fields inside of the concrete Game implementation:

public class TestGame implements Game {

    @Inject
    TestServiceAsync _testService;

    ...

}

In the html/pom.xml, I include the dependency coordinates for Gin:

<dependency>
  <groupId>com.google.gwt.inject</groupId>
  <artifactId>gin</artifactId>
  <version>1.5.0</version>
</dependency>

And I create TestGameGinjector and TestGameModule classes:

TestGameGinjector.java

@GinModules(TestGameModule.class)
public interface TestGameGinjector extends Ginjector {
    TestGame getGame();
}

TestGameModule.java

public class TestGameModule extends AbstractGinModule {
    @Override
    protected void configure() {
    }
}

Since at the moment, I'm only injecting the TestServiceAsync interface, I don't need to put any implementation in the TestGameModule.configure() method; Gin manages instantiation of AsyncServices for me via GWT.create().

I then added the following to TestGame.gwt.xml

<inherits name='com.google.gwt.inject.Inject'/>

And finally, I made the following changes to TestGameHtml.java

public class TestGameHtml extends HtmlGame {

    private final TestGameGinjector _injector = GWT.create(TestGameGinjector.class);

    @Override
    public void start() {
        HtmlPlatform platform = HtmlPlatform.register();
        platform.assetManager().setPathPrefix("test/");
        PlayN.run(_injector.getGame());
    }
}

And this pretty much covers the HTML5 platform for PlayN.

For the Java platform, I add the following dependency coordinates to java/pom.xml:

<dependency>
  <groupId>com.gdevelop.gwt.syncrpc</groupId>
  <artifactId>gwt-syncproxy</artifactId>
  <version>0.4-SNAPSHOT</version>
</dependency>

<dependency>
  <groupId>com.google.inject</groupId>
  <artifactId>guice</artifactId>
  <version>3.0-rc2</version>
</dependency>

Do note that the gwt-syncproxy project on Google Code does not contain a pom.xml. I have a mavenized version of gwt-syncproxy forked and available via git at https://bitbucket.org/hatboyzero/gwt-syncproxy.git. You should be able to clone it, run mvn clean package install to get it into your local Maven repository.

Anyways, I created a TestGameModule.java for the Java platform as follows:

public class TestGameModule extends AbstractModule {

    @Override
    protected void configure() {
        bind(TestServiceAsync.class).toProvider(TestServiceProvider.class);
    }

    public static class TestServiceProvider implements Provider<TestServiceAsync> {
        public TestServiceAsync get() {
            return (TestServiceAsync) SyncProxy.newProxyInstance(
                TestServiceAsync.class,
                Deployment.gwtWebPath(),  // URL to webapp -- http://127.0.0.1:8888/testgame
                "test"
            );
        }
    }
}

And I modified TestGameJava.java as follows:

public class TestGameJava {

    public static void main(String[] args) {
        Injector _injector = Guice.createInjector(new TestGameModule());

        JavaPlatform platform = JavaPlatform.register();
        platform.assetManager().setPathPrefix("test/images");
        PlayN.run(_injector.getInstance(TestGame.class));
    }
}

I went through a similar exercise with the Android platform and RoboGuice -- without going into tremendous detail, the relevant changes/snippets are as follows:

pom.xml dependencies

<dependency>
  <groupId>com.gdevelop.gwt.syncrpc</groupId>
  <artifactId>gwt-syncproxy</artifactId>
  <version>0.4-SNAPSHOT</version>
</dependency>

<dependency>
  <groupId>org.roboguice</groupId>
  <artifactId>roboguice</artifactId>
  <version>1.1.2</version>
</dependency>

<dependency>
  <groupId>com.google.inject</groupId>
  <artifactId>guice</artifactId>
  <version>3.0-rc2</version>
  <classifier>no_aop</classifier>
</dependency>

TestGameApplication.java

public class TestGameApplication extends RoboApplication {
    @Override
    protected void addApplicationModules(List<Module> modules) {
        modules.add(new TestGameModule());
    }
}

TestGameModule.java

public class TestGameModule extends AbstractModule {

    @Override
    protected void configure() {
        bind(TestServiceAsync.class).toProvider(TestServiceProvider.class);
    }

    public static class TestServiceProvider implements Provider<TestServiceAsync> {
        public TestServiceAsync get() {
            return (TestServiceAsync) SyncProxy.newProxyInstance(
                TestServiceAsync.class,
                Deployment.gwtWebPath(),  // URL to webapp -- http://127.0.0.1:8888/testgame
                "test"
            );
        }
    }
}

TestGameActivity.java

public class TestGameActivity extends GameActivity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
    final Injector injector = ((RoboApplication) getApplication()).getInjector();
        injector.injectMembers(this);
        super.onCreate(savedInstanceState);
    }

    @Override
    public void main(){
        platform().assetManager().setPathPrefix("test/images");
        final Injector injector = ((RoboApplication) getApplication()).getInjector();
        PlayN.run(injector.getInstance(TestGame.class));
    }
}

That's a quick and dirty rundown of how I got Gin/Guice/RoboGuice + GWT working in my project, and I have verified that it works on both Java and HTML platforms beautifully.

Anyways, there's the GWT approach to providing RPC calls to multiple PlayN platforms :).