Jump to content

How to make multiplayer online games?


Paul-Andre
 Share

Recommended Posts

I have a good enough sense of how to make single computer games, but network games just throw me off. I tried making a few simple multiplayer online games with html5, node.js websockets, and it's so different from making single computer games. You can learn to make games, (html5 games especially) by actually doing it. You can make a small non-online game and make it grow. For online games, I feel that it's much more complicated. It also feels more fragile. If a normal game runs on your target devices, you're okay. But with online games, so much things can go wrong. I feel that multiplayer games is the edge to what you can get without getting formal education. 

 

To not look sound a noob trying to make a MMORPG, I'll start by... pong, since most people know how to make pong. Let's say I want to make online pong. I'll need to have a server program. Players will be put together two by two so they can play against each other. So in that case, the server will be simulating multiple pong games. So, at that point, all those clients would need to communicate with the server and veci-versa. What would I use, a single port on the server? A single websocket? Multiple websockets? Would all the pong games work in a single program/thread, or would each pong game be there own mini server? Another consideration is the fact that game s may be created and destroyed every second.

 

Now, let's say a MILLION people would want to play on my epic-pong-online-4-free.com site at the same time? It's not really a Massively-Multiplayable-Online-Game,since none of these million people will see more than one adversary at a time. Still, half a million pong games would need to be running! I would need to have more servers. In that case, how would more than one servers work? In a game like Minecraft, you have 1 game per server, but in the case of pong, a good computer might run at least a few dozen games of pong, witch makes it even more confusing. 

 

Also, I've heard about cloud hosting, where the point IS to have lots of computers working for you. But in that case, wouldn't you need a central computer that controls the others? I'm I fan of learning things by doing them, and unlike with html and javascript, it's hard to do with server side stuff, especially when considering more than just my puny little desktop.

 

I tried using nodejs because I felt too lazy to learn another language and another way of doing things. But if there is something that would be more convenient, I would switch to it. I just would like to sort things out and maybe use html5 for the client, and by obligation use websocket for commmunication(and websocket isn't such cool of a protocol anyway, isn't it?) 

 

Oh shit, I am so wordy so late at night!

Link to comment
Share on other sites

Well I took a shot at this for Fight Magic Run this week. I used JSONP with cgi-scripts server-side to manage the data. So basically every time I needed to send information I issued an HTTP Get, and I issued a different HTTP Get every 1 second to retrieve state from the server. Roughly 75% of the 'multiplayer' work was setting up the environment so users could actually decide who to play, and only 25% for maintaining the actual game sessions. For even a simple board game, I ended up with maybe 10 separate server-side scripts for the various things - submit a move, check game status, check user status, challenge a user, cancel a challenge, send a 'I'm still online' ping, etc.

 

This kind of method left a lot to be desired - if instead I had some kind of centralized thing that abstracted the various tasks a bit (for instance, 'relay data to user', etc), it would have prevented the different scripts from becoming subtly inconsistent with each-other during edits, something which happened a few times. I'd probably try to do it differently the next time.

 

For real-time I would have needed to use websocket. I'd need a server that basically sits there, grabs incoming connections, and just relays what needs to be relayed.

 

One thing I haven't been able to figure out though is the monetization side of multiplayer. That is to say, lets say you make such a game and it now relies on your server. When you license that to sponsors, how is that going to be received? Furthermore, lets say you don't want to end up having to commit to basically acting as the server for sponsor player-bases, I don't know how open sponsors would be to 'and you also have to run this server-side stuff and accept connections on this or that port'.

Link to comment
Share on other sites

I left a program running on node.js that used websockets, and it crashed for some untold reason, I think because of an IP address reset. These errors are hard to track, because these things are almost impossible to test.

 

Edit: I think it was because of a IP reset, but I'm really not sure. I wonder if there is a way to force change my external ip, to see if this was the issue.

Link to comment
Share on other sites

This is a mega big topic to write about.

In the first place you should know how TCP/IP works, then you will know how websocket works (websocket is really just forcing data format over TCP/IP).

Pong game that you want to create is not really a good way to learn. In pong games you steer your pad with arrows, and you change direction often, this is close to FPS games when you control character with WSAD. For FPS games TCP/IP is not really an option because you have to read char position as fast as possible from server and UDP is a much better solution for this (TCP resents lost frames which cause big lags sometimes). With pong this is similiar, so IMO you should create something easier or just more suitable for websockets, like checkers.

 

