代码之家  ›  专栏  ›  技术社区  ›  Ries

WCF点对点聊天

  •  1
  • Ries  · 技术社区  · 16 年前

    我为一个wcf p2p聊天程序写了一些代码。

    <services>
      <service name="PeerChat.Form1">
        <host>
          <baseAddresses>
            <add baseAddress="net.p2p://PeerChat/" />
          </baseAddresses>
        </host>
        <endpoint name="PeerChatEndPoint" address="" binding="netPeerTcpBinding" bindingConfiguration="BindingUnsecure"
           contract="PeerChat.IChatService" />
      </service>
    </services>
    <bindings>
      <netPeerTcpBinding>
        <binding name="BindingUnsecure">
          <resolver mode="Pnrp" />
          <security mode="None" />
        </binding>
      </netPeerTcpBinding>
    </bindings>
    <client>
      <endpoint
          name="PeerChatClientEndPoint"
          address="net.p2p://PeerChat/"
          binding="netPeerTcpBinding"
          bindingConfiguration="BindingUnsecure"
          contract="PeerChat.IChatService"
      />
    </client>
    

    然后,我按如下方式主持服务:

    [ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)]
    public partial class Form1 : Form, IChatService
    {
    
        IChatService channel;
        ServiceHost host = null;
        ChannelFactory<IChatService> channelFactory = null;
    
        private void StartService()
        {
            //Instantiate new ServiceHost
            host = new ServiceHost(this);
            //Open ServiceHost
            host.Open();
            //Create a ChannelFactory and load the configuration setting
            channelFactory = new ChannelFactory<IChatService>("PeerChatClientEndPoint");
            channel = channelFactory.CreateChannel();
            //Lets others know that someone new has joined
            channel.SendMessage("Hello."+ Environment.NewLine);
    
            foreach (var cloud in Cloud.GetAvailableClouds())
            {
                textBox2.Text += cloud.Name + Environment.NewLine;
            }
        }
        private void StopService()
        {
            if (host != null)
            {
                channel.SendMessage("Bye." + Environment.NewLine);
                if (host.State != CommunicationState.Closed)
                {
                    channelFactory.Close();
                    host.Close();
                }
            }
        }
    

    问题是我可以向程序的同一个实例发送消息,但不能向另一个实例发送消息。IE实例只接收自己的消息,不接收来自其他实例的消息。不确定是否需要正确配置PNRP?我在Windows7上测试过。

    1 回复  |  直到 16 年前
        1
  •  1
  •   Streklin    16 年前

    你不会碰巧让程序的两个实例听同一个端点,对吗?我不确定,但我怀疑可能发生的情况是,您的客户机应用程序首先在端点上注册自己,然后在第二个应用程序可以获取到该端点的所有消息之前拦截到该端点的所有消息。我建议尝试将第二个实例配置为在具有不同uri的端点上启动。假设一个连接到net.p2p://peerchata/上,另一个连接到net.p2p://peerchatb/。

    推荐文章