I’m writting a browser-based game. The client is in jQuery and the server is in PHP/MySQL.
It’s a turn-based game, so most of the client-server communication is realised by call-respond (jQuery-PHP). That calls happens any time the user clicked button or any other active element. After call in JQuery, PHP controller creates some classes, connects to the database and returns respond. That communication is quite good for me, do not cause the problems with the number of connections etc. (it’s similar to the standard trafic during using the website).
Unfortunatelly, I also need some ‘calls’ from server-side. The example is the list of active games to join. Client must be notify any time the game list has changed. And the maximum delay for that is no more than 1 second.
For now I make it by sending ‘ping’ call from client (jQuery) and server anserws with “nothing” most the time, or “game2 created” etc. But that ‘pings’ will be send every second from each of the players. And for each of them, the server will create classes and connect to the mysql which results with “Database connection error”.
Is there any way to minimalise mysql connections and/or ajax calls?
I use standard www server, don’t have root account.
Start with this:
Instead of calling
every secondthe server by both players (which is actually 2 calls, withthe number going up for every player connected), you can optimize it by checking the
idle timeor how much time passed ofdoing nothing; if nothing has been returned for 2 continuous calls, you should increase the call delay to 2 seconds and then to 4 seconds etc. (just play with setInterval and make it run continuously);This will allows some breathing to your app (i had my own game using this)
Next thing to do is the calling policy; Instead of calling the server in player’s command, you can just store the player’s command in a js array and every X seconds send
that array off; if no commands, no ajax call. Yes, you’ll get a delay but think of many users
connected to a possible poor server…
You can also use comet technology if you want to push things further..