若掉線時服務(wù)器或客戶端又在線時將實現(xiàn)自動重連;將通過WCF的雙工知識以及相應(yīng)的心跳包來實現(xiàn)此功能;
首先了解一下本實例的一個分層架構(gòu)圖;相信了解WCF對這個分層法肯定都很了解;其中Client為客戶端,Interface為契約層,Service則是實現(xiàn)契約的服務(wù)層;
接下來我們將對各層進行一個簡單的講解
首先我們了解契約層Interface;因為我們利用WCF中的雙工知識,所以此處我們定義的兩個接口,其中ICallback為回調(diào)接口;IPushMessage上面定義的回調(diào);我們發(fā)現(xiàn)回調(diào)接口上并沒有定義ServiceContract; 其實是WCF當一個接口被定義成回調(diào)接口時已經(jīng)默認了;
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); } }
接著我們了解一下實現(xiàn)契約的服務(wù)層Service;此處包括實現(xiàn)接口IPushMessage的服務(wù)內(nèi)容,還定義一個類WcfCleint;在PushMessageServer服務(wù)內(nèi)里我們定義的一個靜態(tài)成員Dictionary<string,WcfCleint>用于記錄我們客戶端上下線的操作;定義的一個lockObje用來鎖定每個操作,在最后我們還定義的一個定時器用來判斷客戶端用戶是否脫機了;其中方法Update()我們就實現(xiàn)的雙工中的回調(diào);
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的代碼:
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; } } }
服務(wù)端的配置文件信息,此處我們采用net.tcp方式:
<?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>
服務(wù)端的運行入口代碼,通過一個線程不斷的運行:
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心跳包實現(xiàn)開始監(jiān)聽"); PushMessageServer.StartListenClients(); int i = 0; while (true) { System.Threading.Thread.Sleep(2000); i++; } Console.Read(); host.Abort(); host.Close(); } } } }
接著了解客戶端層的情況;首先是一個實現(xiàn)的回調(diào)接口的內(nèi)容類;
using Interface; namespace Client { public class CallServer:ICallback { public void ShowMessage(string msg) { Console.WriteLine("服務(wù)器正在工作中;" + msg); } } }
客戶端的配置信息如下:
<?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>
客戶端的運行入口代碼如下,它會判斷服務(wù)器是否掉線,若掉線會試著重連:
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:首先運行服務(wù)端,讓其處于監(jiān)聽
2:接著運行客戶端,客戶端會顯示服務(wù)器正在工作中以及當前的賬號
3:當客戶端上線后,服務(wù)端就發(fā)現(xiàn)客戶端用戶,并顯示出來,不停的進行判斷
4:當我們把服務(wù)端停掉以后,此時服務(wù)是不工作的,我們可以看到客戶端就會不停的嘗試著重連服務(wù)端;
5:當我們重新把服務(wù)端打開以后,服務(wù)端會自運發(fā)現(xiàn)正在運行的客戶端信息;
6:上面我們重新打開服務(wù)端,我們可以在客戶端發(fā)現(xiàn)它重連接服務(wù)端成功并顯示如下:
7:接著我們將把客戶端關(guān)閉;我們會在服務(wù)端發(fā)現(xiàn)它提示客戶端的用戶脫機了;
8:此時服務(wù)端還是不停的監(jiān)聽中,我們再把客戶端打開,服務(wù)端將會接著顯示用戶;