0

So my question is: What would be the best way, to make a server-client connection via PHP? My first approach (what I'm using right now) is to do it with AJAX. (with jQuery)

To be a bit more specific: I have a lobby, and a game mode, and my top priorities are the chat, and the in-game users.

What I'm doing right now: I have a PHP file, that updates a record in the database for every user in-game, and another file that cheks this regularly. (let's say in every 5 seconds) The problem with this, that there're a lot of mysql calls, lots of ajax call, and sometimes when the ajax can't load in time, the record won't update in time, ergo -> the user gets disconnected.

Note that I need this for my dissertation so I have time to do my homework about this even if you give me some links.

Any answers and tips are welcome! Thanks in advance.

ZeeCoder
  • 1,001
  • 9
  • 17

4 Answers4

0

You could start by setting a time limit on the AJAX call, and handling any error by double-checking, rather than just disconnecting the user.

One call every five seconds isn't that much, really.

Grim...
  • 16,518
  • 7
  • 45
  • 61
  • Okay, so I use Ajax to check the in-game users, but also for chat, and any event that occurs while being in the lobby, and in the game after. With a lot of users in different games and lobbies at the same time, still wouldn't be much? – ZeeCoder Feb 24 '12 at 15:03
0

You can look at Comet, which uses "long polling" to keep the connection open, allowing for communication without polling.

The issue is that this requires more resources on the server since it may have to keep a large number of connections open simultaneously. If you're on a hosted server with multiple server, managing state can become a headache as you either need to stick the user to a single IP/server or manage the state across servers. You're trading off one set of resources for another.

One this you may want to consider (aside from Coment) is keeping to DB memory-resident with REDIS or MEMCACHED to greatly improve DB performance.

Diodeus - James MacFarlane
  • 112,730
  • 33
  • 157
  • 176
0

try using comet or reverse ajax

http://www.javabeat.net/articles/218-introduction-to-comet-and-reverse-ajax-1.html

Reverse Ajax implementation using php

Community
  • 1
  • 1
mdprotacio
  • 842
  • 6
  • 18
0

An alternative is to use shared memory - PHP: Semaphore, Shared Memory and IPC.
A potential problem could be limitations on shared memory imposed by the system. Also, access to the shared resources might get complicated since you have to make sure any they are guarded against simultaneous read/write. However, this should be faster then using a database. The data can be flushed to a DB once every minute or half a minute, etc.

If the game mode requires a lot of requests/responses per second, then creating a separate program to handle game logic would be a better solution, instead of using PHP.
Another option is to make the game stateful (if that's possible).

Plamen
  • 165
  • 2
  • 10