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

61 lines
1.4 KiB
C#
Raw Normal View History

using System;
using System.Threading;
using System.Threading.Tasks;
using AsbCloudApp.Requests;
using AsbCloudApp.Services.Notifications;
using AsbCloudWebApi.SignalR.Services;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
namespace AsbCloudWebApi.SignalR;
[Authorize]
public class NotificationHub : BaseHub
{
private readonly ConnectionManagerService connectionManagerService;
private readonly INotificationService notificationService;
public NotificationHub(ConnectionManagerService connectionManagerService,
INotificationService notificationService)
{
this.connectionManagerService = connectionManagerService;
this.notificationService = notificationService;
}
public async Task OnConnected(NotificationRequest request)
{
try
{
await base.OnConnectedAsync();
var idUser = Context.User?.GetUserId();
if (!idUser.HasValue)
return;
string connectionId = Context.ConnectionId;
connectionManagerService.AddOrUpdateConnection(idUser.Value, connectionId);
await notificationService.ResendNotificationAsync(idUser.Value,
request,
CancellationToken.None);
}
catch (Exception e)
{
Console.WriteLine(e);
}
}
public async Task OnDisconnected()
{
await base.OnDisconnectedAsync(null);
var idUser = Context.User?.GetUserId();
if (!idUser.HasValue)
return;
connectionManagerService.RemoveConnection(idUser.Value);
}
}