我有一个SignalR集线器发送组通知,我想测试SignalR组。 以下是Hub服务器代码:
Json
服务器代码将消息发送到组(我将调用此代码以在加入组后向组发送消息):
response
现在我想测试SignalR服务器组加入组并在服务器向该组发送消息时接收消息。 下面是我用来测试相同代码的代码,使用此代码我得到错误"任务被取消"在行 Groups.Add(connectionId,groupName).Wait();
return str.trim();
答案 0 :(得分:0)
您正在混合异步和阻止调用(.Wait
),这可能导致方法内的死锁。你应该等待,而不是阻止它。
首先将Hub更新为异步
[HubName("MyHubServer")]
public class HubServer : Hub {
public override Task OnConnected() {
// My code OnConnected
return base.OnConnected();
}
public override Task OnDisconnected(bool stopCalled) {
// My code OnDisconnected
return base.OnDisconnected(stopCalled);
}
public async Task<string> JoinGroup(string connectionId, string groupName) {
await Groups.Add(connectionId, groupName);
return connectionId + " joined " + groupName;
}
public async Task<string> LeaveGroup(string connectionId, string groupName) {
await Groups.Remove(connectionId, groupName);
return connectionId + " removed " + groupName;
}
public async Task<string> LeaveGroup(string connectionId, string groupName, string customerId = "0") {
await Groups.Remove(connectionId, customerId);
return connectionId + " removed " + groupName;
}
}
接下来,在主控制除外的控制台中执行相同操作。
static async Task MainAsync() {
try {
var hubConnection = new HubConnection("http://localhost:12923/");
IHubProxy stockTickerHubProxy = hubConnection.CreateHubProxy("MyHubServer");
stockTickerHubProxy.On<string>("Associate", data => Console.WriteLine("Notification received : ", data));
await hubConnection.Start();
await stockTickerHubProxy.Invoke("JoinGroup", "123456", "Associate");
} catch (Exception ex) {
Console.WriteLine(ex.Message);
Console.ReadLine();
}
}
一旦客户端能够建立连接,一切都应该按预期运行。
测试了以下自托管示例,以显示客户端和集线器之间的通信。
[HubName("MyHubServer")]
public class HubServer : Hub {
public override Task OnConnected() {
// My code OnConnected
return base.OnConnected();
}
public override Task OnDisconnected(bool stopCalled) {
// My code OnDisconnected
return base.OnDisconnected(stopCalled);
}
public async Task<string> JoinGroup(string connectionId, string groupName) {
await Groups.Add(connectionId, groupName);
var message= connectionId + " joined " + groupName;
Clients.Group("Associate").Notify(message);
return message;
}
public async Task<string> LeaveGroup(string connectionId, string groupName) {
await Groups.Remove(connectionId, groupName);
return connectionId + " removed " + groupName;
}
public async Task<string> LeaveGroup(string connectionId, string groupName, string customerId = "0") {
await Groups.Remove(connectionId, customerId);
return connectionId + " removed " + groupName;
}
}
class Program {
static void Main(string[] args) {
string url = "http://localhost:8080";
using (WebApp.Start(url)) {
Console.WriteLine("Server running on {0}", url);
MainAsync(url).Wait();
Console.ReadLine();
}
}
static async Task MainAsync(string url) {
try {
var hubConnection = new HubConnection(url);
IHubProxy stockTickerHubProxy = hubConnection.CreateHubProxy("MyHubServer");
stockTickerHubProxy.On<string>("Notify",
data => Console.WriteLine("Notification received : {0}", data));
await hubConnection.Start();
var connectionId = hubConnection.ConnectionId;
var response = await stockTickerHubProxy.Invoke<string>("JoinGroup", connectionId, "Associate");
Console.WriteLine("Response received : {0}", response);
} catch (Exception ex) {
Console.WriteLine(ex.Message);
Console.ReadLine();
}
}
}
class Startup {
public void Configuration(IAppBuilder app) {
//app.UseCors(CorsOptions.AllowAll);
app.MapSignalR();
}
}