private void BroadcastMessage(ChatEventArgs e) { ChatEventHandler temp = ChatEvent; if (temp != null) { //循环将在线的用户广播信息 foreach (ChatEventHandler handler in temp.GetInvocationList()) { //异步方式调用多路广播委托的调用列表中的ChatEventHandler handler.BeginInvoke(this, e, new AsyncCallback(EndAsync), null); } } }
public void Leave() { if (this.name == null) { return; } lock (syncObj) { chatters.Remove(this.name); } ChatEvent -= myEventHandler; ChatEventArgs e = new ChatEventArgs(); e.msgType = MessageType.UserLeave; e.name = this.name; this.name = null; BroadcastMessage(e); }
public void Whisper(string to, string msg) { ChatEventArgs e = new ChatEventArgs(); e.msgType = MessageType.ReceiveWhisper; e.name = this.name; e.message = msg; try { ChatEventHandler chatterTo;//创建一个临时委托实例 lock (syncObj) { chatterTo = chatters[to]; //查找成员字典中,找到要接收者的委托调用 } chatterTo.BeginInvoke(this, e, new AsyncCallback(EndAsync), null); //异步方式调用接收者的委托调用 } catch (KeyNotFoundException) { } }