AnswerBun.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!

Related Questions

Best way to save game data in Unity

1  Asked on February 28, 2021 by tair-galili

   

Unity – Sprite vertices in 3D world

1  Asked on February 24, 2021 by muckington

   

How to represent a modular FSM for AI using ECS?

2  Asked on February 22, 2021 by christian-ivicevic

       

get access to Transform of a Clone in Unity

1  Asked on February 21, 2021 by amin007

   

Box2d on authoritative server for Unity client

0  Asked on February 19, 2021 by antonio-agustin

         

How to Send Voice over Unity Networking – UNET

1  Asked on February 12, 2021 by muhammad-faizan-khan

         

Mathf.Clamp not Working Properly

1  Asked on February 3, 2021 by shubhendra-chaddha

   

SetTrigger Only Works In Start and Not OnTriggerEnter

1  Asked on February 3, 2021 by kit-k

       

How do I detect image clicks in Dark GDK?

1  Asked on February 2, 2021 by bobman

   

FIx color banding in unity

0  Asked on February 1, 2021

 

OnMouseOver not registering collisions

3  Asked on January 29, 2021 by hyperific

   

Call native code from Unity iOS build error

1  Asked on January 29, 2021 by atlantis

   

Ask a Question

Get help from others!

© 2022 AnswerBun.com. All rights reserved. Sites we Love: PCI Database, MenuIva, UKBizDB, Menu Kuliner, Sharing RPP, SolveDir