我如何通过Python中的套接字保持不断更新的数据?
我有一个客户端和一个服务器,都是用 Python 2.7 编写的。
假设我想制作一个多人游戏服务器(我目前还没有,但我正在努力实现)。我需要让服务器(和其他客户端)及时了解我的角色行踪,对吗?
我该如何用套接字做到这一点?仅在需要时发送或请求信息(例如,角色移动,或另一个玩家角色移动,服务器将信息发送给其他客户端),或者我会保持一个恒定的套接字打开,以实时发送每个人的移动数据,无论是否自从最后一条数据发送或未发送以来,他们实际上已经做了一些事情。
我不会在编码方面遇到困难,我只是需要有关如何实际执行它的概念方面的帮助。
I have a client and a server, both written in Python 2.7.
Lets say I wanted to make a multiplayer game server (which I don't at the moment but I'm working towards it). I would need to keep the server up to date (and other clients) on my characters whereabouts, correct?
How would I do this with sockets? Send or request information only when it is needed (e.g the character moves, or another players character moves and the server sends the information to other clients) or would I keep a constant socket open to send data real-time of EVERYBODY's movement regardless of if they have actually done something since the last piece of data was sent or not.
I won't struggle coding it, I just need help with the concept of how I would actually do it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
对于 TCP 套接字,考虑到拆卸和连接,更典型的是保持连接打开。重建成本。
最终,在扩展时,您将查看 NewIO\RawIO。
如果您不这样做,请想象游戏客户端可能会采取步骤并返回如果将其发送到服务器,则不会得到确认其他玩家。
With TCP sockets it is more typical to leave the connections open, given the teardown & rebuild cost.
Eventually when scaling you will do look into NewIO\RawIO.
If you do not, imagine that the game client might take a step & not get confirmation if sending it to the server & other players.
绝对保持套接字打开,但您应该考虑使用类似 ZeroMQ 的东西,它为您提供了更多类型的套接字可供使用。
Definitely keep the socket open, but you should consider using something like ZeroMQ which gives you more kinds of sockets to work with.