最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

asp.net core - Why is it that all the examples i can find for WebSocket in C# have websocketHolder as a singleton? - Stack Overf

matteradmin6PV0评论

Im trying to learn and understand on how to use websockets. And i have noticed that every tutorial and example i have seen have the holder class for websocket registred as a singleton.

Why doesent each connection have its own handler? I think that would make it more easier to handle concurrent data. for ex a chat application where multiple user send messages at the same time, or multiple parallel chatrooms, etc.

To me it seems like a bottleneck, that when server is accepting data from 1 client, all the other connections have to wait. Or am i mistaken?

Im trying to learn and understand on how to use websockets. And i have noticed that every tutorial and example i have seen have the holder class for websocket registred as a singleton.

Why doesent each connection have its own handler? I think that would make it more easier to handle concurrent data. for ex a chat application where multiple user send messages at the same time, or multiple parallel chatrooms, etc.

To me it seems like a bottleneck, that when server is accepting data from 1 client, all the other connections have to wait. Or am i mistaken?

Share Improve this question asked Nov 16, 2024 at 19:00 Marko TahtMarko Taht 1,5361 gold badge21 silver badges52 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

In most WebSocket examples in C#, a singleton is used to manage connections to make things simpler and to have a central point for managing all connections. However, this doesn't mean that all connections have to wait for each other. WebSockets work concurrently, meaning when a message comes from one client, the server can process it without blocking the other connections. The use of a singleton is just for better and centralized management, not to cause delays or blocking of other connections.

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far