我正在使用@aspnet/signalr连接到集线器,并从集线器调用该方法以返回一些数据。
当我第一次初始化服务器时,连接正常建立,但是如果我刷新窗口 3 或 4 次,客户端将停止向服务器发送连接请求。
我尝试记录HubConnection,connectionState起初等于1但在问题产生后connectionState总是等于0
这是我构建 hubConnection 的方式:
buildConnection() {
this.hubConnection = new HubConnectionBuilder()
.withUrl(this.tradesService.getStockQuotationsHubUrl())
.build();
this.hubConnection.serverTimeoutInMilliseconds = 1000 * 10;
this.hubConnection.onclose(() => setTimeout(() => this.startSignalRConnection(), 2000));}
Run Code Online (Sandbox Code Playgroud)
这是我启动 hubConnection 的方式:
startConnection() {
this.hubConnection
.start()
.then(() => {
this.hubConnection.on('updateMethod', (data: any) => {
this.store.push([
{ type: 'update', key: data.stockID, data },
]);
});
this.dataSource = new DataSource({
store: this.store,
reshapeOnPush: true,
});
});}
Run Code Online (Sandbox Code Playgroud)
注意:我在我的页面中同时列出了 5 个集线器,但唯一有问题的是这个。
[更新]
问题出在服务器上,因为当我重新启动服务器时,客户端和服务器之间会重新建立连接,但是如果客户端多次刷新或退出页面,集线器甚至不会尝试连接到客户端 …