我有一个控制器,用于维持向我的Web应用显示日志。日志是通过SignalR从ASP.NET Core API发送的。
情况是,当我第一次进入该站点时,新的传入日志会正确显示。但是,当我切换到Web应用程序的另一个页面并返回日志时,尽管可以看到控制台中传入了新日志,但是Logs控制器的视图不会更新。
我的日志控制器如下:
export class LoggerComponent implements OnInit {
hubConnection: HubConnection;
public logs: Array<LogMessage> = new Array<LogMessage>();
constructor(private alertService: AlertService,
private loaderService: LoaderService,
private signalRHandler: SignalRHandler,
) {
// Here I subscribe to event that is called if WebSocket connection was
// lost and renewed so I can acquire new instance of HubCOnnection
// and subscribe to Hub events
this.signalRHandler.onAcquireNewHubInstance.subscribe((acquire) => {
if (acquire) {
this.acquireHubInstance();
}
});
}
ngOnInit() {
// As you can see I tried to re-subscribe to HubEvents and this works
// but when I refresh the app the log messages are duplicated.
//this.acquireHubInstance();
}
acquireHubInstance() {
this.hubConnection = this.signalRHandler.hubConnection;
this.hubConnection.on('BroadcastLogMessage', (data) => {
console.log(data)
if (this.logs.length < 2000) {
this.logs.push(data);
}
});
}
}