Java MMO 游戏数据传输

发布于 2025-01-07 18:16:39 字数 264 浏览 2 评论 0原文

我正在尝试编写一个基于图块的小型在线游戏。我已经使用套接字建立了游戏服务器连接,因此我可以轻松地将字符串从客户端发送到服务器,反之亦然。

我不明白的是,我应该以什么形式存储数据才能有效地传输数据?我应该使用数据对象、序列化并发送它吗?我应该将数据存储在一个长字符串中吗?或者还有别的办法吗?

如果我要发送什么样的数据很重要,那么它将是有关其他玩家、地图对象等的信息。

我已经浏览了几天,但仍然没有结果(可能是因为我不确定这个问题如何称呼)。

谢谢 !

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

不念旧人 2025-01-14 18:16:39

这里最重要的是:这是一个回合制游戏吗?

对于回合制游戏,您可以仅使用 TCP 套接字并通过套接字传输完整的游戏状态(或基于前一个游戏状态的增量)。它的编码方式并不重要。

对于实时游戏,尝试尽可能频繁地更新状态非常重要。数据包是否丢失并不重要;也许另一个数据包仍在传输中,它会覆盖前一个数据包。这就是为什么您应该使用 UDP 套接字而不是 TCP。

考虑到您正在谈论基于图块的游戏,请考虑一下您需要向客户传达哪些信息。也许只有玩家在移动,而其他部分是静止的?

这是一个很难给出单一结论性答案的问题。有很多设计决策需要考虑,涉及主游戏模拟循环的设计、作弊、网络架构测试等。请参阅https://developer.valvesoftware.com/wiki/Source_Multiplayer_Networking 查看您所面临问题的小示例。

我可以给你的最简单的解决方案是:

  • 使用 TCP 套接字,这样你就不必处理数据包丢失。
  • 当服务器上的游戏状态发生变化时,将状态传播到所有客户端。在不占用太多空间的情况下执行此操作。一个示例是传送小数据包,如下ENTITY NUMBER - NEW POSITION。 (即两个32位整数)。当您需要一次传送多个更新时,请确保它们包含在同一个 TCP 数据包中,并且 TCP 发送缓冲区在两端立即刷新。
  • 忘记在与往返 (ping) 时间 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:

  • Use TCP sockets so you do not have to handle packet loss.
  • When the game state changes on the server, propagate the state to all clients. Do this in something which does not take a lot of room. An example would be to communicate small packets as follows 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.
  • Forget all the timing issues you will face when dealing with clients with a round-trip (ping) time of 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.
野稚 2025-01-14 18:16:39

这取决于你。您可以使用 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文