游戏联网常用的方法有哪些?
所以我正在编写一个相当简单的游戏,网络要求非常低,我使用 TCP。
我不确定从哪里开始甚至定义/实现客户端和服务器使用的协议。我环顾四周,看到了一些例子,例如 Mojang 的 Minecraft,它使用一个“命令”表,客户端发送到服务器,服务器发送给客户端,并带有许多参数等。
有什么好的方法可以做到这一点?我听说过有关 Minecraft 协议的抱怨,因为如果你过度读取一个字节,就会破坏整个流。
So I'm writing a fairly simple game with very low networking requirements, I'm using TCP.
I'm unsure where to start in even defining/implementing a protocol for the client and server to use. I've been looking around and I've seen a few examples, for instance Mojang's Minecraft which uses a table of 'commands' the client sends the server and the server sends the client, with numbers of arguments and such.
What's a good way to do this? I've heard complaints about Minecraft's protocol because if you overread by a byte you ruin the entire stream.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
游戏网络是一个广泛的问题,具体取决于您要解决的问题类型。 TCP(可能)甚至不是您的正确选择。
例如,发送角色移动的游戏通常是使用 UDP 完成的。原因是角色移动对于游戏的运行并不重要,因此移动的一些数据丢失是“可以接受的”。这可能就是为什么有时你的角色会“跳跃”——一些 UDP 数据包丢失,或者严重乱序。
UDP 被认为是网络游戏的首选协议。因此,在开始之前,请仔细考虑您是否选择了正确的协议。
总的来说,我认为Glenn Fiedler 开发网络游戏的系列是一本很棒的读物。我会从这里开始。他涵盖了使用 UDP 进行游戏的所有基础知识。
如果您只是想使用 TCP 来获取 TCP 的句柄 - 那么 Minecraft 是一个合理的例子。可以来回发送的已知命令列表是一种简单的开始方法。但是,正如您所说,很容易出现一些问题。这更多是因为使用了错误的协议,而不是协议的开发方式。
Game networking is a broad question, depending on what type of problem you are solving. TCP (may) not even be the correct choice for you.
For example - games that send movement of characters is typically done with UDP. The reason being that character movement isn't critical to the operation of the game, so some data loss of movement is "acceptable". That may be why sometimes your character "jumps" - some UDP packets were lost, or severely out-of-order.
UDP is argued as the preferred protocol for networked games. So before you even get started, carefully consider whether you are even picking the correct protocol.
Overall, I consider Glenn Fiedler's series on developing a networked game a fantastic read. I'd start here. He covers all of the basics of using UDP for gaming.
If you want to use TCP simply just to get a handle on TCP - then Minecraft is a reasonable example. A known list of commands that can be sent back and forth is a simple way to start. However, as you stated, is prone to some problems. This is more aligned with using the wrong protocol than how it was developed.
谷歌“游戏网络库”,你会得到一堆结果。 GNE 是一个值得一看的好地方。
Google "game networking library" and you'll get a bunch of results. GNE would be a good one to look at.
我想这取决于你的游戏是什么,它的机制是什么,需要什么信息。无论如何,我认为这个堆栈交换 https://gamedev.stackexchange.com/ 更适合回答您的问题。
I guess it depends on what your game is, what it mechanics are, what information is necessary. In any case I think this stack exchange https://gamedev.stackexchange.com/ is more suited to answer your question.
Gamedev.net 的网络论坛有一个很棒的常见问题解答,涵盖了此类问题和许多其他问题,但是,为了使其不仅仅是“去那里看看”答案,我将建议您可以进行一些小的改进。使用 tcp 时,传输是有保证的,但这会带来速度成本,如果您不制作 fps,这很好,但这意味着您需要从发送的数据中获取更多信息,一个很好的方法是通过 deltas/差异,即仅发送状态变化,而不是整个游戏状态,您还可以通过预测可能性是否允许来验证传入数据包是否存在 TCP 检查上的损坏/异常数据,并且通过相同的预测,您可以删除更多数据等。但正如其他人所说,这是一个广泛的问题,不适合获得真正有用的答案
Gamedev.net's networking forum has a great FAQ covering these sorts of questions and many others, however, to make this more than a 'go-there-look-at-that' answer, I'll suggest some small improvements you can make. When using tcp, delivery is guarenteed, but this has a speed cost, which is fine if your not making a fps, but it means you need to get more from the data you do send, a great way to do this is via deltas/differentials, that is, sending only the change in state, not the entire game state, you can also validate your incoming packets for corrupt/anomalys data over and about tcp checks by predicting possibilities are allow, and with the same prediction, you can cut out even more data etc. But as others have said, this is a broad question, and not suited to getting truely helpful answers
当您使用 lua 进行编码时,任何人使用的唯一库是 luasocket (尽管 ZMQ 正在取得进展)。
您确实需要使用多种协议:TCP 用于必须接收的数据(例如,诸如changemap 或you_got_kicked 之类的服务器命令、对话等;然后使用UDP 来用于非强制数据或很快过期的数据(例如,字符位置)。
As you're coding in lua, the only library anyone uses is luasocket (though ZMQ is gaining ground).
You're really going to have several protocols going: TCP for data that must be received (eg, server commands such as changemap or you_got_kicked, conversations and such; then use UDP for non-compulsory data, or data that quickly expires (eg, character positions).