概述

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:



服务端:

参考资料

后记

这仅仅是个Demo,在实际项目中如果同时在线人数非常多,这样做的性能是否可行还需进一步对WCF双工模式的工作方式进行深入学习。

利用WCF双工模式实现即时通讯的更多相关文章

  1. [SignalR]SignalR与WCF双工模式结合实现服务端数据直推浏览器端

    原文:[SignalR]SignalR与WCF双工模式结合实现服务端数据直推浏览器端 之前开发基于WinForm监控的软件,服务端基于Wcf实现,里面涉及双工模式,在客户端里面,采用心跳包机制保持与服 ...

  2. JAVA与.NET的相互调用——利用JNBridge桥接模式实现远程通讯

    分布式开发的历史 利用Remote方式调用远程对象实现服务器与客户端之间通讯是一种常用的网络开发方式,在.NET与JAVA开发当中,对Remote远程对象早已有着足够的支持(对Remote远程对象调用 ...

  3. WCF 双工模式

    WCF之消息模式分为:1.请求/答复模式2.单向模式3.双工模式 其中,请求/答复模式,在博文: WCF 入门教程一(动手新建第一个WCF程序并部署) WCF 入门教程二 中进行了详细介绍,此处将主要 ...

  4. android环境下的即时通讯

    首先了解一下即时通信的概念.通过消息通道 传输消息对象,一个账号发往另外一账号,只要账号在线,可以即时获取到消息,这就是最简单的即使通讯.消息通道可由TCP/IP UDP实现.通俗讲就是把一个人要发送 ...

  5. 利用WCF的双工通讯实现一个简单的心跳监控系统 z

    利用WCF的双工通讯实现一个简单的心跳监控系统 http://www.cnblogs.com/zuowj/p/5761011.html 何为心跳监控系统? 故名思义,就是监控某个或某些个程序的运行状态 ...

  6. 利用WCF的双工通讯实现一个简单的心跳监控系统

    何为心跳监控系统? 故名思义,就是监控某个或某些个程序的运行状态,就好比医院里面的心跳监视仪一样,能够随时显示病人的心跳情况. 心跳监控的目的是什么? 与医院里面的心跳监视仪目的类似,监控程序运行状态 ...

  7. WCF学习之旅—TCP双工模式(二十一)

    WCF学习之旅—请求与答复模式和单向模式(十九) WCF学习之旅—HTTP双工模式(二十) 五.TCP双工模式 上一篇文章中我们学习了HTTP的双工模式,我们今天就学习一下TCP的双工模式. 在一个基 ...

  8. WCF双工通讯以及客户端间的间接通讯

    由于学习计划安排不当,对WCF的认知一直停滞不前,最近工作上又用回了WCF,重拾一下,看到蒋老师介绍双工通讯的博文,实践一下,积累一下.原想着WCF的双工通讯就是原本的客户端能调用服务端的方法之余,服 ...

  9. WCF学习之旅—HTTP双工模式(二十)

    WCF学习之旅—请求与答复模式和单向模式(十九) 四.HTTP双工模式 双工模式建立在上文所实现的两种模式的基础之上,实现客户端与服务端相互调用:前面介绍的两种方法只是在客户端调用服务端的方法,然后服 ...

随机推荐

  1. 初学者看过来之JSON入门

    1. 什么是JSON JSON---Javascript Object Notation,前两个单词大家应该都认识,最后一个notation,是"记号.标记法"的意思,连在一起,便 ...

  2. 玩转spring boot——结合redis

    一.准备工作 下载redis的windows版zip包:https://github.com/MSOpenTech/redis/releases 运行redis-server.exe程序 出现黑色窗口 ...

  3. 使用HTML5的cavas实现的一个画板

    <!DOCTYPE html><html><head> <meta charset="utf-8"> <meta http-e ...

  4. 港真,到底应该选择OA还是BPM?

    越来越多企业意识到流程管理的重要性,但是,选择OA还是BPM,却让他们产生了选择困难症. 一方面,企业皆注重流程的高效运转,最好内外部的业务都能用一个系统来解决.所有流程一天就能上线什么的,那就更好啦 ...

  5. SQL-日期函数

    GETDATE() :取得当前日期时间 DATEADD (datepart , number, date ),计算增加以后的日期.参数date为待计算的日期:参数number为增量:参数datepar ...

  6. Firebug中调试中的js脚本中中文内容显示为乱码

    Firebug中调试中的js脚本中中文内容显示为乱码 设置 页面 UFT-8 编码没用, 解决方法:点击 "Firebug"工具栏 中的"选项"---" ...

  7. Hello Jexus

    一.关于 CentOS CentOS(Community Enterprise Operating System)是Linux发行版之一,它是来自于Red Hat Enterprise Linux依照 ...

  8. linux下mono播放PCM音频

         测试环境: Ubuntu 14 MonoDevelop CodeBlocks 1.建立一个共享库(shared library) 这里用到了linux下的音频播放库,alsa-lib. al ...

  9. 如何使用RobotFramework编写好的测试用例

    如何使用Robot Framework编写优秀的测试用例 概述 命名 测试套件命名 测试用例命名 关键字命名 setup和teardown的命名 文档 测试套件文档 测试用例文档 用户关键字文档 测试 ...

  10. 《AngularJS深度剖析与最佳实践》简介

    由于年末将至,前阵子一直忙于工作的事务,不得已暂停了微信订阅号的更新,我将会在后续的时间里尽快的继续为大家推送更多的博文.毕竟一个人的力量微薄,精力有限,希望大家能理解,仍然能一如既往的关注和支持sh ...