Java MMO 游戏数据传输
我正在尝试编写一个基于图块的小型在线游戏。我已经使用套接字建立了游戏服务器连接,因此我可以轻松地将字符串从客户端发送到服务器,反之亦然。
我不明白的是,我应该以什么形式存储数据才能有效地传输数据?我应该使用数据对象、序列化并发送它吗?我应该将数据存储在一个长字符串中吗?或者还有别的办法吗?
如果我要发送什么样的数据很重要,那么它将是有关其他玩家、地图对象等的信息。
我已经浏览了几天,但仍然没有结果(可能是因为我不确定这个问题如何称呼)。
谢谢 !
I'm trying to write a small online tile-based game. I've established a game - server connection with sockets, so I can easily send Strings from client to server and vice versa.
What I don't understand, is in what form should I store the data to be able to transfer it efficiently? Should I use an object of data, serialize and send it through? Should I store data in one long string? Or is there another way?
If it matters what kind of data I will be sending, it will be information about other players, map objects and etc.
I've been browsing for a couple of days now, and still no results (probably it's because I'm not sure how this issue is called ).
Thank you !
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这里最重要的是:这是一个回合制游戏吗?
对于回合制游戏,您可以仅使用 TCP 套接字并通过套接字传输完整的游戏状态(或基于前一个游戏状态的增量)。它的编码方式并不重要。
对于实时游戏,尝试尽可能频繁地更新状态非常重要。数据包是否丢失并不重要;也许另一个数据包仍在传输中,它会覆盖前一个数据包。这就是为什么您应该使用 UDP 套接字而不是 TCP。
考虑到您正在谈论基于图块的游戏,请考虑一下您需要向客户传达哪些信息。也许只有玩家在移动,而其他部分是静止的?
这是一个很难给出单一结论性答案的问题。有很多设计决策需要考虑,涉及主游戏模拟循环的设计、作弊、网络架构测试等。请参阅https://developer.valvesoftware.com/wiki/Source_Multiplayer_Networking 查看您所面临问题的小示例。
我可以给你的最简单的解决方案是:
ENTITY NUMBER - NEW POSITION
。 (即两个32位整数)。当您需要一次传送多个更新时,请确保它们包含在同一个 TCP 数据包中,并且 TCP 发送缓冲区在两端立即刷新。400ms
及以上的客户端打交道时将面临的所有计时问题。如果需要,您可以像这样转换数据包:ENTITY NUMBER - NEW POSITION - CURRENT SPEED VECTOR
,这将允许客户端估计实体位置,包括滞后延迟。The thing that is most important here is this: is it a turn-based game?
For turn-based games, you can just use TCP sockets and transfer the full game-state (or a delta based on the previous gamestate) over the socket. Does not really matter how it is encoded even.
For realtime games, it is important that you try to update the state as often as possible. It is not important if a packet is lost; maybe another packet is still in transit which overrides the previous packet. This is why you should use UDP sockets instead of TCP.
Considering you are talking about tile-based games, think about what information you need to communicate to your clients. Maybe only the players move, while the rest is static?
This is a difficult issue that does not allow a single conclusive answer. There are a lot of design decisions to take into account, touching the design of the main game simulation loop, cheating, network architecture tests, etc. See https://developer.valvesoftware.com/wiki/Source_Multiplayer_Networking for a small example of the issues you face.
The most simple solution I can give you is:
ENTITY NUMBER - NEW POSITION
. (i.e. two 32-bit integer). When you need to communicate multiple updates at once, make sure they are contained in the same TCP packet, and that the TCP send buffer is immediately flushed on both ends.400ms
and beyond. If required, you can transform the packet like this:ENTITY NUMBER - NEW POSITION - CURRENT SPEED VECTOR
which will allow the client to estimate the entity position including lag delay.这取决于你。您可以使用 String、序列化或将数据打包到 ByteBuffer 并通过网络发送。选择最适合您的方式。但我认为 ByteBuffer 是最有效的。
我推荐 Netty 库,因为它大大简化了套接字处理。
This is up to you. You can use String, serialization or pack the data to a ByteBuffer and send it over the network. Choose the way that suits you most. But I suppose ByteBuffer is the most efficient for that.
I recommend Netty library because it considerably simplifies sockets handling.