FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


multiplayer: use db, sockets or others?





lightwate
I'm thinking of making a multiplayer turn based HTML game (let's say tic tac toe). I store the players' moves in a database and the client side HTML just queries the moves repeatedly. I think that this method is slow and ineffiient especially if a lot of people are playig. Is it really inefficient? What other alternatives do i have? I am thinking of sockets but maybe that's a bit overkill. thanks Smile
Fire Boar
You'll want to use AJAX for polling the server. There are a couple of ways that you can do it:

- Periodic polling (poll the server every 5 seconds or so, receive new info or "no data" message).
- Long polling (poll the server whenever possible, server doesn't respond immediately).

The latter is more interesting, sometimes called Comet. The idea is basically to have a loop like this (written with jQuery):

Code:
/**
 * listen: A function which long-polls the server at event.php, which returns
 * data comet-style, as and when it is ready. It then interprets that data and
 * makes the necessary updates. This need only be called once: it loops
 * indefinitely.
 */
listen = function()
{
  /**
   * Interpret the received message data.
   *
   * @param data The data received from the server.
   */
  var interpret = function(data)
  {
    // YOUR CODE HERE.
  };

  /**
   * Long-poll the server. The server interfaces with the client using the
   * event.php gateway. When it receives a request, it will respond when there
   * is data available. If no new data is available, it will wait until it is.
   */
  var poll = function()
  {
    $.post("event.php", function(data)
    {
      interpret(data);

      // setTimeout to avoid infinite tail recursion.
      setTimeout(function()
      {
        poll();
      }, 0);
    }, 'text');
  };

  return function()
  {
    poll();
  };
}();


The event.php script should look something like this:

Code:
<?php

while (true)
{
  // Sleep for 10ms to avoid overloading the CPU. Sadly PHP doesn't support threads.
  usleep(10000);

  if (/* Some condition holds where returning data is justified. */)
  {
    break;
  }
}

// Send output to browser.


Then all you have to do is call listen() on the client end, and it will request event.php immediately. Whenever the request returns, interpret() is called, and then event.php is requested again. The server waits until it needs to respond, and then responds.
jmraker
You wouldn't need to keep a history of every move (unless you need an undo), you could just store a serialized array of the game state or store them all in separate fields if you think you'll change the game internals around

The only reason why you'd store a game state in a database would be so they could come back a day, month or years later

I wouldn't use websockets, the websocket protocol is still kinda changing. When it was updated in many browsers (it was temporarily removed due to a security hole) the websocket demos stopped working. Plus websockets need a socket server program running and there doesn't seem to be a free php one that works with the newest protocol.

On the other hand websockets are the ideal solution because without it as the number of users grows, the number of update requests increases and normal ajax calls will eventually start a friendly denial of service attack that will make it look like the server is down. Long polling will help until game becomes active where the server loop is hardly sleeping and acts the same or worse as periodic polling.

You can base the amount of time it sleeps with the 1 or 5 minute load average from the sys_getloadavg function so the game would slow down before there's too many things waiting to run
http://php.net/manual/en/function.sys-getloadavg.php

Code:
$avg = sys_getloadavg();
$updateInterval = round(10000 + (900 * $avg[1] * $avg[1]));


Edit:
Websockets were enabled again in Firefox 6 and updated in 7 but it's object is named window.MozWebSocket until it's standardized
http://hacks.mozilla.org/2011/08/firefox6/
jcreus
lightwate wrote:
I'm thinking of making a multiplayer turn based HTML game (let's say tic tac toe). I store the players' moves in a database and the client side HTML just queries the moves repeatedly. I think that this method is slow and ineffiient especially if a lot of people are playig. Is it really inefficient? What other alternatives do i have? I am thinking of sockets but maybe that's a bit overkill. thanks Smile

If you want to use the /latest/ technology and it is a website, maybe you could look into WebSockets. However, not all the browsers support it (not even Firefox) and I wouldn't recommend it.

Sockets cannot be directly used in web pages, so not an option.

Maybe Keep-Alive connections? Then the number of requests lowers pretty much.

Else, the classical polling every n seconds.
kacsababa
First off Firefox does support WebSockets and you could still fall back to flash based sockets for non-supportive browsers, its not the best, but a possibility.

The main problem with WebSockets is that it's draft is changed a lot in the past and it may will be in the near future too. The meaning of that is different browsers could support different version of the draft. It can be a headache.
Related topics
Net-powered computer goes on show
Which Is Your Fav StrateGy Game?
problem in sql and importing db.
hi
What do most of you do when you are online?
Where I can find a 3d chat script
Anyone?
Thumbs.db and _vti_cnf
Game Idea: Multiplayer Space Turn-Based Strategy game
Mod Projects lots of info
Game Maker
PSN versus Xbox Live versus WiiConnect24
help getting form info into mysql db
The Bug Reporter and others lids
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.