国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C# > 正文

利用WCF雙工模式實現即時通訊

2020-01-24 00:59:18
字體:
來源:轉載
供稿:網友

概述 

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

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林網。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 贵德县| 株洲县| 长子县| 宽甸| 德格县| 莒南县| 永丰县| 安丘市| 石门县| 抚松县| 富民县| 桓仁| 侯马市| 许昌县| 长寿区| 宁国市| 合阳县| 哈巴河县| 沈丘县| 邻水| 昭通市| 永川市| 涡阳县| 高碑店市| 盐池县| 定日县| 南江县| 锡林浩特市| 福建省| 和静县| 南雄市| 五台县| 夹江县| 浙江省| 三台县| 洛南县| 普兰县| 龙川县| 六安市| 同江市| 乌拉特前旗|