我想知道一些用于测试基于SignalR集线器的应用程序的不同方法。
解决方法
@ElHaix从我自己测试中看到的方法,你的方法不是创建一个新的连接,而是重用现有的连接。当您循环查看profileID的集合时,您应该看到hubConnection.ConnectionID保持不变。为了创建一个新的连接,你需要在foreach循环中创建一个HubConnection实例。
int successfulConnections = 0; const int loopId = 10; Console.WriteLine("Starting..."); for (int i = 1; i <= loopId; i++) { Console.WriteLine("loop " + i); var hubConnection = new HubConnection(HUB_URL); IHubProxy chatHub = hubConnection.CreateProxy(HUB_NAME); Console.WriteLine("Starting connection"); hubConnection.Start().Wait(); Console.WriteLine("Connection started: " + hubConnection.ConnectionId); chatHub.Invoke("Register","testroom").ContinueWith(task2 => { if (task2.IsFaulted) { Console.WriteLine(String.Format("An error occurred during the method call {0}",task2.Exception.GetBaseException())); } else { Console.WriteLine("Connected: " + hubConnection.ConnectionId); successfulConnections++; } }); Thread.Sleep(1000); }