Jump to content



Recommended Posts

Hi. I want to have multiplayer in my game. I have a noob dought ( well I am a newbie). When you implement multiplayer using socket.io for example basically what you have to do is to specify what you send to the server? I mean if your user uses an attack like they used to do in a single player game you have to send that to the server? I am confused because I saw a tutorial on implementing multiplayer on a html 5 game but the person created a game using simple canvas and had to take care of the user position (which was just a small dot) and other stuff. I don't understand how you complement that with a Phaser game for example... Can someone clarify and give me an example? Like a simple plataform game made in Phaser.

Link to comment
Share on other sites

Socket.io tries to leverage web sockets so that you get a duplex stream, i.e. you, as the client, can send stuff to the server but, in difference to regular http requests (which auto-close), the server can also send messages out to specific (or all) connected clients.

The chat example on the socket.io homepage shows an example of this two-way communication. Really, this is all you need for a simple game, its just a message system that describes changes to the game state, the clever bit is that it does this really really fast so you can get a game out of it.

Re-read the chat example a few times and replace the bit that renders DOM for something that renders a sprite to a screen using Phaser. Once you've got that sussed start to expand the message structure you are sending around so that you can be more descriptive regarding the changes it holds, this way you can start to update multiple entities from multiple clients, all referenced by the same ID (or whatever).

Something that might help is to shift your way of thinking, normally with front-end technologies like Phaser we would assume that the app/game starts and ends with the front-end, sure we might consume some data from somewhere or send some data somewhere but we often treat these as satellite services, for a multi-player game you should remove your thinking from being constrained to one client and move it to the server, where the game actually runs, this is called an authoritative server model and its a fairly easy shift in thinking.

The server is the game running, all decisions ultimately should be made here and the clients should be dumb, they just take some inputs and render it and they take some user interactions and pass them along to the server, which processes those inputs and ends up sending some data back to each/specific client. The chat example works exactly like this.

There are, of course, a million different ways to actually implement this, but, start to assume that ultimately state is contained at the server and all your clients do is render stuff and pass user interaction to the server, things get a little easier this way. Inevitably you'll end up adding some logic to the client, but, for now at least, try to keep it minimal.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Create New...