TransWikia.com

Is good idea to use single threaded server for games? Also can it handle multiple clients?

Game Development Asked by TRGamer TR on November 28, 2020

I was developing an game. I using single threaded nodejs(socket.io) server. It is good for simple games or games like basic 3d world? Usually game will have 10-20 players(clients).

Also is single threaded server can handle multiple clients same time?

like player 1 walks player 2 jumps same time.

2 Answers

Usually you would have multiple threads in the case of TCP, each for one client connection, and a thread for gamestate/logic. In the case of UDP you have one listening/sending thread and one game state/logic thread.

TCP:

Anything comes in from a client you put it in a shared queue. For instance, you may have a seperate queue for jumps, for shooting, etc. Any game event happens you broadcast it across the clients.

Anyway, using TCP is a pretty bad idea anyway (for the game itself, for a chat it's good). You want to get packets out as fast as possible, and not wait for stuck packets that may take seconds to clear (which is why you have to use seperate threads, otherwise one bad player connection can throttle everything).

UDP:

You might want to keep track of the order of packets, if that is important to you. Also include a little header in each packet of what packets were last received, so you know if you have packet loss and need to resend anything. Here's some links that explain in more detail with code: https://www.gafferongames.com/post/virtual_connection_over_udp/ https://www.gafferongames.com/post/reliability_ordering_and_congestion_avoidance_over_udp/

Conclusion

As you can tell, both TCP and UDP have drawbacks and strongpoints.

TCP: very reliable, but slow, blocks a thread. Good for web servers, chats, user account login, anything that does not require millisecond speeds, but does require reliability.

UDP: lightning fast, non-blocking, unreliable. Used for things that just need very fast speed, use your own programming or a library to make it reliable.

Luckily since you are using node, somebody else has probably figured it out, sadly I'm not familiar with the node ecosystem.

Also, I'm not sure if it still applies on modern routers and modems, but it's often claimed that using both TCP and UDP on the same port can lead to connection problems. So I recommend you use different ports for your chat (TCP) (if you have a chat) and your game (UDP). It doesn't hurt either way.

Answered by AnnoyinC on November 28, 2020

There is nothing good or bad about using NodeJS.

I have experience with multiple relatively heavy loaded projects (2-500 users on a 1 CPU machine) using nothing but NodeJS and Web sockets. No problems with performance. Games were not very complicated - mostly moving units on a 2D plane, units can shoot missiles or do melee attacks.

I'd recommend one of these packages: wse or vanilla ws.

NodeJS is actually not that slow in compare with c++ or g++.

Talking about performance - I'd recommend avoiding socket.io - is a very comfortable and simple library, but it works much slower. But it still not a problem for quick prototyping or a small number of connections (2-30 is small here).

I hope this answer is subjective enough to match the subjectivity of the subject.

P.S.: I'm currently working on a thing capable to handle 1000+ connections per machine. With no fear.

Answered by Vladimir Ishenko on November 28, 2020

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP