频繁的Ajax呼叫(每秒2-3次)

I am creating a game, where the user is moving the main object (left/right/up/down). Every time he moves it, he will make a request to a server, so PHP will place the position of the player onto the map.

The game is so fast, that the user should move 2-3 times per second, after 196-200 request I usually get an error, and not able to connect to the server for about 3-5 minutes, which is not good.

How can I solve this issue considering that I have full access to Control Panel and can not change the request frequency?

Sometimes your HTTP server or firewall blocks these request for prevent DDoS attacks.

To solve that problem, you can use Websocket (https://en.wikipedia.org/wiki/WebSocket) instead of ajax calls.

For each ajax call, the browser will made these steps:

Request 1:

- Open Connection
- Do Request
- Get Any Response
- Close Connection

Request 2:

- Open Connection
- Do Request
- Get Any Response
- Close Connection

But if you use websockets you can just:

- Open Connection
- Do Request
- Do Request
- Do Request
- Do Request
- Do Request
- Do Request
- Get Any Response
- Do Request
- Do Request
- Do Request
- (...)

And the close step will only be executed when you change the page, close the browser or force to close the Websocket connection via API.

That solution was created just for these scenarios, and there is some HTML game engines using the solution for multiplayer games (such as http://www.isogenicengine.com/)

For PHP you can use the Ratchet (http://socketo.me/) or the PHP-websockets (https://github.com/ghedipunk/PHP-Websockets) implementation.

I hope it helps