Fix Rooms Not Refreshing Dynamically After SignalR Method Rework

Rework SignalR Login Flow
Add `NameIdentifier` To Token Claims
Some Cleanup
This commit is contained in:
Alan Moon 2025-06-29 15:57:45 -07:00
parent acc11d536f
commit 6be9ee8b82
4 changed files with 34 additions and 42 deletions

View File

@ -23,7 +23,7 @@ namespace qtc_api.Controllers
public async Task<ActionResult<ServiceResponse<Room>>> CreateRoom(string userId, RoomDto request)
{
var response = await _roomService.AddRoom(userId, request);
await _hubContext.Clients.All.SendAsync("cf", "rr");
await _hubContext.Clients.All.SendAsync("RefreshRoomList");
return Ok(response);
}
@ -32,7 +32,7 @@ namespace qtc_api.Controllers
public async Task<ActionResult<ServiceResponse<Room>>> DeleteRoom(string roomId)
{
var response = await _roomService.DeleteRoom(roomId);
await _hubContext.Clients.All.SendAsync("cf", "rr");
await _hubContext.Clients.All.SendAsync("RefreshRoomList");
return Ok(response);
}

View File

@ -18,7 +18,7 @@ namespace qtc_api.Hubs
public override async Task OnDisconnectedAsync(Exception? ex)
{
Log("Client Disconnected.");
Log("Client Disconnected From Hub");
var connection = ConnectedUsers.FirstOrDefault(x => x.ConnectionId == Context.ConnectionId);
@ -28,45 +28,33 @@ namespace qtc_api.Hubs
if (user != null)
{
ConnectedUsers.Remove(connection);
OnlineUsers.Remove(user);
await LogoutAsync(user!);
}
}
await base.OnDisconnectedAsync(ex);
}
public async override Task OnConnectedAsync()
{
Log("Client Connected To Hub.");
Log("Client Connected To Hub");
var connection = ConnectedUsers.FirstOrDefault(x => x.ConnectionId == Context.ConnectionId);
if(connection != null)
var uid = Context.UserIdentifier;
if (uid != null)
{
var onlineUser = OnlineUsers.FirstOrDefault(x => x.Id == connection.ConnectedUser!.Id);
if (onlineUser != null)
var user = await _userService.GetUserById(uid);
if (user != null && user.Success && user.Data != null)
{
// if the user is reconnecting, ensure their status is set to Online
await _userService.UpdateStatus(new UserStatusDto { Id = connection.ConnectedUser!.Id, Status = 1 });
}
// log them in
ConnectedUsers.Add(new UserConnectionDto() { ConnectedUser = user.Data, ConnectionId = Context.ConnectionId });
OnlineUsers.Add(user.Data);
await LoginAsync(user.Data);
}
}
[HubMethodName("LoginHub")]
public async Task LoginAsync(User user)
{
Log($"User {user.Username} Has Logged In");
var statusDto = new UserStatusDto { Id = user.Id, Status = 1 };
await _userService.UpdateStatus(statusDto);
ConnectedUsers.Add(new UserConnectionDto() { ConnectedUser = user, ConnectionId = Context.ConnectionId });
OnlineUsers.Add(user);
ServerConfig serverConfig = JsonDocument.Parse(File.ReadAllText("./ServerConfig.json")).Deserialize<ServerConfig>();
await Clients.Client(ConnectedUsers.FirstOrDefault(e => e.ConnectionId == Context.ConnectionId)!.ConnectionId!).SendAsync("ReceiveServerConfig", serverConfig);
await Clients.All.SendAsync("RefreshUserList");
await base.OnConnectedAsync();
}
[HubMethodName("UpdateStatus")]
@ -157,23 +145,28 @@ namespace qtc_api.Hubs
}
}
private async Task LogoutAsync(User user)
private async Task LoginAsync(User user)
{
await Clients.All.SendAsync("rm", $"[SERVER] User {user.Username} Is Now Offline");
Log($"User {user.Username} Has Logged Out");
var statusDto = new UserStatusDto { Id = user.Id, Status = 0 };
var statusDto = new UserStatusDto { Id = user.Id, Status = 1 };
await _userService.UpdateStatus(statusDto);
Log($"Set User {user.Username} To Offline");
ServerConfig serverConfig = JsonDocument.Parse(File.ReadAllText("./ServerConfig.json")).Deserialize<ServerConfig>();
await Clients.All.SendAsync("cf", "rul");
await Clients.Client(Context.ConnectionId).SendAsync("ReceiveServerConfig", serverConfig);
await Clients.All.SendAsync("RefreshUserList");
OnlineUsers.Remove(user);
Log($"User {user.Username} Has Logged In");
}
var connection = ConnectedUsers.FirstOrDefault(x => x.ConnectionId == Context.ConnectionId);
ConnectedUsers.Remove(connection!);
private async Task LogoutAsync(User user)
{
var statusDto = new UserStatusDto { Id = user.Id, Status = 0 };
await _userService.UpdateStatus(statusDto);
await Clients.All.SendAsync("RefreshUserList");
Log($"User {user.Username} Has Logged Out");
}
private void Log(string message) => _logger.LogInformation(message);

View File

@ -14,7 +14,6 @@ global using qtc_api.Hubs;
using qtc_api.Services.RoomService;
using qtc_api.Services.ContactService;
using qtc_api.Services.CurrencyGamesService;
using Microsoft.Extensions.DependencyInjection;
var builder = WebApplication.CreateBuilder(args);
@ -59,7 +58,7 @@ builder.Services.AddScoped<IRoomService, RoomService>();
builder.Services.AddScoped<IContactService, ContactService>();
builder.Services.AddSingleton<CurrencyGamesService>();
builder.Services.AddHostedService<CurrencyGamesService>(provider => provider.GetService<CurrencyGamesService>()!);
builder.Services.AddHostedService(provider => provider.GetService<CurrencyGamesService>()!);
var app = builder.Build();

View File

@ -23,7 +23,7 @@ namespace qtc_api.Services.TokenService
List<Claim> claims = new List<Claim>()
{
new Claim(ClaimTypes.Hash, user.Id),
new Claim(ClaimTypes.NameIdentifier, user.Id),
new Claim(ClaimTypes.Name, user.Username),
new Claim(ClaimTypes.Email, user.Email),
new Claim(ClaimTypes.Role, user.Role)