I am working on a multiplayer HTML5 game that makes use of jQuery to poll a server for game state. At present, I'm querying the server with a .ajax call every 2000ms, to give the impression of real-time updates. I'm really not pushing too much data through...mainly just x/y position and a few character attributes...with only a few players being alive in my game instance.

The server itself is a shared-hosting server, and I'd like to try and keep my CPU hits as low as possible as I develop this game (and move it to something a bit higher-powered in the future).

Here is my current method of polling the server for data. Any and all suggestions are welcome, as server loading is kind of a new area for me.

$.ajax({
  type:'GET',
  url:"controller.php?a=player-info",
  dataType:'json',
  success: function(data){
    //parse data
  }
});
....
setInterval(getPlayerInfo,2000);

One thing I should mention is that, since this is on a shared server, I cannot have free-running processes to open connections to (i.e. Node.js).

有帮助吗?

解决方案

First, you are saying that you are not pushing too much data. For me, you are pulling. Which is not exactly the same thing as you might know.

You can use Websockets, long-polling or server-sent events. (Read this article)

其他提示

From what you've said so far, it's not clear that CPU will be a bottleneck for you, but here are a few suggestions that might help you conserve server resources.

Consider using WebSockets instead of AJAX polling. This may eliminate a lot of the overhead of serving pointless "nothing has changed" requests by shifting to a more push-oriented architecture.

You will also probably want to use memcached or some other RAM-based data storage layer (e.g. Redis) to avoid DB bottlenecks.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top