最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

利用WCF雙工模式實現(xiàn)即時通訊

 更新時間:2016年09月22日 14:50:32   作者:會長  
這篇文章主要介紹了利用WCF雙工模式實現(xiàn)即時通訊的相關(guān)資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下

概述 

WCF陸陸續(xù)續(xù)也用過多次,但每次都是淺嘗輒止,以將夠解決問題為王道,這幾天稍閑,特尋了些資料看,昨晚嘗試使用WCF的雙工模式實現(xiàn)了一個簡單的即時通訊程序,通過服務(wù)端轉(zhuǎn)發(fā)實現(xiàn)客戶端之間的通訊。這只是個Demo,沒有考慮異常處理和性能問題。解決方案結(jié)構(gòu)如下:

 

契約

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);
 }
} 

該接口定義了三個行為,分別是:

 •注冊
 •注銷
 •發(fā)消息 

其中,在特性[ServiceContract(CallbackContract = typeof(ICallBack))]中指定了用于服務(wù)端回調(diào)客戶方法的契約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只有一個實體,用來表示已經(jīng)注冊用戶的Id和對應(yīng)的回調(diào)契約的具體實現(xiàn)的實例:

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; }
 }
} 

契約的實現(xiàn)代碼

 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實體包含了用戶信息和實現(xiàn)了回調(diào)契約的一個實例對象。 

UnRegister方法用來把一個Client從列表中移除,模擬注銷行為。 

SendMessage方法用來發(fā)送消息,第一個參數(shù)是發(fā)送者的Id,第二個參數(shù)是消息接受者的Id,第三個參數(shù)是發(fā)送內(nèi)容,該方法先將消息在服務(wù)端打印出來,然后再回調(diào)消息接收者對應(yīng)的回調(diào)契約的具體實現(xiàn)類的實例對象的Notice方法以達(dá)到服務(wù)端向客戶端發(fā)送消息的目的。 

宿主

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();
  }
 }
 }
} 

宿主是一個控制臺應(yīng)用程序,使用的綁定類型為NetTcpBinding,端口是華安的華府的終生代號。 

客戶端代碼 

實現(xiàn)回調(diào)接口

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);
 }
 }
} 

模擬注冊,發(fā)消息和注銷

 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發(fā)送消息;再起一個進(jìn)程test.exe Ross Joey表示Ross注冊,要給他的朋友Joey發(fā)送消息。進(jìn)程啟動后輸入一些字符按回車即發(fā)送至了對方,輸入q回車注銷并退出程序。如下圖所示:

Ross:


Joey:


服務(wù)端:

參考資料

 •無廢話WCF入門教程五[WCF的通信模式]
 •同事 @麥楓 的代碼
 •《WCF全面解析》 

后記 

這僅僅是個Demo,在實際項目中如果同時在線人數(shù)非常多,這樣做的性能是否可行還需進(jìn)一步對WCF雙工模式的工作方式進(jìn)行深入學(xué)習(xí)。 

解決方案下載地址:WCFDemo

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • C#中跨線程訪問控件的實現(xiàn)方法

    C#中跨線程訪問控件的實現(xiàn)方法

    C#中不允許跨線程直接訪問界面控件,即一個線程中如主線程創(chuàng)建的控件不允許被其他線程例如子線程直接訪問,在一個線程中設(shè)置其他線程所有的控件屬性通常有兩種方法,本文將詳細(xì)的給大家介紹一下,需要的朋友可以參考下
    2024-12-12
  • C#使用Aspose.Cells創(chuàng)建和讀取Excel文件

    C#使用Aspose.Cells創(chuàng)建和讀取Excel文件

    這篇文章主要為大家詳細(xì)介紹了C#使用Aspose.Cells創(chuàng)建和讀取Excel文件,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-10-10
  • Unity的IPostprocessBuild實用案例深入解析

    Unity的IPostprocessBuild實用案例深入解析

    這篇文章主要為大家介紹了Unity的IPostprocessBuild實用案例深入解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-05-05
  • C#中的日期時間比較和格式化的方法

    C#中的日期時間比較和格式化的方法

    本文將介紹C#中常用的日期時間比較方法(CompareTo、Equals和比較運算符)以及日期時間格式化方法(ToString、自定義格式字符串和標(biāo)準(zhǔn)格式),具有一定的參考價值,感興趣的可以了解一下
    2023-10-10
  • C# partial關(guān)鍵字說明

    C# partial關(guān)鍵字說明

    C# 中可以將類、結(jié)構(gòu)或接口的定義拆分到兩個或多個源文件中,在類聲明前添加partial關(guān)鍵字即可,通過本文給大家介紹C# partial關(guān)鍵字說明,需要的朋友參考下
    2016-02-02
  • 詳解WPF中值轉(zhuǎn)換器的使用方法

    詳解WPF中值轉(zhuǎn)換器的使用方法

    在WPF(Windows Presentation Foundation)中,值轉(zhuǎn)換器(Value Converter)是一種機(jī)制,允許你在綁定時轉(zhuǎn)換綁定源和綁定目標(biāo)之間的值,本文給大家介紹了WPF中值轉(zhuǎn)換器的使用方法,需要的朋友可以參考下
    2024-02-02
  • C#禁用雙擊窗體圖標(biāo)關(guān)閉窗體的方法

    C#禁用雙擊窗體圖標(biāo)關(guān)閉窗體的方法

    這篇文章主要介紹了C#禁用雙擊窗體圖標(biāo)關(guān)閉窗體的方法,通過對窗體參數(shù)的簡單設(shè)置實現(xiàn)C#禁用雙擊窗體圖標(biāo)關(guān)閉窗體的功能,非常簡單實用,需要的朋友可以參考下
    2015-08-08
  • C#動態(tài)生成DropDownList執(zhí)行失敗原因分析

    C#動態(tài)生成DropDownList執(zhí)行失敗原因分析

    這篇文章主要介紹了C#動態(tài)生成DropDownList執(zhí)行失敗原因分析,以一個實例形式分析了C#動態(tài)生成DropDownList的相關(guān)注意要點與使用技巧,具有一定參考借鑒價值,需要的朋友可以參考下
    2015-03-03
  • C#實現(xiàn)密碼驗證與輸錯密碼賬戶鎖定

    C#實現(xiàn)密碼驗證與輸錯密碼賬戶鎖定

    這篇文章介紹了C#實現(xiàn)密碼驗證與輸錯密碼賬戶鎖定的方法,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-04-04
  • C# 的關(guān)鍵字詳細(xì)介紹

    C# 的關(guān)鍵字詳細(xì)介紹

    本文將詳細(xì)介紹C#關(guān)鍵字的應(yīng)用,可供有需要的朋友參考
    2012-11-11

最新評論

卢氏县| 深圳市| 固阳县| 新安县| 福鼎市| 武定县| 库伦旗| 青川县| 莫力| 虹口区| 桑植县| 囊谦县| 磐石市| 恭城| 棋牌| 化德县| 邯郸县| 黄山市| 沁源县| 湾仔区| 夏河县| 天气| 鄯善县| 云浮市| 涡阳县| 鄂托克旗| 屏边| 灵石县| 英山县| 冀州市| 东莞市| 鄂托克旗| 卢龙县| 泰顺县| 平乡县| 池州市| 马山县| 青河县| 新巴尔虎左旗| 阿荣旗| 婺源县|