简体   繁体   中英

Signal R Connection not establish in .net core application

In my application I had implemented signal R with .net core application. in my case I had implemented load balancer, so I have 2 app server. Now when my application try to make connection with signal R, using socket based transport, sometimes connection established and some it's throw err.

Here is error:-

An error occurred: Uncaught (in promise): Error: Unable to initialize any of the available transports.
Error: Unable to initialize any of the available transports.

Start up file

services
.AddSignalR()
.AddRedis(Configuration.GetConnectionString("ConnectionRedis"), 
    options => {
        options.Configuration.ClientName = "MyApp";
    });

app.UseSignalR(routes => {
    routes.MapHub<Hub>(Configuration.GetValue<string>("SignalR:HubName"));
});

Hub connection:-

public override Task OnConnectedAsync()
{

    return base.OnConnectedAsync();
}

Angular UI:-

this.hubConnection = new signalR.HubConnectionBuilder()
  .withUrl(this.signalRHubConnection, {
    accessTokenFactory: () => this.access_token,
    skipNegotiation: false,
    transport: signalR.HttpTransportType.WebSockets
  })
  .configureLogging(signalR.LogLevel.Debug)
  .build();

this.hubConnection
  .start()
  .then(() => {
    this.commonService.setGlobalVariables("conn", "test");
  })
  .catch();

在此处输入图片说明 Any one have idea why this happed, my both app server using same loadbalance api to make a hub connection. any hint and idea would be helpfull thank you

You should set up your code like this

services.AddSignalR();

app.UseSignalR(routes =>
{
   routes.MapHub<ConnectionHub>("/connectionHub"); // make sure to have / here
});

In your angular some thing like

private _hubConnection: HubConnection | undefined;

this._hubConnection = new HubConnectionBuilder()
      .withUrl("/connectionHub")
      .configureLogging(LogLevel.Error)
      .build();

this._hubConnection.start().catch(err => console.error(err.toString()));

I'm not sure about your this.signalRHubConnection what is the value of that ?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM