![](/img/trans.png)
[英]Hub method from client is not invoking in ASP.NET Core SignalR
[英]Invoking SignalR Hub not working for Asp.Net Core Web API
我是SignalR的新手。 我正在尝试建立一个Asp.Net核心WebAPI,以便其他客户端可以使用SignalR连接到它并获取实时数据。 我的Hub类是:
public class TimeHub : Hub
{
public async Task UpdateTime(string message)
{
await Clients.All.SendAsync("ReceiveMessage", message);
}
}
我有一个接力类如下:
public class TimeRelay : ITimeRelay
{
private readonly IHubContext<TimeHub> _timeHubContext;
public TimeRelay(IHubContext<TimeHub> context)
{
_timeHubContext = context;
Task.Factory.StartNew(async () =>
{
while (true)
{
await context.Clients.All.SendAsync("UpdateTime", DateTime.Now.ToShortDateString());
Thread.Sleep(2000);
}
});
}
}
启动课程:
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
services.AddSignalR();
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
app.UseDeveloperExceptionPage();
app.UseHttpsRedirection();
app.UseSignalR((x) =>
{
x.MapHub<TimeHub>("/timeHub");
});
app.UseMvc();
}
客户端是一个控制台应用程序,代码是:
class Program
{
static Action<string> OnReceivedAction = OnReceived;
static void Main(string[] args)
{
Connect();
Console.ReadLine();
}
private static async void Connect()
{
var hubConnectionBuilder = new HubConnectionBuilder();
var hubConnection = hubConnectionBuilder.WithUrl("http://localhost:60211/timeHub").Build();
await hubConnection.StartAsync();
var on = hubConnection.On("ReceiveMessage", OnReceivedAction);
Console.ReadLine();
on.Dispose();
await hubConnection.StopAsync();
}
static void OnReceived(string message)
{
System.Console.WriteLine($"{message}");
}
}
我试过调试应用程序。 客户端成功连接到TimeHub
。 当客户端连接时, Clients.All
的连接Clients.All
从0变为1。 但是,当await context.Clients.All.SendAsync("UpdateTime", DateTime.Now.ToShortDateString());
被执行时, UpdateTime
在功能TimeHub
是没有得到执行,并且所述客户端是没有得到任何消息。
我尝试在TimeRelay
类中的Clients.All.SendAsync
中使用“ TimeRelay
"UpdateTime"
, "SendMessage"
和"ReceiveMessage"
作为方法。 没有任何效果。 有人能指出我的错误。
对于Clients
,如果没有客户端连接到服务器,则它将为null。 为了同时启动Asp.Net Core SignalR
和Console App
, Clients
可能为空,因为可以在Console App连接signalR服务器之前调用Index
。
请尝试以下步骤:
改变TimeHub
public class TimeHub: Hub { public async Task UpdateTime(string message) { if (Clients != null) { await Clients.All.SendAsync("ReceiveMessage", message); } } }
注册TimeHub
services.AddSingleton<TimeHub>();
调节器
public class HomeController : Controller { private readonly TimeHub _timeHub; public HomeController(TimeHub timeHub) { _timeHub = timeHub; } public IActionResult Index() { Task.Factory.StartNew(async () => { while (true) { try { await _timeHub.UpdateTime(DateTime.Now.ToShortDateString()); Thread.Sleep(2000); } catch (Exception ex) { } } }); return View(); }
我得到了它的工作,并认为我会在这里回答它。 谢谢@TaoZhou的小费。
我的错误是从服务器发送“UpdateTime”并在客户端等待“ReceiveMessage”。
理想情况下,代码应如下所示:
SignalR服务器:
await context.Clients.All.SendAsync("UpdateTime", DateTime.Now.ToShortDateString());
SignalR客户端:
var on = hubConnection.On("UpdateTime", OnReceivedAction);
在这种情况下,将立即在客户端接收从服务器发送的任何消息。
有关详细信息,请参阅问题中提供的代码。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.