-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ed22170
commit d473e96
Showing
1 changed file
with
14 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,7 +16,7 @@ public class ChatHub : Hub | |
/// <summary> | ||
/// List of the connected users names. | ||
/// </summary> | ||
private static List<string> connectedUsers; | ||
public static readonly List<string> ConnectedUsers = new(); | ||
Check warning on line 19 in FU.API/FU.API/Hubs/ChatHub.cs GitHub Actions / test
|
||
|
||
/// <summary> | ||
/// The app db context. | ||
|
@@ -30,62 +30,61 @@ public class ChatHub : Hub | |
public ChatHub(AppDbContext context) | ||
{ | ||
_context = context; | ||
connectedUsers = new List<string>(); | ||
} | ||
|
||
/// <summary> | ||
/// Connect the user to the hub. | ||
/// </summary> | ||
/// <returns>Task.</returns> | ||
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(); | ||
} | ||
|
||
/// <summary> | ||
/// Disconnect the user from the hub. | ||
/// </summary> | ||
/// <param name="exception">Exception.</param> | ||
/// <returns>Task.</returns> | ||
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); | ||
} | ||
|
||
/// <summary> | ||
|
@@ -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; | ||
} | ||
} |