gpt4 book ai didi

.net - 将多个客户端从同一台主机连接到信号服务器 block

转载 作者:行者123 更新时间:2023-12-04 00:44:52 25 4
gpt4 key购买 nike

作为我教育项目的一部分,我正在测试几种 .net 实时通信方法的性能。其中之一是信号器。出于这个原因,我有两个程序:

  • ServerHostApp - 带有 Microsoft.AspNet.SignalR.Hosting.Self.Server 的 WPF 应用程序,它公开一个集线器。
  • ClientHostApp- 一个 WPF 应用程序托管许多信号客户端

  • 程序的典型工作流程是:启动服务器-> 将一些客户端与 ClientHostApp 连接-> 运行测试。

    如果它们都托管在同一台机器上,这些程序可以正常工作,但是如果尝试在不同的计算机上运行这些程序,则会出现问题。特别是,我无法从单个 ClientHostApp 实例连接多个客户端。

    服务器代码:
     //starting up the server
    public override void Start()
    {
    string url = "http://*:9523/MessagingServiceHostWPF/";
    Server = new SignalR.Hosting.Self.Server(url);
    Server.MapHubs();
    Server.Start();
    }

    .
    .
    .
    //one of the methods inside the hub exposed by the server
    public DataContracts.ClientInfo AcknowledgeConnection(string handle)
    {
    ServerLogic.DataContracts.ClientInfo clientInfo = SignalRApplicationState.SingletonInstance.AddClient(handle, Context.ConnectionId);;
    return clientInfo;
    }

    客户端主机应用程序:
    //starting many clients in a loop
    foreach (var client in _clients)
    {
    client.Start();
    }

    .
    .
    .
    //method inside the Client class
    public async void Start()
    {
    _hubConnection = new HubConnection(EndpointAddress);
    _hubProxy = _hubConnection.CreateProxy("MessagingHub");

    await _hubConnection.Start().ContinueWith(task =>
    {
    _hubProxy.Invoke<ClientLogic.MyServices.ClientInfo>("AcknowledgeConnection", Handle).ContinueWith((t) =>
    {
    ClientInfo = t.Result;
    IsConnected = true;
    });
    });
    }

    如果我尝试从同一个 ClientHostApp 实例连接一个客户端 - 我成功了。但是,如果我尝试连接 2 个或更多客户端,则集线器上的 AcknowledgeConnection 方法永远不会执行,而 ClientHostApp 程序只是挂起而没有响应。奇怪的是,如果我在我的客户端机器上启动 Fiddler2,所有客户端都会连接,我可以运行我的测试。

    你知道我在这里缺少什么吗?谢谢。

    最佳答案

    我猜你会在客户端应用程序中遇到每个主机的默认网络连接限制。它不适用于 localhost,因此这很可能是您仅在跨多台机器运行时才遇到它的原因。对于如何解决,您有一些选择:

    基于代码

    增加所有主机

    套装ServicePointManager.DefaultConnectionLimit = 10;在启动时,这将为您提供与您正在通信的任何主机的 10 个出站连接。

    为特定主机增加

    使用 ServicePointManager.FindServicePoint(specificDomainUri).ConnectionLimit = 10在启动时,这将为您提供 10 个仅到该特定 IP/域的出站连接。

    基于配置

    增加所有主机

    配置以下内容以将与您正在通信的任何主机的出站连接增加到 10 个:

     <system.net>
    <connectionManagement>
    <add name="*" maxconnection="10" />
    </connectionManagement>
    </system.net>

    增加特定主机

    将以下配置增加到 10 个仅与特定 IP/域的出站连接:
     <system.net>
    <connectionManagement>
    <add name="somesubdomain.somedomain.com" maxconnection="10" />
    </connectionManagement>
    </system.net>

    关于.net - 将多个客户端从同一台主机连接到信号服务器 block ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13752831/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com