角度代码7 客户端:
export class signalrService {
private hubconnection: signalR.HubConnection;
startConnection = () => {
this.hubconnection = new signalR.HubConnectionBuilder()
.configureLogging(signalR.LogLevel.Trace)
.withUrl('http://localhost:4201/monitor')
.build()
this.hubconnection
.start()
.then(() => console.log('hubconnection ok'))
.catch(err => console.log('Falha hubconnection' + err))
}
代码后端api(.Net核心2.1)
中心:
[AllowAnonymous]
public class monitorhub : Hub
{
}
启动:
在配置服务中:
services.AddSignalR();
在配置中:
app.UseSignalR((configure) =>
{
var desiredTransports = HttpTransportType.WebSockets;
configure.MapHub<monitorhub>("/monitor", (options) =>
{
options.Transports = desiredTransports;
});
});
要求启动:
app.UseCors(opt =>
opt.WithOrigins("http://localhost:4200")
.AllowAnyMethod()
.AllowCredentials()
.AllowAnyHeader());
出现错误的LOG(控制台):