Search code examples
node.jsp2pmultiplayerpeerjsreal-time-multiplayer

Updating the game state in a multiplayer game


I am working on a multiplayer game. Each client have a character that moves in a shared environment.
I use socket.io to create rooms and peer.js to create a peer-to-peer connection between clients.
What I am trying to do is to enable each client to update the positions of the characters of the other players in his map.
For that, each client should have the state of the keyboard cursors (arrow keys) of the other players so that he can move their corresponding characters with a walking animation.

P2P: I am thinking of creating duplex streams between the clients so that each client will have the state of the keyboard cursors of the other players, so that he can move their characters with the appropriate animation...
SOCKETS: I can also pass the information via the server using sockets, but i will have to send an update of the cursors state 60 times per second since the game is on 60 fps, which makes a lot of socket messages. I am not sure this is the most efficient way to handle it

What is the most efficient way to keep everybody updated about the state of the other players ? Any suggestion will be appreciated. Thanks.


Solution

  • Actually your game is likely a little demo of a MMORPG game or something like CS/CSGO.

    For such a game, we always have a loop in main process (work process) with a frequency like several frames a second(say 20 frame). In every frame, the client will process the packets received from others and the options form the pleyer.

    If here is 20 frames, that means every fram can't be more than 50ms, so it will cause some delay if it's in WLAN and some packets drop happend.

    If you want to use P2P to synchronized players action, here is a problem: when player number booms, the comlicated of the connection boos too. What's more, you need a reliable connection protocol, that means you need to know how to use something like QUIC or write a reliable UDP by yourself.

    So I think the most effcient way may keep use C/S model insetead of P2P only if your game will use in LAN and there is quite a few players.