Multiplayer game client/server architecture RMI/JMS/Sockets
I'm in the process of writing a turn-based card game in Java but need some advice on making it multiplayer. Players can create a multiplayer game and then others can join that game, taking turns to play their move. I've spent hours looking into all the different ways to implement this but could really do with some help. I'll list the requirements and what I've found out so far:
- It is an entirely Java game so using something like RMI is not an issue for that reason
- Player makes a move, this move is sent to the server, the server sends this move to the other players (clients) in the game.
- The server needs to store all the games in progress and the players in them (currently doing this with a
HashMap<UniqueGameID, GameObject>
.
I've been experimenting with sockets and RMI so far and it seems:
RMI:
+
Handles multithreading and access to the hashmap
-
Either have to poll the server to see if a player has moved or use callbacks which don't work through firewalls
Sockets:
+
Allows for asynchronous callbacks(?)
-
Can't easily access hashmap from the multiple threads that are spawned(?)
-
More complicated than RMI
I've also been looking into JMS, JINI/JavaSpaces, JGroups and anything else that I can find but I have no idea which one will work best. I've got the client->server part of RMI up and running but server->client seems out of the question as polling is so i开发者_StackOverflow社区nefficient and unscalable
I'd really appreciate any advice you have.
Many thanks
EDIT: I have since discovered ConcurrentHashMap which I think solves one of my problems.
Take a look at ZeroMQ. It abstracts much of the overhead you'd have with sockets and also allows synchronous an asynchronous messages. Think of it as a middleground between RMI and Sockets.
The creators of Second Life have published the results of their message infrastructure discussion online, which contains a list of messaging solutions with comments about their pros and cons:
Message Queue Evaluation Notes (Second Life Wiki)
Have you considered Hazelcast? This supports distributed collections and other data structures. For a game I am sure its reliable enough.
■Distributed java.util.{Queue, Set, List, Map} ■Distributed java.util.concurrency.locks.Lock ■Distributed java.util.concurrent.ExecutorService
■Distributed MultiMap for one to many mapping ■Distributed Topic for publish/subscribe messaging ■Distributed Indexing and Query support ■Transaction support and J2EE container integration via JCA
■Socket level encryption for secure clusters ■Write-Through and Write-Behind persistence for maps ■Java Client for accessing the cluster remotely ■Dynamic HTTP session clustering
■Support for cluster info and membership events ■Dynamic discovery ■Dynamic scaling ■Dynamic partitioning with backups ■Dynamic fail-over ■Web-based cluster monitoring tool
精彩评论