Notes/NoteHub.cs
2023-03-09 22:06:21 -05:00

39 lines
1.0 KiB
C#

using BinaryDad.Notes.Services;
using Microsoft.AspNetCore.SignalR;
namespace BinaryDad.Notes
{
public class NoteHub : Hub
{
private readonly INoteService noteService;
public NoteHub(INoteService 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)
{
noteService.Save(content, noteName);
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);
}
}
}