Java applet communicate with servlet on tomcat server
I've been trying to work out how to approach this problem but I don't know where to start.
I have an game applet written in Java that I'm trying to make multi-player across a network. At the moment it is a standalone application but eventually it needs to be on some sort of web page where clients can access it through a browser and play against each other.
I was thinking that each clients applet could communicate using sockets with an applet that runs constantly on a tomcat server. This applet on the server would handle incoming socket connections, create new threads to handle each connection, maintain a list of connected clients, and set up games. Clients could also get information from a database about past games etc. by communicating with the server.
For example I want a client to be able to start a game. Then other clients are notified that he has started a game and can join the game. Then when the host clicks start game, all the other clients are notified and the game begins in their applet. Whoever solves the puzzle first would then click finished. His score would be sent to the server applet which then forwards the result to each client connected.
So i have some questions
Does this sound like a reasonable plan?
How does tomcat assist with this?
How would i deploy it to tomcat? I'm doing this in eclipse and have set added a local tomcat server. Do i just run the class on the server with eclipse, then socket connect to http://localhost/packagename/classname on the applet?
What classes would the servlet need to extend to make it connectable? Since connections will be made from an applet should the servlet be a Non-Http Servlet?
Can you recommend some documentation or provide some example code of a client applet communicating with a server applet using tomcat? All the examples I've seen are just html forms that pass information to a servlet using Http.
If your application doesn't need real-time communication with a server over a raw socket (and it sounds like yours does not), you might be better off using standard web protocols for your applet to communicate with the server.
One choice might be HTTP/JSON. Your applet could make an HTTP connection to a servlet, and that servlet will generate a JSON object that represents a message the server wants to send to the client. The client will start a thread that will loop, making an asynchronous blocking call to your servlet to poll for new data.
The major advantage of this approach, is that in the future, should you be so inclined, you can ditch the client-side java applet, and replace it with HTML5 and Javascript.
GSON is a library to use for JSON serialization/deserialization. And java.net.URL
is what you would use to connect to the servlet inside your applet.
Yes. What you are describing is applet-servlet communication. Typically, your applet will send messages to the servlet which will then keep track of communicating with all the other client applets.
There are several examples of applet-servlet communications online. Here is one -- old, but still valid and the code is not formatted.
http://docstore.mik.ua/orelly/java-ent/servlet/ch10_01.htm
There are security restrictions around applets and the servers they can communicate to, so thats something else to keep in mind.
链接地址: http://www.djcxy.com/p/55938.html上一篇: > servlet