diff --git a/BinaryDad.Coding/Hubs/CodeHub.cs b/BinaryDad.Coding/Hubs/CodeHub.cs index 43e77d3..0384ff3 100644 --- a/BinaryDad.Coding/Hubs/CodeHub.cs +++ b/BinaryDad.Coding/Hubs/CodeHub.cs @@ -1,4 +1,5 @@ using Microsoft.AspNetCore.SignalR; +using Microsoft.Extensions.Caching.Distributed; using System; using System.Threading.Tasks; @@ -6,23 +7,34 @@ namespace BinaryDad.Coding.Hubs { public class CodeHub : Hub { - public Task UpdateCode(string user, int index, string code, bool isTeacher) + private readonly IDistributedCache _cache; + + public CodeHub(IDistributedCache cache) + { + _cache = cache; + } + + public async Task UpdateCode(string user, int index, string code, bool isTeacher) { Console.WriteLine($"Received code from {user} with index {index}"); if (index == 0) { - if (Users.Sessions.TryGetValue(Context.ConnectionId, out User value)) - { - value.Html = code; - } + await _cache.SetStringAsync($"html-{Context.ConnectionId}", code); + + //if (Users.Sessions.TryGetValue(Context.ConnectionId, out User value)) + //{ + // value.Html = code; + //} } else if (index == 1) { - if (Users.Sessions.TryGetValue(Context.ConnectionId, out User value)) - { - value.Css = code; - } + await _cache.SetStringAsync($"css-{Context.ConnectionId}", code); + + //if (Users.Sessions.TryGetValue(Context.ConnectionId, out User value)) + //{ + // value.Css = code; + //} } // only send to all if it's a teacher @@ -30,24 +42,23 @@ namespace BinaryDad.Coding.Hubs { Console.WriteLine($"Sending code from {user}"); - return Clients.Others.SendAsync("ReceiveCode", user, index, code); + await Clients.Others.SendAsync("ReceiveCode", user, index, code); } - - - - return Task.CompletedTask; } public async Task SaveName(string name, string 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 _cache.SetStringAsync($"name-{Context.ConnectionId}", name); + await _cache.SetStringAsync($"color-{Context.ConnectionId}", string.IsNullOrWhiteSpace(color) ? "white" : color); - await Clients.All.SendAsync("UsersList", Users.Sessions); + //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.Others.SendAsync("UsersList", Users.Sessions); } public override async Task OnConnectedAsync() @@ -57,7 +68,7 @@ namespace BinaryDad.Coding.Hubs Name = string.Empty }; - await Clients.All.SendAsync("UsersList", Users.Sessions); + await Clients.Others.SendAsync("UsersList", Users.Sessions); await base.OnConnectedAsync(); } @@ -69,7 +80,7 @@ namespace BinaryDad.Coding.Hubs Users.Sessions.Remove(Context.ConnectionId); } - await Clients.All.SendAsync("UsersList", Users.Sessions); + await Clients.Others.SendAsync("UsersList", Users.Sessions); await base.OnDisconnectedAsync(exception); } diff --git a/BinaryDad.Coding/Users.cs b/BinaryDad.Coding/Users.cs index f4c97e3..f178671 100644 --- a/BinaryDad.Coding/Users.cs +++ b/BinaryDad.Coding/Users.cs @@ -1,5 +1,7 @@ -using System; +using Microsoft.Extensions.Caching.Distributed; +using System; using System.Collections.Generic; +using System.Threading.Tasks; namespace BinaryDad.Coding { @@ -7,4 +9,26 @@ namespace BinaryDad.Coding { public static readonly IDictionary Sessions = new Dictionary(StringComparer.OrdinalIgnoreCase); } + + public class UserCache + { + private readonly IDistributedCache _cache; + + public UserCache(IDistributedCache cache) + { + _cache = cache; + } + + public async Task GetUser(string connectionId) + { + var user = new User(); + + user.Id = connectionId; + user.Name = await _cache.GetStringAsync($"name-{connectionId}"); + user.Color = await _cache.GetStringAsync($"color-{connectionId}"); + + return user; + } + + } }