diff --git a/BinaryDad.Coding/Hubs/CodeHub.cs b/BinaryDad.Coding/Hubs/CodeHub.cs index a2c42f2..5ab253f 100644 --- a/BinaryDad.Coding/Hubs/CodeHub.cs +++ b/BinaryDad.Coding/Hubs/CodeHub.cs @@ -1,13 +1,46 @@ using Microsoft.AspNetCore.SignalR; +using System; +using System.Collections.Generic; using System.Threading.Tasks; namespace BinaryDad.Coding.Hubs { public class CodeHub : Hub { + private static readonly IDictionary users = new Dictionary(StringComparer.OrdinalIgnoreCase); + public Task UpdateCode(string user, int index, string code) { return Clients.All.SendAsync("ReceiveCode", user, index, code); } + + 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); + } } -} +} \ No newline at end of file diff --git a/BinaryDad.Coding/Pages/Index.cshtml b/BinaryDad.Coding/Pages/Index.cshtml index cbe5908..a9aaca2 100644 --- a/BinaryDad.Coding/Pages/Index.cshtml +++ b/BinaryDad.Coding/Pages/Index.cshtml @@ -31,4 +31,6 @@ } -
\ No newline at end of file +
+ +
\ No newline at end of file diff --git a/BinaryDad.Coding/Pages/Shared/_Layout.cshtml b/BinaryDad.Coding/Pages/Shared/_Layout.cshtml index 5ee596d..e7ac2c2 100644 --- a/BinaryDad.Coding/Pages/Shared/_Layout.cshtml +++ b/BinaryDad.Coding/Pages/Shared/_Layout.cshtml @@ -13,8 +13,12 @@
-