Good day,

the project is a multiplayer game designed for a variety of players (up to 100), like agar.io, and so on. Server in C + +, interface - JavaScript (everything happens inside the browser). The question is how to implement data transfer.

Ideas:

  • TCP - WebSocket
  • UDP - netcode.io being developed "protocol" (but in view of the fact that portability is only on Windows, the idea has been dropped)

Dear users, can you tell me how best to implement these things. And if WebSocket offers, which library to use for it in C ++?

  • I did not understand something. Was it a question or an answer? - Drakonoved
  • one
    @Drakonoved, I just described what I managed to dig up, I want to consult with people who may have already come across a similar one. - koshachok
  • one
    For this kind of games, ideally use UDP (since the state of the world is constantly changing and the previous one becomes irrelevant). Browsers are not going to work directly with UDP yet (and they are unlikely to be), so you can try using RTCDataChannel in WebRTC, with a fallback on web sockets. - Vladimir Gamalyan
  • Look at boost :: asio - Mishakov Maxim

2 answers 2

Well, with the protocol you have already decided on your own question.

How best to implement these things:

For you, the ideal option (if it is a js client and a c ++ server and up to 100 users):

Library: socket.io

Sample server in c ++: c ++ socket.io server

With the right approach and hardware, such a server can handle more than one thousand users online.

  • @Mike socket.io is just for web sockets (with rollback to long-polling and other methods if web sockets are not available in the browser). - Vladimir Gamalyan
  • @VladimirGamalyan is curious, but why then in the example of the server websocket ++ is also used - Mike
  • @Mike In socket.io there are client / server implementations for different languages. For the server on the pros, websocket ++ is used to work with websockets (in order not to reinvent the bike probably). - Vladimir Gamalyan
  • Only it should be noted - the number of users is very conditional, because depends on the hardware, and the iron architecture of the system (cluster), and of course on the directness and complexity of the work of the backend itself. - Goncharov Alexander
  • I myself use socket.io on my servers and I can confidently say that even on such a middling one as 4 3.4 Hz cores with 8GB frames per ssd easily holds a hollow connection between 1k online. The server itself is on java. - Sergey

Hi, Poco is right for you . There is a WebSocket implementation and JSON is very convenient.

To ensure good speed, it is advisable to break the information into small messages. For example, to organize the connection between interrelated objects on the server and on the page, and use the WebSocket connection as a router between them.