Refresh Session Everytime Gateway Connection Is Used To Prevent The Server From Aborting Requests

This commit is contained in:
Alan Moon 2025-07-31 10:06:12 -07:00
parent c7137e4c7e
commit 72d9b99c4c

View File

@ -36,9 +36,6 @@ namespace QtCNETAPI.Services.GatewayService
public async Task StartAsync() public async Task StartAsync()
{ {
// just to be safe (it doesn't load the server since it shouldn't request a new one unless its actually expired)
await _apiService.RefreshSessionIfInvalid();
// build connection // build connection
var gwConBuilder = new HubConnectionBuilder() var gwConBuilder = new HubConnectionBuilder()
.WithAutomaticReconnect() .WithAutomaticReconnect()
@ -51,7 +48,12 @@ namespace QtCNETAPI.Services.GatewayService
}) })
.WithUrl(gwBaseUri, options => .WithUrl(gwBaseUri, options =>
{ {
options.AccessTokenProvider = () => Task.FromResult(_apiService.SessionToken); options.AccessTokenProvider = async () =>
{
// this should hopefully refresh the session every time the gateway connection is used to prevent connection aborts
await _apiService.RefreshSessionIfInvalid();
return _apiService.SessionToken;
};
}); });
HubConnection = gwConBuilder.Build(); HubConnection = gwConBuilder.Build();
@ -119,8 +121,6 @@ namespace QtCNETAPI.Services.GatewayService
public async Task JoinLobbyAsync() public async Task JoinLobbyAsync()
{ {
await _apiService.RefreshSessionIfInvalid();
if (HubConnection == null || HubConnection.State != HubConnectionState.Connected) throw new InvalidOperationException("Function was called before connection was made."); if (HubConnection == null || HubConnection.State != HubConnectionState.Connected) throw new InvalidOperationException("Function was called before connection was made.");
await HubConnection.SendAsync("JoinLobby", _apiService.CurrentUser); await HubConnection.SendAsync("JoinLobby", _apiService.CurrentUser);
@ -130,8 +130,6 @@ namespace QtCNETAPI.Services.GatewayService
public async Task JoinRoomAsync(Room room) public async Task JoinRoomAsync(Room room)
{ {
await _apiService.RefreshSessionIfInvalid();
if (HubConnection == null || HubConnection.State != HubConnectionState.Connected) throw new InvalidOperationException("Function was called before connection was made."); if (HubConnection == null || HubConnection.State != HubConnectionState.Connected) throw new InvalidOperationException("Function was called before connection was made.");
if (InLobby == true) if (InLobby == true)
@ -150,8 +148,6 @@ namespace QtCNETAPI.Services.GatewayService
public async Task LeaveRoomAsync() public async Task LeaveRoomAsync()
{ {
await _apiService.RefreshSessionIfInvalid();
if (HubConnection == null || HubConnection.State != HubConnectionState.Connected) throw new InvalidOperationException("Function was called before connection was made."); if (HubConnection == null || HubConnection.State != HubConnectionState.Connected) throw new InvalidOperationException("Function was called before connection was made.");
if (InLobby) if (InLobby)
@ -168,8 +164,6 @@ namespace QtCNETAPI.Services.GatewayService
public async Task PostMessageAsync(Message message) public async Task PostMessageAsync(Message message)
{ {
await _apiService.RefreshSessionIfInvalid();
if (HubConnection == null || HubConnection.State != HubConnectionState.Connected) throw new InvalidOperationException("Function was called before connection was made."); if (HubConnection == null || HubConnection.State != HubConnectionState.Connected) throw new InvalidOperationException("Function was called before connection was made.");
await HubConnection.SendAsync("SendMessage", _apiService.CurrentUser, message, InLobby, CurrentRoom); await HubConnection.SendAsync("SendMessage", _apiService.CurrentUser, message, InLobby, CurrentRoom);
@ -186,8 +180,6 @@ namespace QtCNETAPI.Services.GatewayService
public async Task UpdateStatus(int status) public async Task UpdateStatus(int status)
{ {
await _apiService.RefreshSessionIfInvalid();
if (HubConnection == null || HubConnection.State != HubConnectionState.Connected) throw new InvalidOperationException("Function was called before connection was made."); if (HubConnection == null || HubConnection.State != HubConnectionState.Connected) throw new InvalidOperationException("Function was called before connection was made.");
await HubConnection.SendAsync("UpdateStatus", _apiService.CurrentUser, status); await HubConnection.SendAsync("UpdateStatus", _apiService.CurrentUser, status);