Coding/BinaryDad.Coding/Hubs/CodeHub.cs

59 lines
1.7 KiB
C#
Raw Normal View History

2021-07-19 01:15:57 +00:00
using Microsoft.AspNetCore.SignalR;
2021-07-21 01:53:39 +00:00
using System;
2021-07-19 01:15:57 +00:00
using System.Threading.Tasks;
namespace BinaryDad.Coding.Hubs
{
public class CodeHub : Hub
{
2021-07-23 00:41:12 +00:00
public Task UpdateCode(string user, int index, string code, bool isTeacher)
2021-07-19 01:15:57 +00:00
{
2021-07-23 00:41:12 +00:00
if (index == 0)
{
Users.Sessions[Context.ConnectionId].Html = code;
}
else if (index == 1)
{
Users.Sessions[Context.ConnectionId].Css = code;
}
2021-07-23 02:51:14 +00:00
// only send to all if it's a teacher
2021-07-23 00:41:12 +00:00
if (isTeacher)
{
return Clients.All.SendAsync("ReceiveCode", user, index, code);
}
return null;
2021-07-19 01:15:57 +00:00
}
2021-07-21 01:53:39 +00:00
public async Task SaveName(string name, string color)
{
2021-07-23 00:41:12 +00:00
Users.Sessions[Context.ConnectionId].Id = Context.ConnectionId;
Users.Sessions[Context.ConnectionId].Name = name;
2021-07-23 02:51:14 +00:00
Users.Sessions[Context.ConnectionId].Color = string.IsNullOrWhiteSpace(color) ? "white" : color;
2021-07-21 01:53:39 +00:00
2021-07-23 00:41:12 +00:00
await Clients.All.SendAsync("UsersList", Users.Sessions);
2021-07-21 01:53:39 +00:00
}
public override async Task OnConnectedAsync()
{
2021-07-23 00:41:12 +00:00
Users.Sessions[Context.ConnectionId] = new User
2021-07-21 01:53:39 +00:00
{
2021-07-23 02:51:14 +00:00
Name = string.Empty
2021-07-21 01:53:39 +00:00
};
2021-07-23 00:41:12 +00:00
await Clients.All.SendAsync("UsersList", Users.Sessions);
2021-07-21 01:53:39 +00:00
await base.OnConnectedAsync();
}
public override async Task OnDisconnectedAsync(Exception exception)
{
2021-07-23 00:41:12 +00:00
Users.Sessions.Remove(Context.ConnectionId);
2021-07-21 01:53:39 +00:00
2021-07-23 00:41:12 +00:00
await Clients.All.SendAsync("UsersList", Users.Sessions);
2021-07-21 01:53:39 +00:00
await base.OnDisconnectedAsync(exception);
}
2021-07-19 01:15:57 +00:00
}
2021-07-21 01:53:39 +00:00
}