实例介绍
【实例简介】360界面winform
【实例截图】
【核心代码】
using System; using System.Collections.Generic; using System.Linq; using System.Text; using Common; using System.ServiceModel; namespace ChatService { public class ChatEventArgs : EventArgs { public MessageType msgType;//消息类型 public Person person;//做这件事的人,消息的发送者 public string message;//做这件事需要发送的消息 } [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession, ConcurrencyMode = ConcurrencyMode.Multiple)] public class ChatService : IChat { //用于保障多线程同步而设置的对象(lock锁) private static Object syncObj = new Object(); //客户端的回调接口对象 IChatCallback callback = null; //用于广播事件的委托//全局的多播委托 public delegate void ChatEventHandler(object sender, ChatEventArgs e); public static event ChatEventHandler ChatEvent; private ChatEventHandler myEventHandler = null; //利用字典对象chatters来保存聊天者对象以及绑定的对应事件委托 static Dictionary<Person, ChatEventHandler> chatters = new Dictionary<Person, ChatEventHandler>(); //当前聊天者对象 private Person person; // 判断具有相应名字的聊天者是否存在于字典对象中 //如果有,返回true,没有返回false private bool checkIfPersonExists(string name) { foreach (Person p in chatters.Keys) { if (p.Name.Equals(name, StringComparison.OrdinalIgnoreCase)) { return true; } } return false; } // 在字典对象中搜索判断其中是否包含了相应名字的聊天者, // 如果有则返回其对应的委托ChatEventHandler;否则返回空 private ChatEventHandler getPersonHandler(string name) { foreach (Person p in chatters.Keys) { //不区分大小写 if (p.Name.Equals(name, StringComparison.OrdinalIgnoreCase)) { ChatEventHandler chatTo = null; chatters.TryGetValue(p, out chatTo); return chatTo; } } return null; } // 在字典对象中搜索判断其中是否包含了相应名字的聊天者, // 如果有则返回聊天者对象;否则返回空 private Person getPerson(string name) { foreach (Person p in chatters.Keys) { if (p.Name.Equals(name, StringComparison.OrdinalIgnoreCase)) { return p; } } return null; } /////////////////////////////////////////////////////////////////////////////////////////////////////// // 加入聊天,如果字典对象chatters中没有同名的聊天者 public Person[] Join(Person person) { //添加用户是否成功 bool userAdded = false; //创建新的ChatEventHandler类型委托,其指向MyEventHandler()方法 myEventHandler = new ChatEventHandler(MyEventHandler); //执行关键区域,判断是否存在同名聊天者, //如果不存在则向字典中添加该对象并将MyEventHandler委托作为其value, //以待后面来触发 lock (syncObj) { if (!checkIfPersonExists(person.Name) && person != null) { this.person = person; chatters.Add(person, MyEventHandler); userAdded = true; } } //如果新的聊天者添加成功,获得一个回调的实例来创建一个消息, //并将其广播给其他的聊天者 //读取字典chatters的所有聊天者对象并返回一个包含了所有对象的列表 if (userAdded) { callback = OperationContext.Current.GetCallbackChannel<IChatCallback>(); ChatEventArgs e = new ChatEventArgs(); e.msgType = MessageType.UserEnter; e.person = this.person; //广播有新聊天者加入的消息 BroadcastMessage(e); //将新加入聊天者对象的委托加到全局的多播委托上 ChatEvent = myEventHandler; Person[] list = new Person[chatters.Count-1]; Person[] listAll = new Person[chatters.Count]; //执行关键区域,将字典chatters的所有聊天者对象 //拷贝至一个聊天者列表上,用于方法返回 lock (syncObj) { chatters.Keys.CopyTo(listAll, 0); int i = 0; foreach (Person name in listAll) { if (name.Name != person.Name) list[i ] = name; } } return list; } else { return null; } } // 广播当前聊天者输入的消息给所有聊天者 public void Say(string msg) { ChatEventArgs e = new ChatEventArgs(); e.msgType = MessageType.Receive; e.person = this.person; e.message = msg; BroadcastMessage(e); } //在字典对象chatters中查找具有相应名称的聊天者对象, //并异步地触发其对应的ChatEventHandle委托 public void Whisper(string tosend, string message) { ChatEventArgs e = new ChatEventArgs(); e.msgType = MessageType.ReceiveWhisper; e.person = this.person; e.message = message; try { ChatEventHandler chatterTo; //执行关键区域,获取具有相应名称的聊天者对象在chatters中所对应的委托 lock (syncObj) { chatterTo = getPersonHandler(tosend); if (chatterTo == null) { throw new KeyNotFoundException(tosend "没有登录!"); } } //异步执行委托 chatterTo.BeginInvoke(this, e, new AsyncCallback(EndAsync), null); } catch (KeyNotFoundException) { } } /// 当聊天者离开时,从字典chatters中移除包含该对象的项。 /// 并从全局的多播委托上移除该对象对应的委托 public void Leave() { if (this.person == null) return; //获得该聊天者对象所对应的ChatEventHandler委托 ChatEventHandler chatterToRemove = getPersonHandler(this.person.Name); //执行关键区域,从从字典chatters中移除包含该聊天者对象的项。 lock (syncObj) { chatters.Remove(this.person); } //从全局的多播委托上移除该对象对应的委托 ChatEvent -= chatterToRemove; ChatEventArgs e = new ChatEventArgs(); e.msgType = MessageType.UserLeave; e.person = this.person; this.person = null; //将消息广播给其他聊天者 BroadcastMessage(e); } // 当chatters中的聊天者对象所对应的ChatEventHandler委托被触发时, // MyEventHandler方法将执行。 // 该方法通过检查传递过来的ChatEventArgs参数类型来执行相应的客户端 //的回调接口中的方法。 private void MyEventHandler(object sender, ChatEventArgs e) { try { switch (e.msgType) { case MessageType.Receive: callback.Receive(e.person, e.message); break; case MessageType.ReceiveWhisper: callback.ReceiveWhisper(e.person, e.message); break; case MessageType.UserEnter: callback.UserEnter(e.person); break; case MessageType.UserLeave: callback.UserLeave(e.person); break; } } catch { Leave(); } } // 异步地触发字典chatters中所有聊天者对象所对应的ChatEventHandler委托。 // BeginInvoke 方法可启动异步调用。第一个参数是一个 AsyncCallback 委托, //该委托引用在异步调用完成时要调用的方法。// 第二个参数是一个用户定义的对象,该对象可向回调方法传递信息。 //BeginInvoke 立即返回,不等待异步调用完成。 //BeginInvoke 会返回 IAsyncResult,这个结果可用于监视异步调用进度。 private void BroadcastMessage(ChatEventArgs e) { ChatEventHandler temp = ChatEvent; if (temp != null) { foreach (ChatEventHandler handler in temp.GetInvocationList()) { handler.BeginInvoke(this, e, new AsyncCallback(EndAsync), null); } } } // EndInvoke 方法检索异步调用的结果。//调用 BeginInvoke 后可随时调用 EndInvoke 方法; //如果异步调用尚未完成,EndInvoke 将一直阻止调用线程,//直到异步调用完成后才允许调用线程执行。 private void EndAsync(IAsyncResult ar) { ChatEventHandler d = null; try { //System.Runtime.Remoting.Messaging.AsyncResult封装了异步委托上 //的异步操作的结果。 //AsyncResult.AsyncDelegate 属性可用于获取在其上调用 //异步调用的委托对象。 System.Runtime.Remoting.Messaging.AsyncResult asres = (System.Runtime.Remoting.Messaging.AsyncResult)ar; d = ((ChatEventHandler)asres.AsyncDelegate); d.EndInvoke(ar); } catch { ChatEvent -= d; } } } }
好例子网口号:伸出你的我的手 — 分享!
网友评论
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明
支持(0) 盖楼(回复)