Coding/BinaryDad.Coding/Hubs/CodeHub.cs

59 lines
1.6 KiB
C#

using Microsoft.AspNetCore.SignalR;
using System;
using System.Collections.Generic;
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;
}
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 = color;
await Clients.All.SendAsync("UsersList", Users.Sessions);
}
public override async Task OnConnectedAsync()
{
Users.Sessions[Context.ConnectionId] = new User
{
Name = ""
};
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);
}
}
}