我通过 SignalR 从客户端(角度 9)和服务器(asp.net core 3.1)创建实时连接,并通过 JWT 令牌授权集线器,如下代码:
private createConnection() {
this.hubConnection = new HubConnectionBuilder().withUrl(`${this.appConfig.hubEndpoint}/Hubs`,
{ accessTokenFactory: () => jwtToken })
.withAutomaticReconnect()
.build();
}
private startConnection(): void {
this.hubConnection
.start()
.then(() => {
this.connectionIsEstablished = true;
this.connectionEstablished.emit(true);
})
.catch(err => {
console.log('Error while establishing connection, retrying...');
});
}
Run Code Online (Sandbox Code Playgroud)
在令牌过期之前,这一切正常。根据我的研究,在收到带有刷新令牌的新令牌后,应停止先前的连接,并使用新令牌创建新连接。现在我想知道我该怎么做?我必须经常检查令牌吗?或者应该通过向服务器发送每个请求来解决这个问题?