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);