ASP.NET核心SignalR-集线器协商-Err_Connection_Refused

时间:2019-04-10 13:58:14

标签: angular asp.net-core asp.net-core-signalr

我第一次使用SignalR,并尝试从Angular连接到集线器。我正在尝试在此LINK

中实现相同的功能

我不确定此协商来自何处

OPTIONS http://localhost:5001/messageHub/negotiate net :: ERR_CONNECTION_REFUSED

这是我的代码。

var scriptNode = document.createElement("script");
scriptNode.src = "/wp-content/themes/gowebsites/gw-addon/js/wow.js";

var cssNode = document.createElement("link");
cssNode.href = "/wp-content/themes/gowebsites/gw-addon/css/animations.css";
cssNode.rel = "stylesheet";

head.appendChild(scriptNode);
head.appendChild(cssNode);

这是appsettings.Development.json

public startConnection = () => {
this.hubConnection = new signalR.HubConnectionBuilder()
  .withUrl('http://localhost:5001/messageHub')
  .build();

this.hubConnection
  .start()
  .then(() => alert('Connection started'))
  .catch(err => alert('Error while starting SignalR connection: ' + err));
}

这使我在Chrome开发工具中出现此错误messageHub Negotiate

1 个答案:

答案 0 :(得分:0)

要在asp.net核心中运行signalR,您必须像这样配置这些步骤

以您的打字稿代码

this._hubConnection = new HubConnectionBuilder()
      .withUrl("/signalr")
      .configureLogging(LogLevel.Error)
      .build();

this._hubConnection.start().catch(err => console.error(err.toString()));

Startup.cs

services.AddSignalR();
app.UseSignalR(routes =>
{
  routes.MapHub<ConnectionHub>("/connectionHub");
});

和您的中心班

 public class ConnectionHub : Hub
    {
        public async Task Send(string userId)
        {
            var message = $"Send message to you with user id {userId}";
            await Clients.Client(userId).SendAsync("ReceiveMessage", message);
        }

        public string GetConnectionId()
        {
            return Context.ConnectionId;
        }
    }