概述
WCF陸陸續續也用過多次,但每次都是淺嘗輒止,以將夠解決問題為王道,這幾天稍閑,特尋了些資料看,昨晚嘗試使用WCF的雙工模式實現了一個簡單的即時通訊程序,通過服務端轉發實現客戶端之間的通訊。這只是個Demo,沒有考慮異常處理和性能問題。解決方案結構如下:
契約
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); }}
該接口定義了三個行為,分別是:
•注冊
•注銷
•發消息
其中,在特性[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實體包含了用戶信息和實現了回調契約的一個實例對象。
UnRegister方法用來把一個Client從列表中移除,模擬注銷行為。
SendMessage方法用來發送消息,第一個參數是發送者的Id,第二個參數是消息接受者的Id,第三個參數是發送內容,該方法先將消息在服務端打印出來,然后再回調消息接收者對應的回調契約的具體實現類的實例對象的Notice方法以達到服務端向客戶端發送消息的目的。
宿主
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,端口是華安的華府的終生代號。
客戶端代碼
實現回調接口
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:
服務端:
參考資料
•無廢話WCF入門教程五[WCF的通信模式]
•同事 @麥楓 的代碼
•《WCF全面解析》
后記
這僅僅是個Demo,在實際項目中如果同時在線人數非常多,這樣做的性能是否可行還需進一步對WCF雙工模式的工作方式進行深入學習。
解決方案下載地址:WCFDemo
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林網。
新聞熱點
疑難解答