應用WCF雙工形式完成即時通信。本站提示廣大學習愛好者:(應用WCF雙工形式完成即時通信)文章只能為提供參考,不一定能成為您想要的結果。以下是應用WCF雙工形式完成即時通信正文
概述
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
以上就是本文的全體內容,願望對年夜家的進修有所贊助,也願望年夜家多多支撐。