我正在尝试使用signalR将ASP.NET Core API与Angular 5连接,在ASP.NET MVC 5中可以正常工作,但是将代码移至ASP.NET Core时会出现此错误
http://localhost:54015/signalr/negotiate?clientProtocol=1.5&connectionData= ..净:: 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)
});
}
答案 0 :(得分:1)
示例代码中至少有两个错误。你似乎想用“聊天”中心,但没有一个路径。其次,客户端要使用端口49319,但显示的请求使用端口54015。我想再检查一下您的配置。