This commit is contained in:
Ryan Peters 2023-03-09 22:06:21 -05:00
parent 5fa4fe6fb5
commit b89a557af1
3 changed files with 25 additions and 2 deletions

7
NoteContext.cs Normal file
View File

@ -0,0 +1,7 @@
namespace BinaryDad.Notes
{
public static class NoteContext
{
public static IDictionary<string, string> ClientNotes { get; } = new Dictionary<string, string>();
}
}

View File

@ -12,11 +12,27 @@ namespace BinaryDad.Notes
this.noteService = noteService; 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) public async Task SaveNote(string content, string? noteName)
{ {
noteService.Save(content, 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);
} }
} }
} }

View File

@ -2,7 +2,7 @@
let connection = new signalR.HubConnectionBuilder() let connection = new signalR.HubConnectionBuilder()
.withAutomaticReconnect() .withAutomaticReconnect()
.withUrl("/noteHub") .withUrl(`/noteHub?noteName=${noteName}`)
.build(); .build();
function start() { function start() {