2

I'm planning a relatively simple, online, multiplayer game and I'm stumped on what to use for player to player communication.

Game details:

  • 6 Players
  • Chat box
  • 30 second rounds (intermissions)
  • Time sync is important

Players select and action and at the end of the round, the action is performed, there is a brief intermission to detail actions and a new round begins.

I've determined to build this game entirely in JS and PHP (of course AJAX). I just don't know what I should use for the client to client communication - I initially planned on using constant 1/2 second AJAX calls to the PHP script using mySQL to store the game/user data, but I'm afraid that will be too slow and unpredictable.

I considered using something like a XML instead of mySQL and using something like the php fopen() function - what do you guys think about that?

I read in this question here, where the poster is considering using XMPP, could that be a good solution?

Should I go for a socket? Is that necessary for a game that doesn't require all that much data to be exchanged? What would be required to get this going?

Any suggestions on syncing the round time? Or is this something that I even really need to concern myself with?

I'm open to all suggestions and really appreciate any help I can get.

3 Answers 3

2

Go for Comet, you don't need sockets. Comet is a kind of reverse-AJAX. Works perfectly for p2p things like instant messenger, for example, because it pushes data to browser without browser needing to constantly poll the server. And there are many implementations for Comet in JS/PHP, just Google that. Then, as a backend, you could use MySQL or XML or whatever you want (but I would consider MySQL as a better solution, because XML is just a text file which needs to be opened, read, written and closed and the database is designed to be faster than that).

Sign up to request clarification or add additional context in comments.

7 Comments

Re: DB vs XML - There is no XML server, unlike the DB which is a server in itself. This means there will be deadlocks accessing the XML file, unlike the DB.
That sounds exactly like what I'm looking for, however, I'm considering using jQuery and it seems like most people are using Dojo or prototype - any suggestions?
@christian-sciberras yep, that's another drawback. I used Prototype for implementing Comet back in 2008. I suppose jQuery does similar job.
I found CometD which extends jQuery to use it, looks like that'll work for me. But my next concern is handling multiple game rooms - will that require different connections,ports,etc? Anyone know about this?
Well, you can send additional IDs for rooms and they could be managed through PHP script and stored in DB. Then you would simply push relevant IDs to relevant clients (connecting to the script using the ID). Of course that wouldn't be secure enough, but I guess there are many options with sessions, cookies, tokens, etc...
|
2

If you're determined to use PHP/JS/Ajax, then I would suggest you read about using the PHP usleep() function to delay returning the results of an ajax call until the server has something to say. This should result in much faster server to client communication.

I haven't done this myself, but I think the basic principle is that you can delay for 5 to 10 seconds per ajax call, performing a new call whenever one times out or is returned from the server with data.

Also, I would recommend a javascript framework (jQuery for example) to make the ajax calls more manageable.

2 Comments

Thanks, I'll check that out. Yeah jQuery is a given these days, but I still might opt for my own specialized set of functions in the interest of performance, because I may not require all that jQuery has to offer. What do you think about XML vs mySQL vs???
XML vs MySql? How exactly are they related??
1

I am also building a game with JS/PHP and here is my plan: have a plain text file on the server. Whenever the user does an action that needs to be sent to the server it sends an XMLHttpRequest to a php script, which appends the action to the file (for example, if a user launches a missile from Miami to Moscow it would write "launch Miami Moscow" to the file). To check for when the other person does something, you open an XMLHttpRequest on a different PHP script, and when the PHP script returns something, you record what it returned and reopen the connection. That PHP script checks for modifications to the file, and returns the file contents when it does change. Here is the PHP file that checks for changes:

<?php
#First, set infinite time limit
set_time_limit(0);
#record last modification time
$f1=filemtime('./nameoffile.txt');
#wait until the file changes
while($f1===filemtime('./nameoffile.txt'){
    #have a 10,000 microsecond break between each check so you do not use too much resources
    usleep(10000);
readfile('./nameoffile.txt');
?>

You may also want to look into HTML5 WebSockets. I do not use them and do not know if they work with PHP, but they seem interesting.

1 Comment

Websockets do work with PHP, but many hosts don't allow you to open ports, so websocet is not allowed. Websocket is a lot better to minimize latency, many games like slither.io and agar.io use WS

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.