【发布时间】:2019-08-27 11:42:54
【问题描述】:
我正在尝试设置 signalR,以便我可以将数据从我的服务器发送到 javascript 以呈现一些图形。
我在 Startup.cs 中添加了所需的项目(删除了所有不相关的东西):
using MyProject.Hubs
public IConfiguration Configuration { get; }
public void ConfigureServices(IServiceCollection services)
{
services.AddSignalR();
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
app.UseSignalR(routes =>
{
routes.MapHub<GraphHub>("/test");
});
app.UseMvc();
}
在 Hubs.cs 中
using Microsoft.AspNetCore.SignalR;
using System.Threading.Tasks;
namespace MyProject.Hubs
{
public class GraphHub : Hub
{
public async Task SendData(int[] data)
{
await Clients.All.SendAsync("sendGraphData", data);
}
}
}
但是在我的 Index.cshtml.cs 中
public IndexModel(MyProject context)
{
_context = context;
}
public async Task OnGetAsync()
{
IList<Cats> cat = await _context.Ticket.Where(x => x.Color = "Black").ToListAsync();
int[] TestData = new int[] {65, 59, 80, 81, 56, 55, 40};
var hubConnection = new HubConnectionBuilder().WithUrl("http://localhost:44307/test").Build();
await hubConnection.StartAsync();
await hubConnection.InvokeAsync("SendData", TestData);
await hubConnection.DisposeAsync();
}
目前该页面在await hubConnection.StartAsync(); 上只是给我一个空白屏幕,我不知道如何调试它。我错过了一步吗?
【问题讨论】:
-
不确定,但可能
.WithUrl()一定是不同的东西 -
我尝试了一个不同的值,它给了我错误“无法检测到 URL 格式”。我尝试了不同的端口,但收到服务器拒绝连接的错误。
-
我发布它可能需要启动时定义的唯一路由,所以我更改了 'routes.MapHub
("/test");'和 '.WithUrl("localhost:44307/test")'。同样的问题。 -
与 Signalr 无关,但我很确定
Where(x => x.Color = "Black")无法编译。此外,下一行ToListAsync()很可能需要在其前面添加一个.。 -
对不起,真实代码很长,所以我把它做短了。我离开了很长时间,现在我收到错误“SocketException:由于线程退出或应用程序请求,I/O 操作已中止”。在线等待 hubConnection.StartAsync.
标签: javascript c# asp.net-core signalr