将多个客户端从同一主机连接到信号服务器块

ree*_*erz 12 .net real-time signalr

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

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

这些程序的典型工作流程是:启动服务器 - >使用ClientHostApp-> run tests连接一些客户端.

如果程序同时托管在同一台计算机上,但如果尝试在不同的计算机上运行程序,则会出现问题.特别是,我无法从单个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();
}
Run Code Online (Sandbox Code Playgroud)

...

//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;
     }
Run Code Online (Sandbox Code Playgroud)

ClientHostApp:

//starting many clients in a loop
foreach (var client in _clients)
{
  client.Start();
}
Run Code Online (Sandbox Code Playgroud)

...

//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;
         });  
     });
}
Run Code Online (Sandbox Code Playgroud)

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

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

Dre*_*rsh 19

我猜你的客户端应用程序中每个主机都遇到了默认的网络连接限制.它不适用于localhost,因此,当您在多台计算机上运行时,这很可能就是为什么您只是遇到它.您可以选择如何解决:

基于代码

增加所有主机

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

增加特定主机

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

基于配置

增加所有主机

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

 <system.net>
     <connectionManagement>
         <add name="*" maxconnection="10" />
     </connectionManagement>
 </system.net>
Run Code Online (Sandbox Code Playgroud)

增加特定主机

配置以下内容仅增加到特定IP /域的10个出站连接:

 <system.net>
     <connectionManagement>
         <add name="somesubdomain.somedomain.com" maxconnection="10" />
     </connectionManagement>
 </system.net>
Run Code Online (Sandbox Code Playgroud)