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:
parent
acc11d536f
commit
6be9ee8b82
@ -23,7 +23,7 @@ namespace qtc_api.Controllers
|
|||||||
public async Task<ActionResult<ServiceResponse<Room>>> CreateRoom(string userId, RoomDto request)
|
public async Task<ActionResult<ServiceResponse<Room>>> CreateRoom(string userId, RoomDto request)
|
||||||
{
|
{
|
||||||
var response = await _roomService.AddRoom(userId, request);
|
var response = await _roomService.AddRoom(userId, request);
|
||||||
await _hubContext.Clients.All.SendAsync("cf", "rr");
|
await _hubContext.Clients.All.SendAsync("RefreshRoomList");
|
||||||
return Ok(response);
|
return Ok(response);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -32,7 +32,7 @@ namespace qtc_api.Controllers
|
|||||||
public async Task<ActionResult<ServiceResponse<Room>>> DeleteRoom(string roomId)
|
public async Task<ActionResult<ServiceResponse<Room>>> DeleteRoom(string roomId)
|
||||||
{
|
{
|
||||||
var response = await _roomService.DeleteRoom(roomId);
|
var response = await _roomService.DeleteRoom(roomId);
|
||||||
await _hubContext.Clients.All.SendAsync("cf", "rr");
|
await _hubContext.Clients.All.SendAsync("RefreshRoomList");
|
||||||
return Ok(response);
|
return Ok(response);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -18,7 +18,7 @@ namespace qtc_api.Hubs
|
|||||||
|
|
||||||
public override async Task OnDisconnectedAsync(Exception? ex)
|
public override async Task OnDisconnectedAsync(Exception? ex)
|
||||||
{
|
{
|
||||||
Log("Client Disconnected.");
|
Log("Client Disconnected From Hub");
|
||||||
|
|
||||||
var connection = ConnectedUsers.FirstOrDefault(x => x.ConnectionId == Context.ConnectionId);
|
var connection = ConnectedUsers.FirstOrDefault(x => x.ConnectionId == Context.ConnectionId);
|
||||||
|
|
||||||
@ -28,45 +28,33 @@ namespace qtc_api.Hubs
|
|||||||
|
|
||||||
if (user != null)
|
if (user != null)
|
||||||
{
|
{
|
||||||
|
ConnectedUsers.Remove(connection);
|
||||||
|
OnlineUsers.Remove(user);
|
||||||
await LogoutAsync(user!);
|
await LogoutAsync(user!);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
await base.OnDisconnectedAsync(ex);
|
||||||
}
|
}
|
||||||
|
|
||||||
public async override Task OnConnectedAsync()
|
public async override Task OnConnectedAsync()
|
||||||
{
|
{
|
||||||
Log("Client Connected To Hub.");
|
Log("Client Connected To Hub");
|
||||||
|
|
||||||
var connection = ConnectedUsers.FirstOrDefault(x => x.ConnectionId == Context.ConnectionId);
|
var uid = Context.UserIdentifier;
|
||||||
|
if (uid != null)
|
||||||
if(connection != null)
|
|
||||||
{
|
{
|
||||||
var onlineUser = OnlineUsers.FirstOrDefault(x => x.Id == connection.ConnectedUser!.Id);
|
var user = await _userService.GetUserById(uid);
|
||||||
|
if (user != null && user.Success && user.Data != null)
|
||||||
if (onlineUser != null)
|
|
||||||
{
|
{
|
||||||
// if the user is reconnecting, ensure their status is set to Online
|
// log them in
|
||||||
await _userService.UpdateStatus(new UserStatusDto { Id = connection.ConnectedUser!.Id, Status = 1 });
|
ConnectedUsers.Add(new UserConnectionDto() { ConnectedUser = user.Data, ConnectionId = Context.ConnectionId });
|
||||||
|
OnlineUsers.Add(user.Data);
|
||||||
|
await LoginAsync(user.Data);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
[HubMethodName("LoginHub")]
|
await base.OnConnectedAsync();
|
||||||
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");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
[HubMethodName("UpdateStatus")]
|
[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");
|
var statusDto = new UserStatusDto { Id = user.Id, Status = 1 };
|
||||||
Log($"User {user.Username} Has Logged Out");
|
|
||||||
|
|
||||||
var statusDto = new UserStatusDto { Id = user.Id, Status = 0 };
|
|
||||||
|
|
||||||
await _userService.UpdateStatus(statusDto);
|
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);
|
private async Task LogoutAsync(User user)
|
||||||
ConnectedUsers.Remove(connection!);
|
{
|
||||||
|
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);
|
private void Log(string message) => _logger.LogInformation(message);
|
||||||
|
@ -14,7 +14,6 @@ global using qtc_api.Hubs;
|
|||||||
using qtc_api.Services.RoomService;
|
using qtc_api.Services.RoomService;
|
||||||
using qtc_api.Services.ContactService;
|
using qtc_api.Services.ContactService;
|
||||||
using qtc_api.Services.CurrencyGamesService;
|
using qtc_api.Services.CurrencyGamesService;
|
||||||
using Microsoft.Extensions.DependencyInjection;
|
|
||||||
|
|
||||||
var builder = WebApplication.CreateBuilder(args);
|
var builder = WebApplication.CreateBuilder(args);
|
||||||
|
|
||||||
@ -59,7 +58,7 @@ builder.Services.AddScoped<IRoomService, RoomService>();
|
|||||||
builder.Services.AddScoped<IContactService, ContactService>();
|
builder.Services.AddScoped<IContactService, ContactService>();
|
||||||
|
|
||||||
builder.Services.AddSingleton<CurrencyGamesService>();
|
builder.Services.AddSingleton<CurrencyGamesService>();
|
||||||
builder.Services.AddHostedService<CurrencyGamesService>(provider => provider.GetService<CurrencyGamesService>()!);
|
builder.Services.AddHostedService(provider => provider.GetService<CurrencyGamesService>()!);
|
||||||
|
|
||||||
var app = builder.Build();
|
var app = builder.Build();
|
||||||
|
|
||||||
|
@ -23,7 +23,7 @@ namespace qtc_api.Services.TokenService
|
|||||||
|
|
||||||
List<Claim> claims = new List<Claim>()
|
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.Name, user.Username),
|
||||||
new Claim(ClaimTypes.Email, user.Email),
|
new Claim(ClaimTypes.Email, user.Email),
|
||||||
new Claim(ClaimTypes.Role, user.Role)
|
new Claim(ClaimTypes.Role, user.Role)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user