diff --git a/qtc-net-server/Controllers/RoomsController.cs b/qtc-net-server/Controllers/RoomsController.cs index 5315b81..4a0b795 100644 --- a/qtc-net-server/Controllers/RoomsController.cs +++ b/qtc-net-server/Controllers/RoomsController.cs @@ -23,7 +23,7 @@ namespace qtc_api.Controllers public async Task>> 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>> 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); } diff --git a/qtc-net-server/Hubs/ChatHub.cs b/qtc-net-server/Hubs/ChatHub.cs index 486dedb..a9eb54b 100644 --- a/qtc-net-server/Hubs/ChatHub.cs +++ b/qtc-net-server/Hubs/ChatHub.cs @@ -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(); - - 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(); - 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); diff --git a/qtc-net-server/Program.cs b/qtc-net-server/Program.cs index a256efb..d781424 100644 --- a/qtc-net-server/Program.cs +++ b/qtc-net-server/Program.cs @@ -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(); builder.Services.AddScoped(); builder.Services.AddSingleton(); -builder.Services.AddHostedService(provider => provider.GetService()!); +builder.Services.AddHostedService(provider => provider.GetService()!); var app = builder.Build(); diff --git a/qtc-net-server/Services/TokenService/TokenService.cs b/qtc-net-server/Services/TokenService/TokenService.cs index 933e927..563691e 100644 --- a/qtc-net-server/Services/TokenService/TokenService.cs +++ b/qtc-net-server/Services/TokenService/TokenService.cs @@ -23,7 +23,7 @@ namespace qtc_api.Services.TokenService List claims = new List() { - 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)