【问题标题】:SignalR Core with Angular: Failed to connect. Error: Error during negotiation requestSignalR Core with Angular:连接失败。错误:协商请求期间出错
【发布时间】:2019-02-02 20:04:48
【问题描述】:

我正在尝试使用 signalR 将 ASP.NET Core API 与 Angular 5 连接,它在 ASP.NET MVC 5 中运行良好,但是当将我的代码移动到 ASP.NET Core 时,它​​给了我这个错误

http://localhost:54015/signalr/negotiate?clientProtocol=1.5&connectionData=..net::ERR_ABORTED 404(未找到)

无法连接 连接失败。错误:协商请求期间出错。

//Startup.cs

public void ConfigureServices(IServiceCollection services)
{
   services.AddSignalR();
   services.AddCors();

   services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
}

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
   //...
   app.UseCors(options => options.AllowAnyHeader().AllowAnyMethod().AllowAnyOrigin());
   app.UseSignalR((options) => {
      options.MapHub<ValuesHub>("/Hubs/Values");
   });

   //app.UseHttpsRedirection();
   app.UseMvc();
}

//NotificationHub.cs

public class NotificationHub: Hub
{
   public Task Send(string message)
   {
      return Clients.All.SendAsync("Send", message);
   }
}

//角度 //app.module.ts

import { SignalRModule } from 'ng2-signalr';
import { SignalRConfiguration } from 'ng2-signalr';

export function createConfig(): SignalRConfiguration {
  const c = new SignalRConfiguration();
  c.hubName = 'Chat';
  c.url = 'http://localhost:49319';
  return c;
}

//app.component.ts

import { SignalR, BroadcastEventListener } from 'ng2-signalr';
onMessageSent$ = new BroadcastEventListener<string>('Send');
constructor(private _signalR: SignalR) {

}
ngOnInit() {
   this._signalR.connect().then((c) => {
   console.log("success", c)
   c.listen(this.onMessageSent$);
      //do stuff
   })
   .catch(
   (error) => {
      console.log("error", error)
   });
   this.onMessageSent$.subscribe((msg: string) => {
      console.log("msg", msg)
   });
}

【问题讨论】:

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


    【解决方案1】:

    您的示例代码中至少有两个错误。您似乎想使用“聊天”中心,但没有路径。其次,客户端想要使用端口 49319,但显示的请求使用端口 54015。我会说再检查一下您的配置。

    【讨论】:

      猜你喜欢
      • 2018-04-18
      • 1970-01-01
      • 1970-01-01
      • 2020-12-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-06-05
      • 2020-08-06
      相关资源
      最近更新 更多