From e9441d510ea7c91027a682c0dfbca8c289c560a2 Mon Sep 17 00:00:00 2001 From: Ryan Peters Date: Wed, 17 Jan 2024 19:27:07 -0500 Subject: [PATCH] null checks on code hub --- BinaryDad.Coding/Hubs/CodeHub.cs | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/BinaryDad.Coding/Hubs/CodeHub.cs b/BinaryDad.Coding/Hubs/CodeHub.cs index 800601b..03b4c18 100644 --- a/BinaryDad.Coding/Hubs/CodeHub.cs +++ b/BinaryDad.Coding/Hubs/CodeHub.cs @@ -10,11 +10,17 @@ namespace BinaryDad.Coding.Hubs { 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) { - 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 @@ -28,9 +34,12 @@ namespace BinaryDad.Coding.Hubs public async Task SaveName(string name, string color) { - Users.Sessions[Context.ConnectionId].Id = Context.ConnectionId; - Users.Sessions[Context.ConnectionId].Name = name; - Users.Sessions[Context.ConnectionId].Color = string.IsNullOrWhiteSpace(color) ? "white" : color; + if (!Users.Sessions.ContainsKey(Context.ConnectionId)) + { + 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); } @@ -49,7 +58,10 @@ namespace BinaryDad.Coding.Hubs 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);