using Microsoft.AspNetCore.SignalR; using System; using System.Threading.Tasks; namespace BinaryDad.Coding.Hubs { public class CodeHub : Hub { public Task UpdateCode(string user, int index, string code, bool isTeacher) { if (index == 0) { 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; } } // only send to all if it's a teacher if (isTeacher) { return Clients.All.SendAsync("ReceiveCode", user, index, code); } return null; } 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 Clients.All.SendAsync("UsersList", Users.Sessions); } public override async Task OnConnectedAsync() { Users.Sessions[Context.ConnectionId] = new User { Name = string.Empty }; await Clients.All.SendAsync("UsersList", Users.Sessions); await base.OnConnectedAsync(); } public override async Task OnDisconnectedAsync(Exception exception) { if (Users.Sessions.ContainsKey(Context.ConnectionId)) { Users.Sessions.Remove(Context.ConnectionId); } await Clients.All.SendAsync("UsersList", Users.Sessions); await base.OnDisconnectedAsync(exception); } } }