Files
Nexus.Reader/src/NexusReader.Infrastructure/RealTime/SyncHub.cs
T

47 lines
1.2 KiB
C#

using MediatR;
using Microsoft.AspNetCore.SignalR;
using Microsoft.AspNetCore.Authorization;
using NexusReader.Application.Commands.Sync;
namespace NexusReader.Infrastructure.RealTime;
[Authorize]
public class SyncHub : Hub
{
private readonly IMediator _mediator;
public SyncHub(IMediator mediator)
{
_mediator = mediator;
}
public async Task UpdateProgress(string pageId)
{
var userId = Context.UserIdentifier;
if (!string.IsNullOrEmpty(userId))
{
await _mediator.Send(new UpdateReadingProgressCommand(pageId, userId));
}
}
public override async Task OnConnectedAsync()
{
var userId = Context.UserIdentifier;
if (!string.IsNullOrEmpty(userId))
{
await Groups.AddToGroupAsync(Context.ConnectionId, $"User_{userId}");
}
await base.OnConnectedAsync();
}
public override async Task OnDisconnectedAsync(Exception? exception)
{
var userId = Context.UserIdentifier;
if (!string.IsNullOrEmpty(userId))
{
await Groups.RemoveFromGroupAsync(Context.ConnectionId, $"User_{userId}");
}
await base.OnDisconnectedAsync(exception);
}
}