3

我想像反恐精英,魔兽世界等游戏使用的方法。在 CS 中,您通常只有 50 次 ping,有没有办法以这种速度将信息发送到在线 MySQL 数据库?

目前我正在使用我的程序请求的在线PHP脚本,但这真的很慢,因为程序首先必须向它发送标题和发布信息,然后将结果作为普通网页检索。

真的必须有更简单、更快的方法吗?我听说过 TCP/IP,这是我应该在这里使用的吗?它是否有可能以比间接通过 PHP 脚本更快的方式连接到数据库?

4

3 回答 3

3

延迟关键的东西的客户端到服务器?使用非阻塞 UDP。

对于可能会慢一些的可靠东西,如果您使用 TCP,请确保以非阻塞方式(select()、非阻塞发送等)这样做。

使用 UDP 的一个重要原因是如果你有时间敏感的数据——如果一个小动物的位置被丢弃,你最好忽略它并发送下一个位置数据包,而不是重新发送最后一个。

而且我不认为任何高性能游戏都会将每个调用都解析为对数据库的调用。更常见的是(如果甚至使用数据库)偶尔或在重要事件中持久化数据。

你不会在 http 之上实现 Counterstrike 或任何类似的东西。

于 2010-02-26T04:13:33.093 回答
3

TCP/IP 由三个协议组成:

  1. TCP
  2. UDP
  3. ICMP

ICMP 是您在网络上 ping 另一台计算机时使用的。

像反恐精英这样的游戏并不关心你以前做过什么。所以不需要完整性,能够重建你所做的事情(这就是为什么竞争对手必须记录他们正在做的事情)。这就是 UDP 的用途——不能保证数据的传送或接收。这就是为什么滞后会成为一个问题的原因——你已经死了,只是你不知道而已。

TCP 保证数据的发送和接收。比UDP慢。

要获得快速连接,需要注意很多事情 - 更少的跃点等。

于 2010-02-26T03:57:46.127 回答
2

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.

于 2010-02-26T04:08:10.987 回答