我无法使用API​​(.net core)建立websocket(signalR)连接

时间:2019-05-20 21:27:56

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

角度代码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(控制台):

enter image description here

0 个答案:

没有答案