Coding/BinaryDad.Coding/Hubs/CodeHub.cs
2022-06-21 17:34:12 -04:00

59 lines
1.7 KiB
C#

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)
{
Users.Sessions[Context.ConnectionId].Html = code;
}
else if (index == 1)
{
Users.Sessions[Context.ConnectionId].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)
{
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)
{
Users.Sessions.Remove(Context.ConnectionId);
await Clients.All.SendAsync("UsersList", Users.Sessions);
await base.OnDisconnectedAsync(exception);
}
}
}