Idea of server and clients is easy. Server is listening on one port for connections from client. Every client that connects to server has assigned new channel created by server. With this channel you can communicate with server. usually it has methods like write and onData. On server you should make data message routing system. Basically every data you get in onData call, should be parsed and according to id (and maybe sub id) you get from it you know what to do with data you got. In example clients sends data (1,2) in binary format, 1 would mean that this is system message, and 2 would mean that user wants to create new checker game. When server gets that message, then it creates new lobby for checkers. Now server should send to channel that it got this data from, message with information that this client has created new game (you should always make confirmations from server). Client also has onData method, and also should parse messages.

This way you can create whole checkers game.

 

About bazilions games you want to create. You should have one main server that is routing connections to subservers. In example you have 100 servers, and 70 of them has high load, and rest low load, so you know that new connection should be routed to server with low load, so you send user ip of low loaded server and user connects to that server.

Link to comment
Share on other sites

I've been working on a multiplayer tower defense game for the last year or so called Tower Storm. The first thing I'd reccommend looking into is using nodejs as your server and socket.io for sending messages. Socket IO is amazing and will make your life 10 x easier than trying to use websockets directly. It's basically a wrapper around websockets that falls back to using different methods of sending messages on older browsers and is much easier to use than Websockets. 

 

There's a really good tutorial on building a multiplayer game using NodeJS + SocketIO here: http://www.youtube.com/watch?v=adwOLUpQF0I

 

With having multiple servers and handling multiple players what I did with Tower Storm is have a bunch of servers behind a load balancer. Then when a player connects they connect to the load balancer which randomly forwards them onto one of the servers. Then they connect to this server directly using websockets and talk to it for the duration of the game. For having multiple games on the one server I used socket.io namespaces (about 2/3rds of the way down http://socket.io/#how-to-use) to create a unique namespace for each game that clients connect to to only send and recieve data for that game. 

 

Hope that helps, let me know if you have any more questions :)

Link to comment
Share on other sites

Thanks everybody for your replies. I'll be thinking about this.

 

Then they connect to this server directly using websockets and talk to it for the duration of the game.

 

When you say connect directly, do you mean that the servers are open to the Internet?

Also, I'm not sure if I should use web-sockets to create new rooms and not rather use plain http.

I have a few projects, and one of them is a real time pixel-art application. A protoype (with just one image) may be found here: http://pixels.dns53.biz:8000/ (It's running on my home computer for demonstration, and will run for maybe a few more hours) I am just wondering how should I build the "architecture" to have multiple images. I used pure binary websockets for the drawing part, so using socket.io probably isn't an option. Also, I would like to ask, when you people have more than one server, do they all have an external address, and do they all have a wide range of openable ports?

Link to comment
Share on other sites

Ezelia, I glanced at eureka.io, and I find the way it works interesting. It's about executing client functions from the server and veci-versa, right?

rolnaaba, I've heard about Pomelo, but somehow I wasn't convinced. Did you make something with it?

 

Nope! But I have heard nothing but good stories on the node.js mailing list, and their arch docs look solid.

Link to comment
Share on other sites

@Paul-Andre eureca.io present an abstraction layer to make client <-> server calls more intuitive I'm using it in my own mltiplayer implementations :)

Pomelo is very good if you are making an MMORPG, but for simple multiplayer games, Pomelo is too complex IMO the time it'll take you to understand its architecture, write your handlers ...etc is more that the time needed to write simple multi-player implementation.
 

 

it all depend on what you are targetting :)

Link to comment
Share on other sites

A couple of years back, I wrote a realtime html5 multiplayer game for the  company I work at ( Ogilvy & Mather ).

After the project, I abstracted out the realtime multiplayer engine from the game, and gave a talk on it at JSConf 2010 - as well as publishing the results on GitHub.

 

You can find the project here:

https://github.com/onedayitwillmake/RealtimeMultiplayerNodeJs

 

And here's a demo of the project:

 

I believe something in the websocket spec has changed, so the socket.io used in the project needs to be updated, and I haven't had time to do that myself but it shouldn't be too much work.

Link to comment
Share on other sites

@Quetzacotl

 

TCP/IP is fine, you don't need UDP for Pong, don't be ridiculous. The only problem with TCP/IP is that it can use Nagle's algorithm, which is an issue, since Pong would most likely send lots of small packets, but it's disabled by default in socket.io.

 

