如何像很多游戏一样快速发送信息?
我的想法就像《Counter Sstrike》、《WoW》等游戏所使用的方法一样。在CS中,你经常有50个ping,有没有办法以这样的速度将信息发送到在线MySQL数据库?
目前,我正在使用程序请求的在线 PHP 脚本,但这确实很慢,因为程序首先必须向其发送标头和后信息,然后将结果作为普通网页检索。
真的必须有更简单、更快的方法来做到这一点吗?我听说过 TCP/IP,这是我应该在这里使用的吗?它是否有可能以比通过 PHP 脚本间接连接更快的方式连接到数据库?
I'm thinking like the methods games like Counter Sstrike, WoW etc uses. In CS you often have just like 50 ping, is there any way to send information to an online MySQL database at that speed?
Currently I'm using an online PHP script which my program requests, but this is really slow, because the program first has to send headers and post-information to it, and then retrieve the result as an ordinary webpage.
There really have to be any easier, faster way of doing this? I've heard about TCP/IP, is this what I should use here? Is it possible for it to connect to the database in a faster way than indirectly via the PHP script?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
TCP/IP 由三种协议组成:
ICMP 是您在网络上 ping 另一台计算机时使用的协议。
像《反恐精英》这样的游戏并不关心你之前做过什么。因此,不需要完整性,能够重建你所做的事情(这就是为什么竞争对手必须记录他们正在做的事情的原因)。这就是 UDP 的用途 - 无法保证数据已发送或接收。这就是为什么延迟会成为一个问题——你已经死了,只是你不知道而已。
TCP 保证数据的发送和接收。比 UDP 慢。
为了获得快速连接,需要注意很多事情——更少的跳数等。
TCP/IP is made up of three protocols:
ICMP is what you are using when you ping another computer on a network.
Games, like CounterStrike, don't care about what you previously did. So there's no requirement for completeness, to be able to reconstruct what you did (which is why competitors have to tape what they are doing). This is what UDP is used for - there's no guarantee that data is delivered or received. Which is why lag can be such a problem - you're already dead, you just didn't know it.
TCP guarantees that data is sent and received. Slower than UDP.
There are numerous things to be aware of to have a fast connection - less hops, etc.
客户端到服务器对于延迟关键的东西?使用非阻塞 UDP。
对于可能慢一点的可靠内容,如果您使用 TCP,请确保以非阻塞方式(select()、非阻塞发送等)进行操作。
使用 UDP 的一个重要原因是,如果您有时间敏感的数据 - 如果生物的位置丢失,您最好忽略它并发送下一个位置数据包,而不是重新发送上一个位置数据包。
我不认为任何高性能游戏的每个调用都会解析为对数据库的调用。更常见的是(如果甚至使用数据库)偶尔或在重要事件中保留数据。
你不会在 http 之上实现 Counterstrike 或类似的东西。
Client-to-server for latency-critical stuff? Use non-blocking UDP.
For reliable stuff that can be a little slower, if you use TCP make sure you do so in a non-blocking fashion (select(), non-blocking send, etc.).
The big reason to use UDP is if you have time-sensitive data - if the position of a critter gets dropped, you're better off ignoring it and sending the next position packet rather than re-sending the last one.
And I don't think any high-performance game has each and every call resolve to a call to the database. It's more common to (if a database is even used) persist data occasionally, or at important events.
You're not going to implement Counterstrike or anything similar on top of http.
大多数游戏(如您引用的游戏)都使用 UDP(TCP/IP 协议套件之一)。对于此应用程序,选择 UDP 而不是 TCP,因为它重量更轻,可以实现更好的性能,并且不需要 TCP 的可靠性功能。
但请记住,这些游戏具有通常用 C 或 C++ 编写的独立客户端和服务器。如果您的应用程序是基于浏览器的,并且您尝试通过 HTTP 执行此操作,则使用长期连接并尽可能删除标头,包括 cookie。您可能会对 Tornado 框架感兴趣。您可能还想研究 HTML5 WebSockets,但是距离广泛支持仍然有很长的路要走。
如果您的目标是基于浏览器的插件,例如 Flash、Java、SilverLight,那么您也许可以使用 UDP,但我对这些平台了解不够,无法确认。
编辑:
还值得一提的是:一旦您的网络代码和协议得到充分优化您仍然可以采取一些措施来改善高 ping 值玩家的体验。
Most games like the ones you cite use UDP for this (one of the TCP/IP suite of protocols.) UDP is chosen over TCP for this application since it's lighter weight allowing for better performance and TCP's reliability features aren't necessary.
Keep in mind though, those games have standalone clients and servers usually written in C or C++. If your application is browser-based and you're trying to do this over HTTP then use a long-lived connection and strip back the headers as much as possible, including cookies. The Tornado framework may be of interest to you there. You may also want to look into HTML5 WebSockets however widespread support is still a fair way off.
If you are targeting a browser-based plugin like Flash, Java, SilverLight then you may be able to use UDP but I don't know enough about those platforms to confirm.
Edit:
Also worth mentioning: once your networking code and protocol is sufficiently optimized there are still things you can do to improve the experience for players with high pings.