attempts to share user info

This commit is contained in:
Ryan Peters 2024-01-17 21:49:23 -05:00
parent 4a0603b724
commit f2ff558d2c
2 changed files with 59 additions and 24 deletions

View File

@ -1,4 +1,5 @@
using Microsoft.AspNetCore.SignalR; using Microsoft.AspNetCore.SignalR;
using Microsoft.Extensions.Caching.Distributed;
using System; using System;
using System.Threading.Tasks; using System.Threading.Tasks;
@ -6,23 +7,34 @@ namespace BinaryDad.Coding.Hubs
{ {
public class CodeHub : Hub 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}"); Console.WriteLine($"Received code from {user} with index {index}");
if (index == 0) if (index == 0)
{ {
if (Users.Sessions.TryGetValue(Context.ConnectionId, out User value)) await _cache.SetStringAsync($"html-{Context.ConnectionId}", code);
{
value.Html = code; //if (Users.Sessions.TryGetValue(Context.ConnectionId, out User value))
} //{
// value.Html = code;
//}
} }
else if (index == 1) else if (index == 1)
{ {
if (Users.Sessions.TryGetValue(Context.ConnectionId, out User value)) await _cache.SetStringAsync($"css-{Context.ConnectionId}", code);
{
value.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
@ -30,24 +42,23 @@ namespace BinaryDad.Coding.Hubs
{ {
Console.WriteLine($"Sending code from {user}"); 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) public async Task SaveName(string name, string color)
{ {
if (!Users.Sessions.ContainsKey(Context.ConnectionId)) await _cache.SetStringAsync($"name-{Context.ConnectionId}", name);
{ await _cache.SetStringAsync($"color-{Context.ConnectionId}", 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); //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() public override async Task OnConnectedAsync()
@ -57,7 +68,7 @@ namespace BinaryDad.Coding.Hubs
Name = string.Empty Name = string.Empty
}; };
await Clients.All.SendAsync("UsersList", Users.Sessions); await Clients.Others.SendAsync("UsersList", Users.Sessions);
await base.OnConnectedAsync(); await base.OnConnectedAsync();
} }
@ -69,7 +80,7 @@ namespace BinaryDad.Coding.Hubs
Users.Sessions.Remove(Context.ConnectionId); Users.Sessions.Remove(Context.ConnectionId);
} }
await Clients.All.SendAsync("UsersList", Users.Sessions); await Clients.Others.SendAsync("UsersList", Users.Sessions);
await base.OnDisconnectedAsync(exception); await base.OnDisconnectedAsync(exception);
} }

View File

@ -1,5 +1,7 @@
using System; using Microsoft.Extensions.Caching.Distributed;
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Threading.Tasks;
namespace BinaryDad.Coding namespace BinaryDad.Coding
{ {
@ -7,4 +9,26 @@ namespace BinaryDad.Coding
{ {
public static readonly IDictionary<string, User> Sessions = new Dictionary<string, User>(StringComparer.OrdinalIgnoreCase); public static readonly IDictionary<string, User> Sessions = new Dictionary<string, User>(StringComparer.OrdinalIgnoreCase);
} }
public class UserCache
{
private readonly IDistributedCache _cache;
public UserCache(IDistributedCache cache)
{
_cache = cache;
}
public async Task<User> 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;
}
}
} }