diff --git a/NoteContext.cs b/NoteContext.cs new file mode 100644 index 0000000..eaed12f --- /dev/null +++ b/NoteContext.cs @@ -0,0 +1,7 @@ +namespace BinaryDad.Notes +{ + public static class NoteContext + { + public static IDictionary ClientNotes { get; } = new Dictionary(); + } +} \ No newline at end of file diff --git a/NoteHub.cs b/NoteHub.cs index 057f101..791d375 100644 --- a/NoteHub.cs +++ b/NoteHub.cs @@ -12,11 +12,27 @@ namespace BinaryDad.Notes this.noteService = noteService; } + public override Task OnConnectedAsync() + { + var noteName = Context.GetHttpContext().Request.Query["noteName"]; + + NoteContext.ClientNotes[Context.ConnectionId] = noteName; + + return base.OnConnectedAsync(); + } + public async Task SaveNote(string content, string? noteName) { noteService.Save(content, noteName); - await Clients.Others.SendAsync("updateNote", content); + var clientConnections = NoteContext.ClientNotes + .Where(c => c.Value == noteName && c.Key != Context.ConnectionId) + .Select(c => c.Key) + .ToList(); + + await Clients + .Clients(clientConnections) + .SendAsync("updateNote", content); } } } diff --git a/wwwroot/js/site.js b/wwwroot/js/site.js index 4ff5e9e..267fcf0 100644 --- a/wwwroot/js/site.js +++ b/wwwroot/js/site.js @@ -2,7 +2,7 @@ let connection = new signalR.HubConnectionBuilder() .withAutomaticReconnect() - .withUrl("/noteHub") + .withUrl(`/noteHub?noteName=${noteName}`) .build(); function start() {