From a1e97d6063ddbb10f7a8c327b782209d36f184c7 Mon Sep 17 00:00:00 2001 From: Ryan Peters Date: Tue, 20 Jul 2021 21:53:39 -0400 Subject: [PATCH] added users list on bottom --- BinaryDad.Coding/Hubs/CodeHub.cs | 35 ++++++++++++++++++- BinaryDad.Coding/Pages/Index.cshtml | 4 ++- BinaryDad.Coding/Pages/Shared/_Layout.cshtml | 6 +++- BinaryDad.Coding/User.cs | 8 +++++ BinaryDad.Coding/wwwroot/css/site.css | 11 ++++++ BinaryDad.Coding/wwwroot/js/hub.js | 36 ++++++++++++++++---- 6 files changed, 91 insertions(+), 9 deletions(-) create mode 100644 BinaryDad.Coding/User.cs 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 @@
-