配色: 字号:
利用WCF双工模式实现即时通讯
2016-09-23 | 阅:  转:  |  分享 
  
利用WCF双工模式实现即时通讯

概述

WCF陆陆续续也用过多次,但每次都是浅尝辄止,以将够解决问题为王道,这几天稍闲,特寻了些资料看,昨晚尝试使用WCF的双工模式实现了一个简单的即时通讯程序,通过服务端转发实现客户端之间的通讯。这只是个Demo,没有考虑异常处理和性能问题。解决方案结构如下:

契约

usingSystem;

usingSystem.Collections.Generic;

usingSystem.Linq;

usingSystem.ServiceModel;

usingSystem.Text;

usingSystem.Threading.Tasks;



namespaceService.Interface

{

[ServiceContract(CallbackContract=typeof(ICallBack))]

publicinterfaceINoticeOperator

{

[OperationContract]

voidRegister(Stringid);



[OperationContract]

voidUnRegister(Stringid);



[OperationContract]

voidSendMessage(Stringfrom,Stringto,Stringmessage);

}

}

该接口定义了三个行为,分别是:

注册

注销

发消息

其中,在特性[ServiceContract(CallbackContract=typeof(ICallBack))]中指定了用于服务端回调客户方法的契约ICallBack,其定义如下:

usingSystem;

usingSystem.Collections.Generic;

usingSystem.Linq;

usingSystem.ServiceModel;

usingSystem.Text;

usingSystem.Threading.Tasks;



namespaceService.Interface

{

publicinterfaceICallBack

{

[OperationContract(IsOneWay=true)]

voidNotice(Stringmessage);

}

}

实体

本Demo只有一个实体,用来表示已经注册用户的Id和对应的回调契约的具体实现的实例:

usingService.Interface;

usingSystem;

usingSystem.Collections.Generic;

usingSystem.Linq;

usingSystem.Text;

usingSystem.Threading.Tasks;



namespaceModels

{

publicclassClient

{

publicStringId{get;set;}



publicICallBackCallBack{get;set;}

}

}

契约的实现代码

usingModels;

usingService.Interface;

usingSystem;

usingSystem.Collections.Generic;

usingSystem.Linq;

usingSystem.ServiceModel;

usingSystem.Text;

usingSystem.Threading.Tasks;



namespaceService

{

publicclassNoticeOperator:INoticeOperator

{

privatestaticListclientList=newList();



publicvoidRegister(stringid)

{

Console.WriteLine("register:"+id);



ICallBackcallBack=OperationContext.Current.GetCallbackChannel();

clientList.Add(newClient(){Id=id,CallBack=callBack});

}



publicvoidUnRegister(stringid)

{

Console.WriteLine("unRegister:"+id);



Clientclient=clientList.Find(c=>c.Id==id);

if(client!=null)

{

clientList.Remove(client);

}

}



publicvoidSendMessage(stringfrom,stringto,stringmessage)

{

Clientclient=clientList.Find(c=>c.Id==to);

if(client!=null)

{

StringlongMessage=String.Format("messagefrom{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方法以达到服务端向客户端发送消息的目的。

宿主

usingService;

usingService.Interface;

usingSystem;

usingSystem.Collections.Generic;

usingSystem.Linq;

usingSystem.ServiceModel;

usingSystem.ServiceModel.Description;

usingSystem.Text;

usingSystem.Threading.Tasks;



namespaceHosting

{

classProgram

{

staticvoidMain(string[www.hunanwang.net]args)

{

using(ServiceHosthost=newServiceHost(typeof(NoticeOperator)))

{

host.AddServiceEndpoint(typeof(INoticeOperator),newNetTcpBinding(),"net.tcp://127.0.0.1:9527/NoticeOperator");



host.Opened+=(s,e)=>Console.WriteLine("serviceisrunning...");

host.Open();

Console.ReadLine();

}

}

}

}

宿主是一个控制台应用程序,使用的绑定类型为NetTcpBinding,端口是华安的华府的终生代号。

客户端代码

实现回调接口

usingService.Interface;

usingSystem;

usingSystem.Collections.Generic;

usingSystem.Linq;

usingSystem.Text;

usingSystem.Threading.Tasks;



namespaceTest

{

classCallBack:ICallBack

{

publicvoidNotice(stringmessage)

{

Console.WriteLine(message);

}

}

}

模拟注册,发消息和注销

usingService.Interface;

usingSystem;

usingSystem.Collections.Generic;

usingSystem.Linq;

usingSystem.ServiceModel;

usingSystem.Text;

usingSystem.Threading.Tasks;



namespaceTest

{

classProgram

{

staticvoidMain(string[www.visa158.com]args)

{

InstanceContextcontext=newInstanceContext(newCallBack());

using(ChannelFactoryfactory=newDuplexChannelFactory(context,newNetTcpBinding(),"net.tcp://127.0.0.1:9527/NoticeOperator"))

{

INoticeOperatorproxy=factory.CreateChannel();



StringselfId=args[0];

StringfriendId=args[1];



proxy.Register(selfId);

Console.WriteLine("----------Register------------");



while(true)

{

Stringmessage=Console.ReadLine();

if(message=="q")

{

proxy.UnRegister(selfId);

break;

}

else

{

proxy.SendMessage(selfId,friendId,message);

}

}

}

}

}

}

在CMD中运行test.exeJoeyRoss表示Joey注册,要给他的朋友Ross发送消息;再起一个进程test.exeRossJoey表示Ross注册,要给他的朋友Joey发送消息。进程启动后输入一些字符按回车即发送至了对方,输入q回车注销并退出程序。如下图所示:

Ross:Joey:服务端:后记

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























献花(0)
+1
(本文系白狐一梦首藏)