DD.WellWorkover.Cloud/AsbCloudWebApi/SignalR/NotificationHub.cs

57 lines
1.7 KiB
C#
Raw Normal View History

using System;
using System.Threading;
using System.Threading.Tasks;
using AsbCloudApp.Services.Notifications;
using AsbCloudWebApi.SignalR.Clients;
using AsbCloudWebApi.SignalR.Services;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
namespace AsbCloudWebApi.SignalR;
[Authorize]
public class NotificationHub : BaseHub<INotificationHubClient>
{
private readonly ConnectionManagerService connectionManagerService;
private readonly NotificationService notificationService;
private readonly NotificationPublisher notificationPublisher;
public NotificationHub(ConnectionManagerService connectionManagerService,
NotificationService notificationService,
NotificationPublisher notificationPublisher)
{
this.connectionManagerService = connectionManagerService;
this.notificationService = notificationService;
this.notificationPublisher = notificationPublisher;
}
public override async Task OnConnectedAsync()
{
var idUser = Context.User?.GetUserId();
2023-07-17 11:48:52 +05:00
if (!idUser.HasValue)
return;
2023-07-17 11:48:52 +05:00
string connectionId = Context.ConnectionId;
2023-07-17 11:48:52 +05:00
connectionManagerService.AddOrUpdateConnection(idUser.Value, connectionId);
2023-07-17 11:48:52 +05:00
await base.OnConnectedAsync();
2023-07-17 11:48:52 +05:00
await notificationPublisher.PublishCountUnreadAsync(idUser.Value, CancellationToken.None);
await notificationService.RenotifyAsync(idUser.Value,
CancellationToken.None);
}
public override async Task OnDisconnectedAsync(Exception? exception)
{
var idUser = Context.User?.GetUserId();
if (!idUser.HasValue)
return;
connectionManagerService.RemoveConnection(idUser.Value);
await base.OnDisconnectedAsync(exception);
}
}