phpsocketsmultiplayer

How do online games connect players together?


I've been making an online based connect4 game which, for the most part, works perfectly.

The only problem with it is that when a move is made, the game invokes a PHP script which writes data used for the session to a publicly visible text file, and then is read by the other player (using jQuery), and whose computer updates and renders it accordingly.

However, I feel that this is a very bad way of achieving this goal. I've thought about using sockets, but since there is no daemon running on the computers listening for incoming packets, and the ports aren't being forwarded on the players computers, I'm not sure how could I achieve this effect using sockets.


Solution

  • Few online games connect players directly. In almost every online game, all data exchange goes over a central server. That's important to prevent cheating. As long as a neutral server manages the game state, no client can manipulate it to its advantage.

    To save bandwidth it isn't a good method to read the whole game state with every move. It would be better when the client is only informed about the one move which the other player made, as all other information is redundant.

    Javascript Websockets would be the best option to push updates to the other player. Unfortunately not all browsers support it yet (Internet Explorer is unlikely to ever support it on any operating system prior to Windows 7). An inferior but much more widely supported option would be AJAX.