Coding/BinaryDad.Coding/Hubs/CodeHub.cs

46 lines
1.3 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;
using System.Collections.Generic;
2021-07-19 01:15:57 +00:00
using System.Threading.Tasks;
namespace BinaryDad.Coding.Hubs
{
public class CodeHub : Hub
{
2021-07-21 01:53:39 +00:00
private static readonly IDictionary<string, User> users = new Dictionary<string, User>(StringComparer.OrdinalIgnoreCase);
2021-07-20 02:02:14 +00:00
public Task UpdateCode(string user, int index, string code)
2021-07-19 01:15:57 +00:00
{
2021-07-20 02:02:14 +00:00
return Clients.All.SendAsync("ReceiveCode", user, index, code);
2021-07-19 01:15:57 +00:00
}
2021-07-21 01:53:39 +00:00
public async Task SaveName(string name, string color)
{
users[Context.ConnectionId].Name = name;
users[Context.ConnectionId].Color = color;
await Clients.All.SendAsync("UsersList", users);
}
public override async Task OnConnectedAsync()
{
users[Context.ConnectionId] = new User
{
Name = ""
};
await Clients.All.SendAsync("UsersList", users);
await base.OnConnectedAsync();
}
public override async Task OnDisconnectedAsync(Exception exception)
{
users.Remove(Context.ConnectionId);
await Clients.All.SendAsync("UsersList", users);
await base.OnDisconnectedAsync(exception);
}
2021-07-19 01:15:57 +00:00
}
2021-07-21 01:53:39 +00:00
}