小程序-> servlet->另一个小程序通讯
我正在实现一个简单的西洋跳棋游戏(Java 小程序作为客户端 + servlet),其中两个玩家可以连接到 servlet 并玩游戏。由于我刚刚开始进行 applet-servlet 编程,因此在正确的通信方面遇到了一些问题。
假设游戏开始并且第一个玩家移动。然后我必须将他的动作发送到 servlet,该 servlet 应该通知其他玩家。在这里我不知道该怎么做。 servlet 如何将接收到的数据传递给另一个小程序,而不是发送数据的小程序? servlet 应该如何以及在哪里保存与玩家相关的信息,以便它能够识别哪一个发送了数据?
I'm implementing a simple checkers game (java applets as clients + servlet) where two players can connect to the servlet and play. As I'm just beginning with applet-servlet programming, I have some problems with the proper communication.
Let's suppose the game begins and first player moves. Then I have to send his move to the servlet which should inform the other player. And here I don't know how to do that. How the servlet can pass received data to another applet, not the one that sent it? How and where should the servlet keep the info related to players so that it can recognize which one sent the data?
首先:Servlet 不会主动向任何 Servlet 传递数据或消息。
它必须等待小程序发送请求,然后才能发送正确的响应。
因此,最简单的情况似乎是您的小程序定期向 servlet 发送请求,例如“怎么了?”。这是我正在谈论的投票系统。
要识别哪个玩家正在轮询,请在他们第一次访问您的 servlet 时给他们一个 id(注册?),并让该 id 出现在以后的每个请求中。
这够清楚了吧? :)
(是的,下一步是使用 COMET)
Fisrt: the servlet won't pass data or message to any servlet from its initiative.
It will have to wait for the applet to send a request, and then will be able to send a proper response.
So, the simplest seems that your applets regularly send requests to the servlet, like "what's up?". It's the polling system ome is talking about.
To recognize which player is polling, give them an id when they first access your servlet (register ?), and make that id present in every future request.
Is that clear enough ? :)
(and yes, next step is to use COMET)