Pong is a great place to start because it will require you to implement client-side prediction, which is an essential thing to understand. Writing Draughts, or what ever, is a waste of your time, you won't learn much of anything from doing that except for the socket.io/WebSocket API (which takes 10 minutes on its own).

Link to comment
Share on other sites

TCP/IP is fine, you don't need UDP for Pong, don't be ridiculous. The only problem with TCP/IP is that it can use Nagle's algorithm, which is an issue, since Pong would most likely send lots of small packets, but it's disabled by default in socket.io.

I respectfully disagree.

TCP has more problems for real-time things than just Nagle's. For example a dropped packet can mean waiting for useless data for a long time. Not to talk about actual packet size and how fast those packets go through routers.

I'm not saying that it can't be done though. Just that Nagle's isn't all there is to it.

Link to comment
Share on other sites

I agree with all that except that Nagle's isn't the only real issue. Dropped packets are still a real issue even if they can be solved by extrapolation like Ezalia suggested, but it won't be as good as using UDP in the first place.

So again, I'm not saying TCP is unusable (iirc even big mmo like Lineage 2 used TCP), just that it's not suited for real-time games.

Link to comment
Share on other sites

@Travis:

 

I am the author of PeerGaming and although its created for the exact purpose of helping devs to create multiplayer games without regarding an additional server, I currently can't recommend to use WebRTC (~ PeerConnections / DataChannels) in real applications or games yet. Unfortunately the browser support is quite limited and a cross-browser communication between at Firefox and Chrome still need some fixes (Desktop & Mobile). As reliable channels are implemented and getting stable for proper data exchange, I will update the framework and create proper examples for showing its usage. So far it broke already a few times because of these changes. It will be official announced as soon as the vendors adapt the browser internals according to the latest specs and finalize their API  :)

 

@Paul-Andre:

 

For the basic understanding of creating a realtime multiplayer game with socket.io could you take a look at this article of buildnewgames.

In a previous thread were also various ideas & hosting options mentioned.

Link to comment
Share on other sites

@Travis:

 

I am the author of PeerGaming and although its created for the exact purpose of helping devs to create multiplayer games without regarding an additional server, I currently can't recommend to use WebRTC (~ PeerConnections / DataChannels) in real applications or games yet. Unfortunately the browser support is quite limited and a cross-browser communication between at Firefox and Chrome still need some fixes (Desktop & Mobile). As reliable channels are implemented and getting stable for proper data exchange, I will update the framework and create proper examples for showing its usage. So far it broke already a few times because of these changes. It will be official announced as soon as the vendors adapt the browser internals according to the latest specs and finalize their API  :)

 

Ah, I see. I read a portion of your thesis, the idea is awesome and something I definitely want to use in my projects when the support is there!

Link to comment
Share on other sites

Thanks for the resources. I've got something going.

Some more or less specific questions:

 

If for some reason I need a png to be transmitted to the players once they join my game (players can join anytime) what is better, to pipe it through WS, or to have the client fetch it though http?

 

If each player has an avatar image, how should I transmit them?

 

Would using node.js for the game backend of the multiplayer part, but some other language (like php or python) for the rest of my web server be a good Idea?

 

How do I run this all on my computer, and how would I make sure it would run in any other place?

 

Do I need to get formal education for stuff like this, (or some education I can get from a book)?

Link to comment
Share on other sites

Thanks for the resources. I've got something going.

Some more or less specific questions:

 

If for some reason I need a png to be transmitted to the players once they join my game (players can join anytime) what is better, to pipe it through WS, or to have the client fetch it though http?

Http definitely. Mainly because static files should be handed by static file server (apache, nginx).

 

If each player has an avatar image, how should I transmit them?

Ajax will be ok.

 

Would using node.js for the game backend of the multiplayer part, but some other language (like php or python) for the rest of my web server be a good Idea?

Whatever suits you. But if you know Python, then don't go with node.js. Python Twisted is a better option. I'm using it, it works great and you have much cleaner and easy to maintan code.

 

How do I run this all on my computer, and how would I make sure it would run in any other place?

I don't get what you mean. Use few machines and test it on local network.

 

 

Do I need to get formal education for stuff like this, (or some education I can get from a book)?

In IT you need experience at most.

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.

Guest
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.

Loading...
 Share

  • Recently Browsing   0 members

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