Tell me more ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

Assume that we have a multiplayer game and the users are moving their characters (might be a circle) on the game area. At every game loop some changes can be happened. Should clients send their changes at every game loop to the server ? What would be best practice to comunicate clients each others by avoding overloading server?

Note: I'm asking this question for mostly HTML5 multiplayer games and will be using Box2D.

share|improve this question

2 Answers

I'm far from an expert but I've been working on an HTML5 version of Pong that uses Websockets for multiplayer so I can share what's been working for me. The general maxim I've been living by is:

"Send the minimal amount of information necessary as infrequently as possible."

In my case I maintain the state of the keyboard by listening for the keyup and keydown events in the client. When one of these events is fired the client sends a message to the server and then updates its own state. This allows the client to employ client-side prediction (see this article for more details) and gives the server the minimal amount of information it needs to execute the move.

When the server receives one of these events it updates its own internal state and then rebroadcasts the event to all other clients so they can handle it as well.

To keep everything in sync the server will periodically (every 10 ticks of the engine in my case) send a "sync state" message to all of the clients giving them the exact positions of all players. You never want to sync the state of a client to the server as that opens up all sorts of cheating opportunities.

I found Maple.js to be quite useful as a reference for a concrete implementation in Javascript.

share|improve this answer

There is usually no good reason for the clients to send their positions at every game loop, because in most situations their positions will either not change at all or change in a very predictable way (because they are moving with a constant speed in a constant direction).

To avoid a storm of lots of redundant network packages, you should just report changes in movement.

Also keep in mind that all game mechanics should happen on the server. When a client says that it moves towards point x:y, the server should treat this as a request to do so, and only relay it to the other clients when its actually able to do it according to the game mechanics. When you don't do this, players will be easily able to cheat.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.