WCF陸陸續續也用過屢次,但每次都是淺嘗輒止,以將夠解決問題爲王道,這幾天稍閒,特尋了些資料看,昨晚嘗試使用WCF的雙工模式實現了一個簡單的即時通信程序,經過服務端轉發實現客戶端之間的通信。這只是個Demo,沒有考慮異常處理和性能問題。解決方案結構以下:html
using System; using System.Collections.Generic; using System.Linq; using System.ServiceModel; using System.Text; using System.Threading.Tasks; namespace Service.Interface { [ServiceContract(CallbackContract = typeof(ICallBack))] public interface INoticeOperator { [OperationContract] void Register(String id); [OperationContract] void UnRegister(String id); [OperationContract] void SendMessage(String from, String to, String message); } }
該接口定義了三個行爲,分別是:tcp
其中,在特性[ServiceContract(CallbackContract = typeof(ICallBack))]
中指定了用於服務端回調客戶方法的契約ICallBack
,其定義以下:性能
using System; using System.Collections.Generic; using System.Linq; using System.ServiceModel; using System.Text; using System.Threading.Tasks; namespace Service.Interface { public interface ICallBack { [OperationContract(IsOneWay = true)] void Notice(String message); } }
本Demo只有一個實體,用來表示已經註冊用戶的Id和對應的回調契約的具體實現的實例:學習
using Service.Interface; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Models { public class Client { public String Id { get; set; } public ICallBack CallBack { get; set; } } }
using Models; using Service.Interface; using System; using System.Collections.Generic; using System.Linq; using System.ServiceModel; using System.Text; using System.Threading.Tasks; namespace Service { public class NoticeOperator : INoticeOperator { private static List<Client> clientList = new List<Client>(); public void Register(string id) { Console.WriteLine("register:" + id); ICallBack callBack = OperationContext.Current.GetCallbackChannel<ICallBack>(); clientList.Add(new Client() { Id = id, CallBack = callBack }); } public void UnRegister(string id) { Console.WriteLine("unRegister:" + id); Client client = clientList.Find(c => c.Id == id); if (client != null) { clientList.Remove(client); } } public void SendMessage(string from, string to, string message) { Client client = clientList.Find(c => c.Id == to); if (client != null) { String longMessage = String.Format("message from {0} to {1} at {2} : {3}", from, to, DateTime.Now.ToString("HH:mm:ss"), message); Console.WriteLine(longMessage); client.CallBack.Notice(longMessage); } } } }
Register方法用來把Client實體加入到一個列表中,模擬註冊行爲,Clinet實體包含了用戶信息和實現了回調契約的一個實例對象。spa
UnRegister方法用來把一個Client從列表中移除,模擬註銷行爲。code
SendMessage方法用來發送消息,第一個參數是發送者的Id,第二個參數是消息接受者的Id,第三個參數是發送內容,該方法先將消息在服務端打印出來,而後再回調消息接收者對應的回調契約的具體實現類的實例對象的Notice方法以達到服務端向客戶端發送消息的目的。orm
using Service; using Service.Interface; using System; using System.Collections.Generic; using System.Linq; using System.ServiceModel; using System.ServiceModel.Description; using System.Text; using System.Threading.Tasks; namespace Hosting { class Program { static void Main(string[] args) { using (ServiceHost host = new ServiceHost(typeof(NoticeOperator))) { host.AddServiceEndpoint(typeof(INoticeOperator), new NetTcpBinding(), "net.tcp://127.0.0.1:9527/NoticeOperator"); host.Opened += (s, e) => Console.WriteLine("service is running..."); host.Open(); Console.ReadLine(); } } } }
宿主是一個控制檯應用程序,使用的綁定類型爲NetTcpBinding
,端口是華安的華府的終生代號。htm
using Service.Interface; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Test { class CallBack : ICallBack { public void Notice(string message) { Console.WriteLine(message); } } }
using Service.Interface; using System; using System.Collections.Generic; using System.Linq; using System.ServiceModel; using System.Text; using System.Threading.Tasks; namespace Test { class Program { static void Main(string[] args) { InstanceContext context = new InstanceContext(new CallBack()); using (ChannelFactory<INoticeOperator> factory = new DuplexChannelFactory<INoticeOperator>(context, new NetTcpBinding(), "net.tcp://127.0.0.1:9527/NoticeOperator")) { INoticeOperator proxy = factory.CreateChannel(); String selfId = args[0]; String friendId = args[1]; proxy.Register(selfId); Console.WriteLine("----------Register------------"); while(true) { String message = Console.ReadLine(); if (message == "q") { proxy.UnRegister(selfId); break; } else { proxy.SendMessage(selfId, friendId, message); } } } } } }
在CMD中運行test.exe Joey Ross
表示Joey註冊,要給他的朋友Ross發送消息;再起一個進程test.exe Ross Joey
表示Ross註冊,要給他的朋友Joey發送消息。進程啓動後輸入一些字符按回車即發送至了對方,輸入q回車註銷並退出程序。以下圖所示:對象
Ross:
Joey:
服務端:
blog
這僅僅是個Demo,在實際項目中若是同時在線人數很是多,這樣作的性能是否可行還需進一步對WCF雙工模式的工做方式進行深刻學習。