Notes/NoteHub.cs

39 lines
1.0 KiB
C#
Raw Normal View History

2023-01-25 13:48:50 +00:00
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;
}
2023-03-10 03:06:21 +00:00
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)
2023-01-25 13:48:50 +00:00
{
noteService.Save(content, noteName);
2023-01-25 13:48:50 +00:00
2023-03-10 03:06:21 +00:00
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);
2023-01-25 13:48:50 +00:00
}
}
}