开发者

How to implement a multiplayer browser game?

Specifically, how to implement the multiplayer portion? I've been toying around with checkers to exercise my JS/PHP/AJAX muscles and have it working pretty well for one person (or two people on the same computer). But I'm kind of getting stumped when it comes to making it work between two people on two different computers. I have the AJAX portion down, and the server is receiving/sending moves from/to the browser. I just can't wrap my head aro开发者_高级运维und what I need to do next to incorporate a second player.

Do I need MySQL for something as simple as this? Can I use some combination of the players' session IDs to just transmit the moves back and forth, rather than storing any info server-side? How does a game session even get started between these two independent entities?

Sorry if this question is ignorant, but I'm just really unsure on how to proceed from this point.

Edit: Just to clarify, there's definitely a server involved (hence the PHP/AJAX references). This much is obvious to me. A "move" will obviously require a trip from player A to the server, then to player B. It's the how that I'm drawing a blank on. Having said that, there seem to be some good answers below and I'll investigate each of them in turn. But feel free to keep adding possible suggestions/solutions as I've already learned a lot just from basic research into the answers already posted.


I'd recommend going with Comet.


You can repurpose one of the chat like systems such as jabber and create an equivalent of a chatroom where messages are received and retransmitted to all clients connected. This is where real time aspects and sequencing comes into play. Since checkers is turn based that is quite simple. You can use a standard Jabber server and a Javascript chat client in the browser. This will have the added advantage of letting your players actually message each other on the same bus in addition to game moves.


Direct answer to the question asked: you do need some sort of "global storage" on the server side, as each Session is independant and does not know each other.

You can use text file, database or memory storage, in ASP it's known as Cache or Application level variables, dunno about PHP.


  • Use a database if you want to hold player/user information (this should be obvious).
  • If it's session based, you definitely need a server.
  • For client-side I suggest something like Silverlight or Flash...etc.

  • For a chatroom-like experience you could have each client connect to the server with the ability to listen for push notifications (this would prevent any kind of manual "polling" to the server for updates). I know this is directly supported with the latest Silverlight and WCF technologies.


Kindly checkout Jaxl (Jabber XMPP Library in PHP/JS) BOSH MUC Chat room sample example. You can easily extend this sample example to develop your browser based multi player games using PHP/JS.


Check APE (Ajax push engine)

http://www.ape-project.org/home.html


One technique I've thought of before (but never implemented) is to actually share the same PHP session between all players (using session_id) but differentiate them based on the IP address ($_SERVER['REMOTE_ADDR']). This way you can store all game state in the same session, and player-specific data can be stored in an array keyed by IP address.

Of course, you need a separate channel to distribute the session ID in the first place. This can be done in different ways:

  1. for private games: give the session ID to the game host and have him/her distribute it to friends who wish to join the game
  2. for public games: use a database to store the session ID of active games and let anyone join
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