利用WCF雙工模式實(shí)現(xiàn)即時(shí)通訊
概述
WCF陸陸續(xù)續(xù)也用過多次,但每次都是淺嘗輒止,以將夠解決問題為王道,這幾天稍閑,特尋了些資料看,昨晚嘗試使用WCF的雙工模式實(shí)現(xiàn)了一個(gè)簡單的即時(shí)通訊程序,通過服務(wù)端轉(zhuǎn)發(fā)實(shí)現(xiàn)客戶端之間的通訊。這只是個(gè)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);
}
}
該接口定義了三個(gè)行為,分別是:
•注冊
•注銷
•發(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);
}
}
實(shí)體
本Demo只有一個(gè)實(shí)體,用來表示已經(jīng)注冊用戶的Id和對應(yīng)的回調(diào)契約的具體實(shí)現(xiàn)的實(shí)例:
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; }
}
}
契約的實(shí)現(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實(shí)體加入到一個(gè)列表中,模擬注冊行為,Clinet實(shí)體包含了用戶信息和實(shí)現(xiàn)了回調(diào)契約的一個(gè)實(shí)例對象。
UnRegister方法用來把一個(gè)Client從列表中移除,模擬注銷行為。
SendMessage方法用來發(fā)送消息,第一個(gè)參數(shù)是發(fā)送者的Id,第二個(gè)參數(shù)是消息接受者的Id,第三個(gè)參數(shù)是發(fā)送內(nèi)容,該方法先將消息在服務(wù)端打印出來,然后再回調(diào)消息接收者對應(yīng)的回調(diào)契約的具體實(shí)現(xiàn)類的實(shí)例對象的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();
}
}
}
}
宿主是一個(gè)控制臺(tái)應(yīng)用程序,使用的綁定類型為NetTcpBinding,端口是華安的華府的終生代號(hào)。
客戶端代碼
實(shí)現(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中運(yùn)行test.exe Joey Ross表示Joey注冊,要給他的朋友Ross發(fā)送消息;再起一個(gè)進(jìn)程test.exe Ross Joey表示Ross注冊,要給他的朋友Joey發(fā)送消息。進(jìn)程啟動(dòng)后輸入一些字符按回車即發(fā)送至了對方,輸入q回車注銷并退出程序。如下圖所示:
Ross:

Joey:

服務(wù)端:

參考資料
•無廢話WCF入門教程五[WCF的通信模式]
•同事 @麥楓 的代碼
•《WCF全面解析》
后記
這僅僅是個(gè)Demo,在實(shí)際項(xiàng)目中如果同時(shí)在線人數(shù)非常多,這樣做的性能是否可行還需進(jìn)一步對WCF雙工模式的工作方式進(jìn)行深入學(xué)習(xí)。
解決方案下載地址:WCFDemo
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
C#使用Aspose.Cells創(chuàng)建和讀取Excel文件
這篇文章主要為大家詳細(xì)介紹了C#使用Aspose.Cells創(chuàng)建和讀取Excel文件,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-10-10
Unity的IPostprocessBuild實(shí)用案例深入解析
這篇文章主要為大家介紹了Unity的IPostprocessBuild實(shí)用案例深入解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-05-05
C#動(dòng)態(tài)生成DropDownList執(zhí)行失敗原因分析
這篇文章主要介紹了C#動(dòng)態(tài)生成DropDownList執(zhí)行失敗原因分析,以一個(gè)實(shí)例形式分析了C#動(dòng)態(tài)生成DropDownList的相關(guān)注意要點(diǎn)與使用技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-03-03
C#實(shí)現(xiàn)密碼驗(yàn)證與輸錯(cuò)密碼賬戶鎖定
這篇文章介紹了C#實(shí)現(xiàn)密碼驗(yàn)證與輸錯(cuò)密碼賬戶鎖定的方法,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-04-04

