diff --git a/FU.API/FU.API/Hubs/ChatHub.cs b/FU.API/FU.API/Hubs/ChatHub.cs index a2f6d22f..1fbf977f 100644 --- a/FU.API/FU.API/Hubs/ChatHub.cs +++ b/FU.API/FU.API/Hubs/ChatHub.cs @@ -16,7 +16,7 @@ public class ChatHub : Hub /// /// List of the connected users names. /// - private static List connectedUsers; + public static readonly List ConnectedUsers = new(); /// /// The app db context. @@ -30,34 +30,33 @@ public class ChatHub : Hub public ChatHub(AppDbContext context) { _context = context; - connectedUsers = new List(); } /// /// Connect the user to the hub. /// /// Task. - public async override Task OnConnectedAsync() + public override Task OnConnectedAsync() { var userId = UserId; var user = _context.Users.Find(userId); if (user is null || user.Username is null) { - return; + return Task.CompletedTask; } - if (!connectedUsers.Contains(user.Username)) + if (!ConnectedUsers.Contains(user.Username)) { - connectedUsers.Add(user.Username); + ConnectedUsers.Add(user.Username); } // Update the online status of the user user.IsOnline = true; _context.Users.Update(user); - await _context.SaveChangesAsync(); + _context.SaveChanges(); - await base.OnConnectedAsync(); + return base.OnConnectedAsync(); } /// @@ -65,27 +64,27 @@ public async override Task OnConnectedAsync() /// /// Exception. /// Task. - public override async Task OnDisconnectedAsync(Exception? exception) + public override Task OnDisconnectedAsync(Exception? exception) { var userId = UserId; var user = _context.Users.Find(userId); if (user is null || user.Username is null) { - return; + return Task.CompletedTask; } - if (connectedUsers.Contains(user.Username)) + if (ConnectedUsers.Contains(user.Username)) { - connectedUsers.Remove(user.Username); + ConnectedUsers.Remove(user.Username); } // Update the online status of the user user.IsOnline = false; _context.Users.Update(user); - await _context.SaveChangesAsync(); + _context.SaveChanges(); - base.OnDisconnectedAsync(exception); + return base.OnDisconnectedAsync(exception); } /// @@ -114,4 +113,4 @@ public async Task LeaveChatGroup(int chatId) private int UserId => Context?.User?.Claims?.FirstOrDefault(c => c.Type == CustomClaimTypes.UserId)?.Value is not null ? int.Parse(Context.User.Claims.FirstOrDefault(c => c.Type == CustomClaimTypes.UserId)?.Value ?? string.Empty) : -1; -} +} \ No newline at end of file