Mar*_*mit 9 rpc json client-server gwt-rpc playn
我想用PlayN创建一个客户/服务器卡片游戏,例如Hearts.虽然我主要关注的是HTML5输出,但我理想的是,如果我决定将来制作一个Android客户端,我希望能够与输出平台无关.我该如何处理RPC机制?
这些是我想到的选项:
GWT.create(MyService.class)(或至少是它的包装器)生成的GWT Async接口.我不知道这对非HTML版本有多好.我可以直接从客户端Java代码使用GWT-RPC吗?hat*_*ero 12
对于Java和Android平台上的GWT RPC,我目前正在尝试使用gwt-syncproxy来提供Java客户端对GWT RPC方法的访问,并且我在他们各自的目标平台上使用Guice,Gin和RoboGuice进行注入实例化的Game对象的相应异步服务实例.
在PlayN项目的core/pom.xml中,我根据需要包含以下依赖关系坐标以支持来自Gin/Guice/RoboGuice的DI:
<dependency>
<groupId>javax.inject</groupId>
<artifactId>javax.inject</artifactId>
<version>1</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)
然后我将@Inject注释添加到具体Game实现中的任何字段:
public class TestGame implements Game {
@Inject
TestServiceAsync _testService;
...
}
Run Code Online (Sandbox Code Playgroud)
在html/pom.xml中,我包含了Gin的依赖关系坐标:
<dependency>
<groupId>com.google.gwt.inject</groupId>
<artifactId>gin</artifactId>
<version>1.5.0</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)
我创建了TestGameGinjector和TestGameModule类:
TestGameGinjector.java
@GinModules(TestGameModule.class)
public interface TestGameGinjector extends Ginjector {
TestGame getGame();
}
Run Code Online (Sandbox Code Playgroud)
TestGameModule.java
public class TestGameModule extends AbstractGinModule {
@Override
protected void configure() {
}
}
Run Code Online (Sandbox Code Playgroud)
从目前开始,我只注入了TestServiceAsync接口,我不需要在TestGameModule.configure()方法中添加任何实现; Gin通过GWT.create()为我管理AsyncServices的实例化.
然后我将以下内容添加到TestGame.gwt.xml中
<inherits name='com.google.gwt.inject.Inject'/>
Run Code Online (Sandbox Code Playgroud)
最后,我对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());
}
}
Run Code Online (Sandbox Code Playgroud)
这几乎涵盖了PlayN的HTML5平台.
对于Java平台,我将以下依赖关系坐标添加到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>
Run Code Online (Sandbox Code Playgroud)
请注意,Google Code上的gwt-syncproxy项目不包含pom.xml.我有一个gwt-syncproxy 的mavenized版本,可以通过https://bitbucket.org/hatboyzero/gwt-syncproxy.git上的git获得.您应该能够克隆它,运行mvn clean package install以将其放入本地Maven存储库.
无论如何,我为Java平台创建了一个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"
);
}
}
}
Run Code Online (Sandbox Code Playgroud)
我修改了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));
}
}
Run Code Online (Sandbox Code Playgroud)
我在Android平台和RoboGuice上进行了类似的练习 - 没有详细介绍,相关的更改/片段如下:
pom.xml依赖项
<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>
Run Code Online (Sandbox Code Playgroud)
TestGameApplication.java
public class TestGameApplication extends RoboApplication {
@Override
protected void addApplicationModules(List<Module> modules) {
modules.add(new TestGameModule());
}
}
Run Code Online (Sandbox Code Playgroud)
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"
);
}
}
}
Run Code Online (Sandbox Code Playgroud)
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));
}
}
Run Code Online (Sandbox Code Playgroud)
这是我在项目中如何使Gin/Guice/RoboGuice + GWT工作的快速而简洁的概述,我已经证实它在Java和HTML平台上都能很好地工作.
无论如何,有GWT方法为多个PlayN平台提供RPC调用:).
| 归档时间: |
|
| 查看次数: |
1113 次 |
| 最近记录: |