使用WCF 命名管道 创建的局域网聊天

WCF 是微软分布式系统开发中的一个重量级产品,也是.net中不得不学的一个通信框架,本文通过利用WCF实现一个局域网聊天,希望给大家做个抛砖引玉的作用。


下面是项目的主要结构

项目结构

    /// <summary>
    /// 通信载体接口
    /// </summary>
    [ServiceContract(SessionMode = SessionMode.Allowed)]
    public interface IMessagesInterface
    {
        [OperationContract]
        string DoMessage(string text);
    }

 public class NamedPipeServer
    {
        private readonly ServiceHost serverHost;

        public string SelfName { get; set; }
        public List<string> ClientNames { get; set; }

        public NamedPipeServer(IMessagesInterface messageInterface, ClientNames selfName)
        {
            SelfName = selfName.ToString();
            ClientNames = new List<string>();
            RegisterClientName();
            serverHost = new ServiceHost(messageInterface);
            serverHost.AddServiceEndpoint((typeof(IMessagesInterface)), new NetNamedPipeBinding(), "net.pipe://localhost/Server/" + SelfName);
            serverHost.Open();
        }


        /// <summary>
        /// 调用远程 DoMessage,发送消息给远程
        /// </summary>
        /// <param name="clientName"></param>
        /// <param name="text">消息正文</param>
        /// <returns>发送结果</returns>
        public void SendMessage(string clientName, string text)
        {
            var msgInterface = GetMessagesInterface(clientName);
            if (msgInterface == null) return;
            try
            {
                msgInterface.DoMessage(text);
            }
            catch (Exception ex)
            {
            }
            finally
            {
                CloseChannel((ICommunicationObject)msgInterface);
            }
        }

        /// <summary>
        /// 打开通道 并 获取可供调用的远程接口
        /// </summary>
        /// <param name="clientKey"></param>
        /// <returns></returns>
        public IMessagesInterface GetMessagesInterface(string clientKey)
        {
            try
            {
                var factory = new ChannelFactory<IMessagesInterface>(new NetNamedPipeBinding(),
                    new EndpointAddress("net.pipe://localhost/Server/" + clientKey));
                return factory.CreateChannel();
            }
            catch (Exception)
            {
                return null;
            }
        }
        /// <summary>
        /// 关闭通道
        /// </summary>
        /// <param name="channel"></param>
        /// <returns></returns>
        private string CloseChannel(ICommunicationObject channel)
        {
            try
            {
                channel.Close();
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
            finally
            {
                channel.Abort();
            }
            return null;
        }

        private void RegisterClientName()
        {
            foreach (var name in Enum.GetNames(typeof(ClientNames)))
            {
                ClientNames.Add(name);
            }
        }
    }

    public enum ClientNames
    {
        win1, win2, win3, win4
    }

core中封装了两个类,一个是通信载体接口,一个是服务的宿主,至于具体的服务实现,每个的实现都不一样。


通过枚举ClientNames 注册各自的名字,每个win 都持有所有的服务名,想给谁发信息,只用告知宿主,对方的名字,就能自己寻找到对方。


core封装了通信过程,每个人都不用去管通信,只用实现通信载体,就行了 。


代码有点抽象,不宜理解,但是结合代码,还是很好理解的。


源码:http://pan.baidu.com/s/1gdwGtwv

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

我是不会选择做一个普通人的

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值