【发布时间】:2020-02-06 17:17:02
【问题描述】:
我创建了 2 个项目。第一个项目是 .net 核心 mvc 应用程序与 SignalR 一起使用,第二个项目是客户端。所以在我的服务器正在运行但是当我想从 console.app 连接到 signalR 时出现了一些错误。
这是一个代码。 (客户)
HubConnection conn = new HubConnectionBuilder()
.WithUrl("http://localhost:5001/userHub")
.Build();
conn.StartAsync().ContinueWith(t=>{
if(t.IsFaulted)
Console.WriteLine(t.Exception.GetBaseException());
else
Console.WriteLine("Connected to Hub");
}).Wait();
服务器:
public class UserHub : Microsoft.AspNetCore.SignalR.Hub
{
public override Task OnConnectedAsync()
{
System.Console.WriteLine($"New Connection {Context.ConnectionId}");
Clients.All.SendAsync("ReceiveMessage","New Connection Id", Context.ConnectionId);
return base.OnConnectedAsync();
}
public async Task SendMessage(string user, string message)
{
await Clients.All.SendAsync("ReceiveMessage", user, message);
}
}
服务器(配置)
public void ConfigureServices(IServiceCollection services)
{
services.AddControllersWithViews();
services.AddSignalR();
}
app.UseEndpoints(endpoints =>
{
endpoints.MapHub<UserHub>("/userHub");
});
【问题讨论】:
-
嗨@user3304820,关于这个问题的任何更新?您是否尝试过修改我在帖子中提到的 URL?
标签: asp.net-core signalr.client