.net 核心应用程序中未建立 Signalr 连接
Signal R Connection not establish in .net core application
在我的应用程序中,我使用 .net 核心应用程序实现了信号 R。在我的例子中,我已经实现了负载均衡器,所以我有 2 个应用程序服务器。现在,当我的应用程序尝试使用基于套接字的传输与信号 R 建立连接时,有时会建立连接,有时会抛出错误。
这是错误:-
An error occurred: Uncaught (in promise): Error: Unable to initialize any of the available transports.
Error: Unable to initialize any of the available transports.
启动文件
services
.AddSignalR()
.AddRedis(Configuration.GetConnectionString("ConnectionRedis"),
options => {
options.Configuration.ClientName = "MyApp";
});
app.UseSignalR(routes => {
routes.MapHub<Hub>(Configuration.GetValue<string>("SignalR:HubName"));
});
集线器连接:-
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();
任何人都知道为什么会这样,我的两个应用程序服务器使用相同的负载平衡 api 来建立集线器连接。任何提示和想法都会有所帮助
谢谢
你应该像这样设置你的代码
services.AddSignalR();
app.UseSignalR(routes =>
{
routes.MapHub<ConnectionHub>("/connectionHub"); // make sure to have / here
});
在你的angular中有一些像
private _hubConnection: HubConnection | undefined;
this._hubConnection = new HubConnectionBuilder()
.withUrl("/connectionHub")
.configureLogging(LogLevel.Error)
.build();
this._hubConnection.start().catch(err => console.error(err.toString()));
我不确定你的 this.signalRHubConnection 有什么价值?
在我的应用程序中,我使用 .net 核心应用程序实现了信号 R。在我的例子中,我已经实现了负载均衡器,所以我有 2 个应用程序服务器。现在,当我的应用程序尝试使用基于套接字的传输与信号 R 建立连接时,有时会建立连接,有时会抛出错误。
这是错误:-
An error occurred: Uncaught (in promise): Error: Unable to initialize any of the available transports.
Error: Unable to initialize any of the available transports.
启动文件
services
.AddSignalR()
.AddRedis(Configuration.GetConnectionString("ConnectionRedis"),
options => {
options.Configuration.ClientName = "MyApp";
});
app.UseSignalR(routes => {
routes.MapHub<Hub>(Configuration.GetValue<string>("SignalR:HubName"));
});
集线器连接:-
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();
你应该像这样设置你的代码
services.AddSignalR();
app.UseSignalR(routes =>
{
routes.MapHub<ConnectionHub>("/connectionHub"); // make sure to have / here
});
在你的angular中有一些像
private _hubConnection: HubConnection | undefined;
this._hubConnection = new HubConnectionBuilder()
.withUrl("/connectionHub")
.configureLogging(LogLevel.Error)
.build();
this._hubConnection.start().catch(err => console.error(err.toString()));
我不确定你的 this.signalRHubConnection 有什么价值?