日期:2014-05-18 浏览次数:20811 次
using System; using System.Collections.Generic; using System.Linq; using System.Runtime.Serialization; using System.ServiceModel; using System.Text; namespace CoreDll { // NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IService1" in both code and config file together. [ServiceContract(SessionMode = SessionMode.Required, CallbackContract = typeof(IQQSback))] public interface IQQS { [OperationContract(IsOneWay = true, IsInitiating = true, IsTerminating = false)] void Speak(string value); [OperationContract(IsOneWay = true, IsInitiating = true, IsTerminating = false)] void Whisper(string name, string value); [OperationContract(IsOneWay = true, IsInitiating = true, IsTerminating = false)] void Leave(); [OperationContract(IsOneWay = false, IsInitiating = true, IsTerminating = false)] List<string> join(string value); } interface IQQSback { [OperationContract(IsOneWay = true)] void Receive(string senderName, string message); [OperationContract(IsOneWay = true)] void ReceiveWhisper(string senderName, string message); [OperationContract(IsOneWay = true)] void UserEnter(List<string> name); [OperationContract(IsOneWay = true)] void UserLeave(string name); } }
using System; using System.Collections.Generic; using System.Linq; using System.Runtime.Serialization; using System.ServiceModel; using System.Text; namespace CoreDll { [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession, ConcurrencyMode = ConcurrencyMode.Multiple)] public class Service1 : IQQS { IQQSback QQback = OperationContext.Current.GetCallbackChannel<IQQSback>(); delegate void userenter(List<string> username); static event userenter UserEnterEvent; delegate void userspeak(string name, string content); static event userspeak UserSpeakEvent; delegate void userWhisper(string name, string content); static SortedList<string, userWhisper> Userspeaklist = new SortedList<string, userWhisper>(); string Name; public List<string> join(string value) { if (Userspeaklist.Keys.Contains(value)) { return new string[] { "已经有此用户" }.ToList(); } this.Name = value; UserSpeakEvent += new userspeak(UserSpeakEventFunction); if (UserEnterEvent != null) { UserEnterEvent(new string[] { value }.ToList()); } UserEnterEvent += new userenter(UserEnterEventFunction); Userspeaklist.Add(value, UserWhisperFunction); return Userspeaklist.Keys.ToList(); } public void Speak(string value) { if (UserSpeakEvent != null) { UserSpeakEvent(this.Name, value); } } public void Whisper(string name, string value) { if (!Userspeaklist.Keys.Contains(name)) { return; } Userspeaklist[name](name,value); } public void Leave() { UserEnterEvent -= UserEnterEventFunction; UserSpeakEvent -= UserSpeakEventFunction; Userspeaklist.Remove(this.Name); //退出的地方没实现,但是方法跟添加,说话一样。 QQback.UserLeave(this.Name); } void UserEnterEventFunction(List<