WCF 点对点聊天

发布于 2024-08-06 22:31:46 字数 2273 浏览 4 评论 0原文

我为 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();
            }
        }
    }

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

I wrote some code for a WCF P2P chat program.

<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>

I then host the service as follows:

[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();
            }
        }
    }

The problem is I can send a message to the same instance of the program but not to another instance. Ie an instance only receives its own messages not messages from other instances. Not sure if it is a matter of configuring PNRP correctly? I tested on Windows 7.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

可爱咩 2024-08-13 22:31:46

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

You wouldn't happen to have both instances of the program listening to the same end point would you? I am not certain, but I suspect what may be happening is that your client application is registering itself on the endpoint first, then intercepting all the messages that come to that endpoint before the second one can get them. What I'd suggest trying to do is configuring the second instance to start up on an endpoint with a diferent Uri. So say one connects on net.p2p://PeerChatA/ and the other net.p2p://PeerChatB/ .

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文