利用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
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持猪先飞。
相关文章
- 以订票为例简单应用wcf程序,需要的朋友可以参考下...2020-06-25
- 这篇文章主要介绍了C# yield在WCF中的错误使用(一),本文讲解的内容据说是99%的开发人员都有可能犯的错误,需要的朋友可以参考下...2020-06-25
- 这篇文章主要介绍了IIS8支持WCF的更简单的方法,大家可以参考使用...2016-01-27
- 这篇文章主要帮助大家区分WCF与WebService的异同、优势,分为三大方面进行研究学习,感兴趣的小伙伴们可以参考一下...2020-06-25
- 在Winform窗体中调用部署在其他服务器中的WCF服务时,由于调用服务需要一定时延,因此窗体在这段时间一直卡住不能进行其他操作...2020-06-25
WCF如何绑定netTcpBinding寄宿到控制台应用程序详解
这篇文章主要给大家介绍了关于WCF如何绑定netTcpBinding寄宿到控制台应用程序的相关资料,文中通过示例代码介绍的非常详细,对大家学习或者使用WCF具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧...2021-09-22- 这篇文章主要介绍了WCF简单入门图文教程,版本是VS2010版,帮助大家轻松学习了解DataContract、ServiceContract等特性,感兴趣的小伙伴们可以参考一下...2020-06-25
- 这篇文章主要介绍了WCF实现的计算器功能,结合具体实例形式较为详细的分析了WCF实现计算器功能的具体步骤与相关操作技巧,需要的朋友可以参考下...2020-06-25
- IIS8中添加WCF支持几种方法总结,有需要学习的朋友可参考参考...2016-01-27
在WCF数据访问中使用缓存提高Winform字段中文显示速度的方法
这篇文章主要介绍了在WCF数据访问中使用缓存提高Winform字段中文显示速度的方法,是非常实用的功能,需要的朋友可以参考下...2020-06-25- 这篇文章主要为大家详细介绍了C#创建WCF服务控制台应用程序,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2020-06-25
- 这篇文章主要介绍了利用WCF双工模式实现即时通讯的相关资料,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2020-06-25
- 这篇文章主要介绍了C# yield在WCF中的错误使用(二),本文讲解的内容据说是99%的开发人员都有可能犯的错误,需要的朋友可以参考下...2020-06-25
- 这篇文章主要介绍了WCF如何使用动态代理精简代码架构,帮助大家更好的理解和学习使用c#,感兴趣的朋友可以了解下...2021-03-12
解析Silverlight调用WCF/Rest异常的解决方法
本篇文章对Silverlight调用WCF/Rest异常的解决方法进行了详细的分析介绍,需要的朋友参考下...2020-06-25- 下面小编就为大家分享一篇WCF实现进程间管道通信Demo,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2020-06-25
- 这篇文章主要给大家介绍了关于WCF中使用nettcp协议进行通讯的相关资料,文中通过示例代码介绍的非常详细,对大家学习或者使用WCF具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧...2021-09-22
- 这篇文章主要为大家详细介绍了.net WCF简单实例,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2021-09-22
- 这篇文章给大家总结了C#动态调用WCF接口的两种方法,大家可以根据自己的需求选择对应的方式,下面来一起看看。...2020-06-25
WCF Linux 客户端程序创建(通过 WCF dotnet-svcutil 工具)
1. 工具介绍 (WCF) dotnet-svcutil 工具是一种 .NET 工具,此工具从网络位置上的 Web 服务中或从 WSDL 文件中检索元数据,并 生成包含访问 Web 服务操作的客户端代理方...2020-06-30