Signalr 来自TypeScript的信号器集线器连接失败
我正试图从TypeScript初始化与核心3.1信号器集线器的连接。然而,我的前端Signalr 来自TypeScript的信号器集线器连接失败,signalr,signalr-hub,Signalr,Signalr Hub,我正试图从TypeScript初始化与核心3.1信号器集线器的连接。然而,我的前端/negotiate请求在最终失败之前等待了很长一段时间 我的角度服务是notification.service.ts- 从'@angular/core'导入{Injectable}; 从'@microsoft/signalr'导入*作为信号器; @注射的({ providedIn:'根' }) 出口类通知服务{ 专用hubConnection:signal.hubConnection; 公共启动连接=()=>{
/negotiate
请求在最终失败之前等待了很长一段时间
我的角度服务是notification.service.ts-
从'@angular/core'导入{Injectable};
从'@microsoft/signalr'导入*作为信号器;
@注射的({
providedIn:'根'
})
出口类通知服务{
专用hubConnection:signal.hubConnection;
公共启动连接=()=>{
this.hubConnection=new signal.HubConnectionBuilder()
.withUrl('http://localhost:44311/hub')
.build();
这是hubConnection
.start()
。然后(()=>console.log('连接已启动'))
.catch((err)=>console.log(`Error whilestarting connection:${err}`));
}
构造函数(){}
}
您需要将前端的url更改为https
如下:
从'@angular/core'导入{Injectable};
从'@microsoft/signalr'导入*作为信号器;
@注射的({
providedIn:'根'
})
出口类通知服务{
专用hubConnection:signal.hubConnection;
公共启动连接=()=>{
this.hubConnection=new signal.HubConnectionBuilder()
.withUrl('https://localhost:44311/hub')
.build();
这是hubConnection
.start()
。然后(()=>console.log('连接已启动'))
.catch((err)=>console.log(`Error whilestarting connection:${err}`));
}
构造函数(){}
}
然后,在服务器端,只需正确执行CORS
:
services.AddCors(options =>
{
options.AddPolicy(CorsPolicy, builder => builder.WithOrigins("http://localhost:4200")
.AllowAnyHeader()
.AllowAnyMethod()
.AllowCredentials()
.SetIsOriginAllowed((host) => true));
});
services.AddCors(选项=>
{
options.AddPolicy(CorsPolicy,builder=>builder.WithOrigins(“http://localhost:4200")
.AllowAnyHeader()
.AllowAnyMethod()
.AllowCredentials()
.SetIsOriginAllowed((主机)=>true));
});
您在示例中使用的是https而不是https。此外,您的CORS设置不正确