Java 客户端/服务器回合制帮助
我正在尝试制作客户端/服务器回合制游戏。我希望这是一款 2 人游戏。我将使用 Java 小程序作为客户端,以便人们可以通过浏览器在线玩游戏。
到目前为止我所知道的是,我可以创建一个接受所有传入连接的服务器,并创建一个线程来处理连接的客户端。我还可以编写连接到服务器的客户端。我不知道如何让两个单独的客户端相互交互。
小程序无法相互通信,因此必须通过服务器/线程完成通信(我假设)。
我对Java并不陌生,但之前从未接触过任何网络。有人可以帮我吗?
I am trying to make a client/server turn based game. I want this to be a 2 player game. I will be using a Java applet as the client so that people can play online through a browser.
What I know so far is that I can create a server that accepts all incoming connections and create a thread to handle clients that connect. I can also write the client that will connect to the server. What I don't know is how to get two separate clients to interact with each other.
Applets cannot talk to each other so communication must be done via the server/threads (I assume).
I am not new to Java, but I have never done any networking before. Can someone help me out?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在我看来,处理此类回合制游戏的最佳策略是决定一些基本的架构方法。绘制出组件和一些基本的游戏流程图。
您应该将大部分游戏引擎逻辑放在服务器组件中。客户端应尽可能保持精简,主要关注
您的服务器/游戏引擎应该是相对无状态的,但维护当前正在运行的游戏会话的列表。有状态 SOAP Web 服务甚至 HTTP Servlet 都是不错的选择,因为它们通过在请求中放置和读取会话 cookie 来为您维护会话。
所有 Web 内容都基于请求响应,因此本质上是无状态的,但某些技术(例如 Java servlet)将帮助您维护会话,这样您就不必这样做。不需要物理上创建单独的线程,每个请求都会导致应用程序服务器生成一个新的执行线程,而会话本质上是易失性的。
在服务器端,我将保留会话中特定活动游戏的所有数据。这样,你的游戏引擎就会维持两个玩家之间的有序沟通。
In my opinion the best strategy to approach a turn based game such as this, is to decide on some basic architectural approaches. Diagram out the componenents and some basic game flow diagrams.
You should put the bulk of the game engine logic in the server component. The clients should be kept as thin as possible, focusing primarily on
Your server/game engine should be relatively stateless, yet maintain a list of current game sessions in play. Stateful SOAP web services or even HTTP Servlets would be a good choice because they maintain session for you by placing and reading session cookies in the request.
Everything web works on request response so it is by nature stateless, but certain technologies like Java servlets will help you maintain sessions so that you don't have to. No need for physically creating seperate threads, each request causes the application server to spawn a new thread of execution, while the session by nature is volatile.
On the server side I would persist all data for a particular active game in the session. In this way, your game engine will maintain the orderly communication between the two players.
您只需使用服务器作为中间人。
You just use the server as a middle-man.