我如何继续尝试以一种安全的方式连接到signalR (至少在两次尝试之间允许几百毫秒),直到连接建立为止,如果是的话,有人能提供一种更好的方法pf来处理连接到web套接字的各个阶段吗?
我们在一个signalR后端上使用.NET,我试图用它在一个角度4UI中显示实时推送通知。当它连接时,它工作得很好,但是有一个问题,有时连接需要2-3次。如果我将一个错误this.startSignalRListener is not a function放在catch块中,尝试以这种方式重新连接,我也会得到一个错误。
如有任何建议,将不胜感激。
我在package.json中使用package.json
这是我的服务类的一些代码..。
import { HubConnection } from '@aspnet/signalr-client';
@Injectable()
export class RealTimeService implements OnInit {
private hubConnection: HubConnection;
constructor() {}
ngOnInit() {
this.startSignalRListener();
}
public onConnection(): void {
const domain = this.state.domain;
this.hubConnection
.invoke('WatchSomething', [ 'abc.com' ])
.catch(err => console.error('signalR error: ', err));
this.hubConnection.on('some_thing', (res: any) => {
console.log('do stuff...', res);
});
}
public startSignalRListener() {
const connection = `www.realtimeapi.com`;
this.hubConnection = new HubConnection(connection);
this.hubConnection
.start()
.then(() => {
this.onConnection();
})
.catch(err => {
console.log('Error while establishing connection...');
});
}
}当连接失败时,我怎样才能最好地重新连接?任何建议都会对我有帮助,因为它常常在第一次尝试时就失败。
发布于 2018-04-01 01:14:31
您可以尝试使用setTimeout函数来延迟重新连接:
.catch(err => {
console.log('Error while establishing connection... Retrying...');
setTimeout(() => this.startSignalRListener(), 3000);
});在这种情况下,我建议您将this.hubConnection.on('domain_bid'移出startSignalRListener,只绑定一次。
https://stackoverflow.com/questions/49591843
复制相似问题