简体   繁体   English

C#即时通讯

[英]C# Instant messaging

I'm working on a instant messaging program in C#(for learning only). 我正在使用C#开发一个即时消息传递程序(仅供学习)。
Just wanna to know if my way is right or wrong. 只想知道我的方法是对还是错。
I created a Client class whice contains a NetworkStream and Read/Write functions. 我创建了一个Client类,其中包含NetworkStream和Read / Write函数。
The server creates a new thread for every client, the thread listen for any new messages. 服务器为每个客户端创建一个新线程,该线程侦听任何新消息。

Any better way? 还有更好的方法吗?

Try WCF. 尝试WCF。 Here is a nice sample. 是一个很好的示例。

You don't necessarily need to spawn a thread for each client. 您不一定需要为每个客户端生成线程。 I'd investigate the Observer design pattern as it addresses the publish-subscribe problem, which is a good way to look at an instant messaging application, particularly if you want multiple listeners to one talker. 我将研究Observer设计模式,因为它解决了发布-订阅问题,这是查看即时消息传递应用程序的好方法,特别是如果您希望一个对话者有多个侦听器时。 Here's a good place to start: http://www.blackwasp.co.uk/Observer.aspx . 这是一个不错的起点: http : //www.blackwasp.co.uk/Observer.aspx This link discusses the Observer pattern and mentions instant messaging: http://www.oodesign.com/observer-pattern.html . 该链接讨论了观察者模式并提到了即时消息传递: http : //www.oodesign.com/observer-pattern.html

You may find that a single-threaded approach may be able to keep up with a lot of messages. 您可能会发现,单线程方法可能能够跟上大量消息。 Depending upon how you design you classes you may find it useful to put entire conversations in their own thread. 根据类的设计方式,您可能会发现将整个对话置于自己的线程中很有用。 You should also think about using queues to handle incoming and outgoing messages, with queue readers in their own thread as well. 您还应该考虑使用队列来处理传入和传出消息,队列读取器也位于其自己的线程中。

Sounds like a fun project. 听起来像是一个有趣的项目。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM