Fix Users Not Disappearing From Room Lists If Client Is Exited Ungracefully

SQL SCHEMA CHANGE
`ALTER TABLE Users ADD COLUMN CurrentRoomId VARCHAR(255);`
This commit is contained in:
Alan Moon 2025-11-17 17:46:10 -08:00
parent 241e159906
commit 2d5cd25a55
2 changed files with 18 additions and 2 deletions

View File

@ -8,16 +8,18 @@ namespace qtc_api.Hubs
private IUserService _userService; private IUserService _userService;
private IRoomService _roomService; private IRoomService _roomService;
private ILogger<ChatHub> _logger; private ILogger<ChatHub> _logger;
private DataContext _dataContext;
private static readonly Dictionary<string, List<User>> GroupUsers = []; private static readonly Dictionary<string, List<User>> GroupUsers = [];
private static readonly Dictionary<string, List<string>> ConnectedUsers = []; private static readonly Dictionary<string, List<string>> ConnectedUsers = [];
private static readonly List<User> OnlineUsers = []; private static readonly List<User> OnlineUsers = [];
public ChatHub(IUserService userService, IRoomService roomService, ILogger<ChatHub> logger) public ChatHub(IUserService userService, IRoomService roomService, ILogger<ChatHub> logger, DataContext dataContext)
{ {
_userService = userService; _userService = userService;
_roomService = roomService; _roomService = roomService;
_logger = logger; _logger = logger;
_dataContext = dataContext;
} }
public async override Task OnConnectedAsync() public async override Task OnConnectedAsync()
@ -78,13 +80,20 @@ namespace qtc_api.Hubs
ConnectedUsers.Remove(userId); ConnectedUsers.Remove(userId);
if (OnlineUsers.Any(e => e.Id == userId)) if (OnlineUsers.Any(e => e.Id == userId))
{ {
// set user offline if there aren't any more connections
var onlineUser = OnlineUsers.FirstOrDefault(e => e.Id == userId); var onlineUser = OnlineUsers.FirstOrDefault(e => e.Id == userId);
if(onlineUser != null) if(onlineUser != null)
{ {
Log("User Has No More Connections. Setting Offline..."); Log("User Has No More Connections. Setting Offline...");
OnlineUsers.Remove(onlineUser); OnlineUsers.Remove(onlineUser);
if (user.Data.Status >= 1) await UpdateStatusAsync(onlineUser, 0); if (user.Data.Status >= 1)
await UpdateStatusAsync(onlineUser, 0);
} }
// also remove the user from any rooms
var room = await _roomService.GetRoom(user.Data.CurrentRoomId);
if (room.Data != null)
await LeaveRoomAsync(user.Data, room.Data);
} }
} }
} }
@ -123,6 +132,9 @@ namespace qtc_api.Hubs
await Clients.Group(room.Id).SendAsync("RoomUserList", GroupUsers[room.Id]); await Clients.Group(room.Id).SendAsync("RoomUserList", GroupUsers[room.Id]);
Log($"User {user.Username} Has Joined {room.Name}"); Log($"User {user.Username} Has Joined {room.Name}");
user.CurrentRoomId = room.Id;
await _dataContext.SaveChangesAsync();
} }
[HubMethodName("JoinRoomGuest")] [HubMethodName("JoinRoomGuest")]
@ -151,6 +163,9 @@ namespace qtc_api.Hubs
await Clients.Group(room.Id).SendAsync("RoomUserList", GroupUsers[room.Id]); await Clients.Group(room.Id).SendAsync("RoomUserList", GroupUsers[room.Id]);
Log($"User {user.Username} Has Left {room.Name}"); Log($"User {user.Username} Has Left {room.Name}");
user.CurrentRoomId = string.Empty;
await _dataContext.SaveChangesAsync();
} }
[HubMethodName("SendMessage")] [HubMethodName("SendMessage")]

View File

@ -17,6 +17,7 @@
public int StockAmount { get; set; } = 0; public int StockAmount { get; set; } = 0;
public DateTime LastCurrencySpin { get; set; } public DateTime LastCurrencySpin { get; set; }
public int ActiveProfileCosmetic { get; set; } = 0; public int ActiveProfileCosmetic { get; set; } = 0;
public string CurrentRoomId { get; set; } = string.Empty;
public virtual IEnumerable<RefreshToken>? RefreshTokens { get; } public virtual IEnumerable<RefreshToken>? RefreshTokens { get; }
public virtual IEnumerable<Contact>? ContactsMade { get; } public virtual IEnumerable<Contact>? ContactsMade { get; }