多人游戏客户端/服务器架构 RMI/JMS/Sockets
我正在用 Java 编写一款回合制纸牌游戏,但需要一些关于使其成为多人游戏的建议。玩家可以创建多人游戏,然后其他人可以加入该游戏,轮流下棋。我花了几个小时研究实现这一点的所有不同方法,但确实需要一些帮助。我将列出要求以及到目前为止我发现的内容:
- 这是一个完全 Java 的游戏,因此使用 RMI 之类的东西不是问题,因为
- 玩家移动,该移动被发送到服务器,服务器发送将此移动到游戏中的其他玩家(客户端)。
- 服务器需要存储所有正在进行的游戏以及其中的玩家(目前使用
HashMap
来实现这一点。
到目前为止,我一直在尝试套接字和 RMI,看起来:
RMI:
+
处理多线程并访问哈希图
-
要么必须轮询服务器以查看玩家是否已移动,要么使用不移动的回调无法通过防火墙
套接字:
+
允许异步回调(?)
-
无法轻松地从生成的多个线程访问 hashmap (?)
-
比 RMI 更复杂
我也一直在研究 JMS、JINI/JavaSpaces、JGroups 和其他我能找到的东西,但我不知道哪一个最有效。让 RMI 的客户端->服务器部分启动并运行,但服务器->客户端似乎是不可能的,因为轮询效率如此低且不可扩展,
感谢你的任何建议
我真的很
编辑:我已经发现了 ConcurrentHashMap。我认为这解决了我的问题之一。
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 inefficient 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
看一下 ZeroMQ。它抽象了套接字的大部分开销,并且还允许同步和异步消息。将其视为 RMI 和 Sockets 之间的中间地带。
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.
第二人生的创建者在线发布了他们的消息基础设施讨论结果,其中包含一系列消息传递解决方案以及对其优缺点的评论:
消息队列评估说明(第二人生 Wiki)
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)
您是否考虑过Hazelcast?这支持分布式集合和其他数据结构。对于一款游戏来说,我确信它足够可靠。
■分布式java.util.{Queue, Set, List, Map}
分布式java.util.concurrency.locks.Lock
分布式java.util.concurrent.ExecutorService
用于一对多映射的分布式MultiMap
用于发布/订阅消息的分布式主题
■分布式索引和查询支持
■通过JCA进行事务支持和J2EE容器集成
■用于安全集群的套接字级加密
■映射的直写式和后写式持久性
■Java客户端用于远程访问集群
动态HTTP会话集群
支持集群信息和成员资格事件
■动态发现
■动态缩放
■带备份的动态分区
■动态故障转移
■基于Web的集群监控工具
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