admin管理员组

文章数量:1336083

I am currently making a html5 game with node.js and socket.io. The basics of the game are 4 people moving around as circles trying to hit eachother...

My question is should I use Websockets or WebRTC? What is best for this kind of munication? peer to peer with 4 players or over a server?

Feel free to share your thoughts, I'm pretty new to this stuff..

I am currently making a html5 game with node.js and socket.io. The basics of the game are 4 people moving around as circles trying to hit eachother...

My question is should I use Websockets or WebRTC? What is best for this kind of munication? peer to peer with 4 players or over a server?

Feel free to share your thoughts, I'm pretty new to this stuff..

Share Improve this question edited Nov 5, 2015 at 19:27 Pieter-Jan De Bruyne asked Nov 5, 2015 at 17:37 Pieter-Jan De BruynePieter-Jan De Bruyne 1231 silver badge10 bronze badges 1
  • 1 The primary thing you want ask is: who owns the state and data? If state and data are stored on the clients and sent/received directly to and from each other, what happens if a client gets disconnected? Does part of the game go away? What if they cheat by mucking about with the data? Would you rather the server own the data (or at least validate it) for those reasons? – xdumaine Commented Nov 5, 2015 at 22:45
Add a ment  | 

2 Answers 2

Reset to default 5

WebRTC can be used not only for streaming audio/video, but also for sending data. And P2P is useful when sending huge amount of data.

In your case the traffic is very small. And I see many advantages of using a server - synchronization, or in the future, features like authentication or history.

There is also the implementation part. With WebRTC you still need a signaling server. And websockets are much easier to implement, as you don't have the session negotiation part. Also the connection is faster.

Personally, in your case I would not bother with WebRTC.

Later update: There's also the problem of browser support: websockets vs WebRTC, as @Myst mentioned in the ments.

Use both.

WebRTC data channels are great for sending data with the lowest possible latency between clients, since the data does not go through a server.

From the game you describe, it sounds like low latency will be critical, so I would definitely look at using data channels to update opponents' positions as quickly as possible.

At the same time, I'd send data with web sockets to the server as well, as the keeper of truth in the game, to verify that no-one is cheating.

Fours player should not be a problem. Have each client open a peer connection to all the other clients, in a "mesh".

本文标签: javascriptshould I use websockets or webRTC for 4player gameStack Overflow