我即将开始一个游戏项目。它将是一款采用客户端-服务器架构的网络游戏,配有 Java 服务器以及 Web 和 Android 客户端。影响在于低带宽(用于移动设备)和快速响应。有哪些技术/库可用于客户端-服务器通信?我在 Web 应用程序(GWT/Vaadin 和 servlet)方面有一定的经验,但不知道在实现游戏服务器(主要用于通信)时使用什么。
我知道我可以使用带有对象序列化的 Java 套接字或 JSON 将数据从客户端传递到服务器,但我不知道这些方法在带宽方面的效率如何?或者还有比这些更适合的吗?只要为我指明正确的方向就足够了。
提前致谢!
I'm about to start a game project. It will be a network game with client-server architecture with Java server and a Web and Android clients. Impact is on low bandwidth (for use on mobile devices) and fast response. What technologies / libraries are out there for client-server communication? I am somehow experienced in web applications (GWT/Vaadin and servlets) but have no clue what to use when implementing a game server, mainly for the communication.
I am aware that I could use Java sockets with Object serialization or maybe JSON to pass the data from client to server, but I don't know how efficient in terms of bandwidth these approaches are? Or are there any more suited than these? Just pointing me in the right direction will suffice.
Thanks in advance!
发布评论
评论(3)
就 Android 客户端库而言,有两个主要选项。第一个是 java.net.* 包,您将在其中主要使用 HttpUrlConnection。
更好的选择是使用 Apache HTTP 包也成为 Android SDK 的标准配置。它在处理网络连接时为您提供了更多的控制/灵活性/详细性。
这是一个关于如何使用 Apache 客户端库的示例。我建议使用这些,因为 java.net 包实际上只适合最基本的 GET 请求。
我建议使用 JSON 方法,因为这样您就不必编写 Java servlet 后端来反序列化 Java 对象。然后后端可以独立于客户端进行更改。
As far as Android client libraries go, there's two main options. The first is just the java.net.* package in which you'll mainly use HttpUrlConnection.
The better option is to use the Apache HTTP package that also comes standard in the Android SDK. It gives you a lot more control / flexibility / verbosity in dealing with network connections.
Here is a decent example of how to use the Apache client libraries. I suggest using these, as the java.net packages are really only suited for the most basic of GET requests.
I suggest using the JSON method, because then you're not stuck having to write a Java servlet backend to deserialize the Java objects. The backend can then change independently of the client.
主要问题是,如果你使用WebTechnologies,那么你无法实现一种有效的方法来实现从服务器到客户端的通信...
客户端到服务器通过HTTP GET工作正常,但你必须使用某种Comet来实现从服务器到客户端的通信。
这就是为什么通过套接字的普通旧 tcp 连接可能更好地与客户端通信,尤其是与 Android 客户端通信。
但是用 tcp 套接字实现它在 Android 上工作得很好,但在浏览器上却不行。
我的解决方案:通过WebSocket实现通信。
WebSockets(HTML5 的一部分)是一种 HTTP 扩展,可实现客户端和服务器之间的全双工通信。
大多数主流网络浏览器都支持 WebSocket,例如 Firefox 4、Chrome 9、Opera 10.7
但不是 Internet Explorer(计划在 IE 10 中支持,与 Windows 8 一起提供)
对于 Android,也存在 Java 库来实现通信,我希望它们也能与 Android 很好地配合。
示例:
http://code.google.com/p/weberknecht/
对于服务器端:Servlet API 3.0 支持这一点,例如 Jetty 8
在我看来,WebSocket 可以解决这个问题,因此您可以实现支持 WebSocket 的单个服务器,并通过 WebSocket 与 Android 客户端以及浏览器客户端(Internet Explorer 除外)进行通信
The main problem is, if you use WebTechnologies, than you can't implement an efficient way to realize the communication from Server to Client ...
Client to Server over HTTP GET works fine, but you must use some kind of Comet to realise the communication from server to client.
Thats why plain old tcp connection over socket would may be the better to communicate with a Client, especially with Android Client.
But implementing it with tcp socket work great with android, but not with a Browser.
My solution: implement the communication via WebSocket.
WebSockets (part of HTML5) is a HTTP extension to enable full duplex communication between client and server.
The most major web browser support WebSocket, like Firefox 4, Chrome 9, Opera 10.7
BUT NOT the Internet Explorer (support planned in IE 10, comming with Windows 8)
And for android, there exists also java libaries to implement the communication and i would excpect, that they work also well with android.
Expample:
http://code.google.com/p/weberknecht/
For the server side: Servlet API 3.0 support that, like Jetty 8
In my opinion WebSocket would save the problem, so you can implement a single server which supports WebSocket and communicate via WebSocket to an Android client as well as with a Browser Client (except Internet Explorer)
这取决于您的项目的时间范围。如果时间足够,请尝试并比较与您的目的相关的技术。
It depends on timeframe of your project. If time is enough – try in your hands and compare the technologies related to your purposes.