本篇文章將經過一個實例實現對WCF中針對服務端以及客戶端是否掉線進行判斷;若掉線時服務器或客戶端又在線時將實現自動重連;將經過WCF的雙工知識以及相應的心跳包來實現此功能;服務器
首先了解一下本實例的一個分層架構圖;相信瞭解WCF對這個分層法確定都很瞭解;其中Client爲客戶端,Interface爲契約層,Service則是實現契約的服務層;架構
接下來咱們將對各層進行一個簡單的講解;方便你們理解實例的實現方式;tcp
首先咱們瞭解契約層Interface;由於咱們利用WCF中的雙工知識,因此此處咱們定義的兩個接口,其中ICallback爲回調接口;IPushMessage上面定義的回調;咱們發現回調接口上並無定義ServiceContract; 實際上是WCF當一個接口被定義成回調接口時已經默認了;學習
using System.ServiceModel; namespace Interface { [ServiceContract(SessionMode=SessionMode.Required,CallbackContract=typeof(ICallback))] public interface IPushMessage { [OperationContract(IsOneWay = true)] void Login(string UID); [OperationContract(IsOneWay = true)] void Update(string UID); [OperationContract(IsOneWay = true)] void Leave(string UID); } }
using System.ServiceModel; namespace Interface { public interface ICallback { [OperationContract(IsOneWay = true)] void ShowMessage(string msg); } }
接着咱們瞭解一下實現契約的服務層Service;此處包括實現接口IPushMessage的服務內容,還定義一個類WcfCleint;在PushMessageServer服務內裏咱們定義的一個靜態成員Dictionary<string,WcfCleint>用於記錄咱們客戶端上下線的操做;定義的一個lockObje用來鎖定每一個操做,在最後咱們還定義的一個定時器用來判斷客戶端用戶是否脫機了;其中方法Update()咱們就實現的雙工中的回調;ui
using System.ServiceModel; using Interface; namespace Service { public class PushMessageServer:IPushMessage { static Dictionary<string, WcfCleint> dit_callback = new Dictionary<string, WcfCleint>(); private static Object lockObje = new Object(); public void Login(string UID) { Console.WriteLine("用戶:" + UID + "上線"); lock (lockObje) { dit_callback.Add(UID, new WcfCleint(OperationContext.Current.GetCallbackChannel<ICallback>(), DateTime.Now)); } } public void Update(string UID) { ICallback callback = OperationContext.Current.GetCallbackChannel<ICallback>(); Console.WriteLine("用戶:" + UID + "心跳更新!"); lock (lockObje) { if (dit_callback.ContainsKey(UID)) { dit_callback.Remove(UID); } dit_callback.Add(UID, new WcfCleint(OperationContext.Current.GetCallbackChannel<ICallback>(), DateTime.Now)); callback.ShowMessage(UID); } } public void Leave(string UID) { Console.WriteLine("用戶:" + UID + "退出!"); lock (lockObje) { dit_callback.Remove(UID); } } static System.Timers.Timer timer1; public static void StartListenClients() { timer1 = new System.Timers.Timer(); timer1.Interval = 500; timer1.Elapsed += new System.Timers.ElapsedEventHandler(time_EventArgs); timer1.Start(); } static void time_EventArgs(object sender, System.Timers.ElapsedEventArgs e) { lock (lockObje) { foreach (string key in new List<string>(dit_callback.Keys)) { if (dit_callback[key].NowdateTime.AddSeconds(5) < DateTime.Now) { dit_callback.Remove(key); Console.WriteLine("脫機用戶" + key); } } } } } }
接着咱們簡單看一下WcfCleint的代碼:this
using Interface; using System.ServiceModel; namespace Service { public class WcfCleint { public DateTime NowdateTime { get; set; } public ICallback callbackHandler { get; set; } public WcfCleint(ICallback callback, DateTime nowTime) { this.callbackHandler = callback; this.NowdateTime = nowTime; } } }
服務端的配置文件信息,此處咱們採用net.tcp方式:spa
<?xml version="1.0" encoding="utf-8" ?> <configuration> <system.serviceModel> <behaviors> <serviceBehaviors> <behavior name="exposeExceptionDetail"> <serviceDebug includeExceptionDetailInFaults="true"/> </behavior> </serviceBehaviors> </behaviors> <services> <service name="Service.PushMessageServer" behaviorConfiguration="exposeExceptionDetail"> <endpoint address="net.tcp://127.0.0.1:3721/PushMessageServer" binding="netTcpBinding" contract="Interface.IPushMessage"/> </service> </services> </system.serviceModel> </configuration>
服務端的運行入口代碼,經過一個線程不斷的運行:線程
using System.Threading; using System.ServiceModel.Description; namespace Service { class Program { static void Main(string[] args) { using (ServiceHost host = new ServiceHost(typeof(PushMessageServer))) { host.Open(); Console.WriteLine("WCF心跳包實現開始監聽"); PushMessageServer.StartListenClients(); int i = 0; while (true) { System.Threading.Thread.Sleep(2000); i++; } Console.Read(); host.Abort(); host.Close(); } } } }
接着瞭解客戶端層的狀況;首先是一個實現的回調接口的內容類;code
using Interface; namespace Client { public class CallServer:ICallback { public void ShowMessage(string msg) { Console.WriteLine("服務器正在工做中;" + msg); } } }
客戶端的配置信息以下:server
<?xml version="1.0" encoding="utf-8" ?> <configuration> <system.serviceModel> <client> <endpoint name="pushmessageserver" address="net.tcp://127.0.0.1:3721/PushMessageServer" binding="netTcpBinding" contract="Interface.IPushMessage"/> </client> </system.serviceModel> </configuration>
客戶端的運行入口代碼以下,它會判斷服務器是否掉線,若掉線會試着重連:
using System.ServiceModel; using Interface; using System.ServiceModel.Channels; using System.Threading; namespace Client { class Program { static void Main(string[] args) { InstanceContext callback = new InstanceContext(new CallServer()); using (DuplexChannelFactory<IPushMessage> channelFactory = new DuplexChannelFactory<IPushMessage>(callback, "pushmessageserver")) { IPushMessage proxy = channelFactory.CreateChannel(); using (proxy as IDisposable) { string UID = "踏浪帥"; proxy.Login(UID); while (true) { Thread.Sleep(3000); try { proxy.Update(UID); } catch { try { Console.WriteLine("正在重連"); proxy = channelFactory.CreateChannel(); proxy.Login(UID); } catch { Console.WriteLine("重連異常"); } } } } } Console.Read(); } } }
代碼的內容就如上,接着咱們簡單看一下運行的效果:
1:首先運行服務端,讓其處於監聽
2:接着運行客戶端,客戶端會顯示服務器正在工做中以及當前的帳號
3:當客戶端上線後,服務端就發現客戶端用戶,並顯示出來,不停的進行判斷
4:當咱們把服務端停掉之後,此時服務是不工做的,咱們能夠看到客戶端就會不停的嘗試着重連服務端;
5:當咱們從新把服務端打開之後,服務端會自運發現正在運行的客戶端信息;
6:上面咱們從新打開服務端,咱們能夠在客戶端發現它重鏈接服務端成功並顯示以下:
7:接着咱們將把客戶端關閉;咱們會在服務端發現它提示客戶端的用戶脫機了;
8:此時服務端仍是不停的監聽中,咱們再把客戶端打開,服務端將會接着顯示用戶;
實例爲本人學習WCF知識所寫,其中可能存在不足或錯誤,歡迎你們指正;如果本文對你有幫助別忘記點右下角幫忙推薦,畢竟有你們的支持纔是咱們寫做的動力;