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();
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-07 17:21:22 +00:00
|
|
|
|
public async Task SaveNote(string content, string? noteName)
|
2023-01-25 13:48:50 +00:00
|
|
|
|
{
|
2023-02-07 17:21:22 +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
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|