利用WCF双工模式实现即时通讯

 更新时间:2020年6月25日 11:22  点击:1570

概述 

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

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持猪先飞。

[!--infotagslink--]

相关文章