2023-07-12 13:31:55 +05:00
|
|
|
using System;
|
2023-07-10 16:59:11 +05:00
|
|
|
using System.Threading;
|
|
|
|
using System.Threading.Tasks;
|
2023-07-13 14:44:40 +05:00
|
|
|
using AsbCloudApp.Requests;
|
2023-07-11 19:07:57 +05:00
|
|
|
using AsbCloudApp.Services.Notifications;
|
2023-07-14 11:40:57 +05:00
|
|
|
using AsbCloudWebApi.SignalR.Services;
|
2023-07-10 16:59:11 +05:00
|
|
|
using Microsoft.AspNetCore.Authorization;
|
2023-07-14 11:40:57 +05:00
|
|
|
using Microsoft.AspNetCore.Mvc;
|
2023-07-10 16:59:11 +05:00
|
|
|
|
|
|
|
namespace AsbCloudWebApi.SignalR;
|
|
|
|
|
|
|
|
[Authorize]
|
|
|
|
public class NotificationHub : BaseHub
|
|
|
|
{
|
2023-07-14 11:40:57 +05:00
|
|
|
private readonly ConnectionManagerService connectionManagerService;
|
2023-07-17 11:48:52 +05:00
|
|
|
private readonly NotificationService notificationService;
|
2023-07-10 16:59:11 +05:00
|
|
|
|
2023-07-14 11:40:57 +05:00
|
|
|
public NotificationHub(ConnectionManagerService connectionManagerService,
|
2023-07-17 11:48:52 +05:00
|
|
|
NotificationService notificationService)
|
2023-07-10 16:59:11 +05:00
|
|
|
{
|
2023-07-14 11:40:57 +05:00
|
|
|
this.connectionManagerService = connectionManagerService;
|
2023-07-10 16:59:11 +05:00
|
|
|
this.notificationService = notificationService;
|
|
|
|
}
|
|
|
|
|
2023-07-17 11:48:52 +05:00
|
|
|
public override async Task OnConnectedAsync()
|
2023-07-10 16:59:11 +05:00
|
|
|
{
|
2023-07-17 11:48:52 +05:00
|
|
|
var idUser = Context.User?.GetUserId();
|
|
|
|
|
|
|
|
if (!idUser.HasValue)
|
|
|
|
return;
|
|
|
|
|
|
|
|
string connectionId = Context.ConnectionId;
|
|
|
|
|
|
|
|
connectionManagerService.AddOrUpdateConnection(idUser.Value, connectionId);
|
|
|
|
|
|
|
|
await base.OnConnectedAsync();
|
|
|
|
|
|
|
|
await notificationService.ResendNotificationAsync(idUser.Value,
|
|
|
|
new NotificationRequest { IsSent = false, IdTransportType = 0},
|
|
|
|
CancellationToken.None);
|
2023-07-12 13:31:55 +05:00
|
|
|
}
|
|
|
|
|
2023-07-17 11:48:52 +05:00
|
|
|
public override async Task OnDisconnectedAsync(Exception? exception)
|
|
|
|
{
|
2023-07-14 11:40:57 +05:00
|
|
|
var idUser = Context.User?.GetUserId();
|
|
|
|
|
|
|
|
if (!idUser.HasValue)
|
|
|
|
return;
|
|
|
|
|
|
|
|
connectionManagerService.RemoveConnection(idUser.Value);
|
2023-07-17 11:48:52 +05:00
|
|
|
await base.OnDisconnectedAsync(exception);
|
2023-07-10 16:59:11 +05:00
|
|
|
}
|
|
|
|
}
|