利用利用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;