WCF双工,实现简单的聊天程序,开他娘的源!!

2020-02-14 21:22栏目:竞技宝app
TAG:

wcfDLL的接口

usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Runtime.Serialization;usingSystem.ServiceModel;usingSystem.Text;namespaceCoreDll{//NOTE:Youcanusethe"Rename"commandonthe"Refactor"menutochangetheinterfacename"IService1"inbothcodeandconfigfiletogether.[ServiceContract(SessionMode=SessionMode.Required,CallbackContract=typeof(IQQSback))]publicinterfaceIQQS{[OperationContract(IsOneWay=true,IsInitiating=true,IsTerminating=false)]voidSpeak(stringvalue);[OperationContract(IsOneWay=true,IsInitiating=true,IsTerminating=false)]voidWhisper(stringname,stringvalue);[OperationContract(IsOneWay=true,IsInitiating=true,IsTerminating=false)]voidLeave();[OperationContract(IsOneWay=false,IsInitiating=true,IsTerminating=false)]Liststringjoin(stringvalue);}interfaceIQQSback{[OperationContract(IsOneWay=true)]voidReceive(stringsenderName,stringmessage);[OperationContract(IsOneWay=true)]voidReceiveWhisper(stringsenderName,stringmessage);[OperationContract(IsOneWay=true)]voidUserEnter(Liststringname);[OperationContract(IsOneWay=true)]voidUserLeave(stringname);}}

wcf的实现

usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Runtime.Serialization;usingSystem.ServiceModel;usingSystem.Text;namespaceCoreDll{[ServiceBehavior(InstanceContextMode=InstanceContextMode.PerSession,ConcurrencyMode=ConcurrencyMode.Multiple)]publicclassService1:IQQS{IQQSbackQQback=OperationContext.Current.GetCallbackChannelIQQSback();delegatevoiduserenter(Liststringusername);staticeventuserenterUserEnterEvent;delegatevoiduserspeak(stringname,stringcontent);staticeventuserspeakUserSpeakEvent;delegatevoiduserWhisper(stringname,stringcontent);staticSortedListstring,userWhisperUserspeaklist=newSortedListstring,userWhisper();stringName;publicListstringjoin(stringvalue){if(Userspeaklist.Keys.Contains(value)){returnnewstring[]{"已经有此用户"}.ToList();}this.Name=value;UserSpeakEvent+=newuserspeak(UserSpeakEventFunction);if(UserEnterEvent!=null){UserEnterEvent(newstring[]{value}.ToList());}UserEnterEvent+=newuserenter(UserEnterEventFunction);Userspeaklist.Add(value,UserWhisperFunction);returnUserspeaklist.Keys.ToList();}publicvoidSpeak(stringvalue){if(UserSpeakEvent!=null){UserSpeakEvent(this.Name,value);}}publicvoidWhisper(stringname,stringvalue){if(!Userspeaklist.Keys.Contains(name)){return;}Userspeaklist[name](name,value);}publicvoidLeave(){UserEnterEvent-=UserEnterEventFunction;UserSpeakEvent-=UserSpeakEventFunction;Userspeaklist.Remove(this.Name);//退出的地方没实现,但是方法跟添加,说话一样。QQback.UserLeave(this.Name);}voidUserEnterEventFunction(Liststringusername){this.QQback.UserEnter(username);}voidUserSpeakEventFunction(stringname,stringcontent){QQback.Receive(name,content);}voidUserWhisperFunction(stringname,stringcontent){QQback.ReceiveWhisper(name,content);}}}

宿主

usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.ServiceModel;namespaceWCFsuzhu{classProgram{staticvoidMain(string[]args){Console.WriteLine("正在启动服务");ServiceHostsh=newServiceHost(typeof(CoreDll.Service1));sh.Open();Console.WriteLine("启动服务成功!回车键退出。");Console.ReadLine();sh.Abort();sh.Close();}}}

版权声明:本文由龙竞技官网发布于竞技宝app,转载请注明出处:WCF双工,实现简单的聊天程序,开他娘的源!!