Sign in to follow this  

Multiplayer HTML5

Recommended Posts

CarlosNavarro    105
I've been working on some simple games on HTML5 connecting them with DBs (using AJAX to update them), and I've think on advancing a little more and create something multiplayer. My first thought was to create a game of turns, that is, first one player, then the other (example: almost any card game), using exactly the same method I've been using until now (AJAX(Client)>PHP>MySQL>PHP>Client).
But the real deal would be something a little more dynamic, character moving and so.
Of course, depending on the complexity you can also use the ajax>php>mysql method I talked about (example: just sending to server the new x,y coordinates where the user wants to move to), but if there are things between you can't walk through & stuff it gets more complicated.
Then I remembered when I used Flash for a thing like this, the sockets, servers & all that, so I wonder, creating games with JS also requires that kind of stuff? How data is sent to the server?
I hope you've understand me, sorry for my awful english :S
And another doubt: when I tried Flash & multiplayer I was mad about something related to movement, which data should I send to the server? Because updating every pixel the character moves was insane, I was updating the position in the server a lot of times per second per user and that caused a lot of lag... Sending just the final position (where the user clicked) was not a good idea neither, because if there was anything not-walkable with movement (such as other characters), the user ended in different positions in each client...

Thank you in advance. I'm not asking for code, just some 'light'.

Share this post

Link to post
Share on other sites
hplus0603    11347
[quote name='cnavast' timestamp='1311202204' post='4838173']
Thank you in advance. I'm not asking for code, just some 'light'.

First: do not let players just write to your database using code in the web page. They will figure out how to cheat by issuing their own write requests. You want rules enforced on the server somehow.

Second: To support interactive updating, you want to use a long-poll or "Comet" style update. Typically, you'll want to double-buffer. The client issues two AJAX requests asking for "updates for user X" (properly authenticated). The server will pend both of them, keeping the requests alive, until either a timeout happens (say, 30 seconds), or there is something to send. When there is something to send, and there are two waiting requests from the player, then send the data by returning it through one of the requests. The client then handles the data and issues another request, so again there are two outstanding requests. This will double-buffer/pipeline requests such that you always know that a client is connected while it's active. When you have zero requests from a client, it means they lost the connection and disconnected from the game.

Third: You want a web server that can deal with thousands of active, idle connections at the same time. This means not Apache, and probably not anything Java based. I'd recommend node.js as a first approximation, perhaps.

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this