null checks on code hub

This commit is contained in:
Ryan Peters 2024-01-17 19:27:07 -05:00
parent 056d810909
commit e9441d510e

View File

@ -10,11 +10,17 @@ namespace BinaryDad.Coding.Hubs
{ {
if (index == 0) if (index == 0)
{ {
Users.Sessions[Context.ConnectionId].Html = code; if (Users.Sessions.TryGetValue(Context.ConnectionId, out User value))
{
value.Html = code;
}
} }
else if (index == 1) else if (index == 1)
{ {
Users.Sessions[Context.ConnectionId].Css = code; if (Users.Sessions.TryGetValue(Context.ConnectionId, out User value))
{
value.Css = code;
}
} }
// only send to all if it's a teacher // only send to all if it's a teacher
@ -28,9 +34,12 @@ namespace BinaryDad.Coding.Hubs
public async Task SaveName(string name, string color) public async Task SaveName(string name, string color)
{ {
Users.Sessions[Context.ConnectionId].Id = Context.ConnectionId; if (!Users.Sessions.ContainsKey(Context.ConnectionId))
Users.Sessions[Context.ConnectionId].Name = name; {
Users.Sessions[Context.ConnectionId].Color = string.IsNullOrWhiteSpace(color) ? "white" : color; Users.Sessions[Context.ConnectionId].Id = Context.ConnectionId;
Users.Sessions[Context.ConnectionId].Name = name;
Users.Sessions[Context.ConnectionId].Color = string.IsNullOrWhiteSpace(color) ? "white" : color;
}
await Clients.All.SendAsync("UsersList", Users.Sessions); await Clients.All.SendAsync("UsersList", Users.Sessions);
} }
@ -49,7 +58,10 @@ namespace BinaryDad.Coding.Hubs
public override async Task OnDisconnectedAsync(Exception exception) public override async Task OnDisconnectedAsync(Exception exception)
{ {
Users.Sessions.Remove(Context.ConnectionId); if (Users.Sessions.ContainsKey(Context.ConnectionId))
{
Users.Sessions.Remove(Context.ConnectionId);
}
await Clients.All.SendAsync("UsersList", Users.Sessions); await Clients.All.SendAsync("UsersList", Users.Sessions);