页面刷新后的SignalR无法正常工作

时间:2018-05-01 16:18:54

标签: locking signalr angular5 signalr-hub live-update

我在angular5网站上使用SignalR来呈现文档锁定。我在数据服务中存储集线器连接,很快广告用户登录,我将开始连接。在我需要集线器连接的每个组件中,我有一个Observable,在建立连接后,它会监听集线器。一切都正常。但刷新页面后,它不会显示SignalR命令的结果。如果我在该页面上单击或鼠标悬停,则会显示SignalR的结果!

这是登录后运行的代码:

startConnection(): void {
//Create the hub connection for SignalR
this.dataService.connection = $.hubConnection(this.dataService.getServerConn());
this.dataService.authProxy = this.dataService.connection.createHubProxy('auth');
this.dataService.authProxy.on('handler', () => { });
this.dataService.authProxyCreated = false;
this.dataService.connection.qs = { "AuthenticationToken": sessionStorage.getItem('UMToken') };
if (this.dataService.connection.state != $.signalR.connectionState.connected)
  this.dataService.connection.start().done(() => {
    console.log('Connected to SignalR hub!');
  }).catch((error: any) => {
    console.log('Hub error -> ' + error);
  });
}

这是组件中侦听集线器的代码:

ngOnInit() {

//SignalR
if (this.storeDataService.connection.state === $.signalR.connectionState.connected)
  this.registerSignalR();
this.storeDataService.connection.stateChanged((change) => {
  if (change.newState === $.signalR.connectionState.connected)
    this.registerSignalR();
});
}
ngOnDestroy() {
this.storeDataService.authProxy.off('lockAuth');
this.storeDataService.authProxy.off('unlockAuth');
}

registerSignalR() {
this.storeDataService.authProxy.on('lockAuth', (authNo: string, username: string) => {
  var auth = this.queueList.data.find(p => p.AuthNo == authNo);
  if (auth) {
    auth.LockedOn = new Date();
    auth.LockedByUserName = username;
  }
});
this.storeDataService.authProxy.on('unlockAuth', (authNo: string) => {
  var auth = this.queueList.data.find(p => p.AuthNo == authNo);
  if (auth) {
    auth.RmiLockedOn = null;
  }
});
}

这也是编辑页面中调用lock的代码:

if (this.dataService.connection.state === $.signalR.connectionState.connected) {
    this.dataService.authProxy.invoke('lock', this.authNo, this.userService.userName, this.userService.userId);
  }
  this.dataService.connection.stateChanged((change) => {
    if (change.newState === $.signalR.connectionState.connected) {
      this.dataService.authProxy.invoke('lock', this.authNo, this.userService.userName, this.userService.userId);
    }
  });

0 个答案:

没有答案