2023-07-11 19:07:57 +05:00
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
2023-07-14 11:40:57 +05:00
|
|
|
using System.Linq;
|
2023-07-11 19:07:57 +05:00
|
|
|
using System.Threading;
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
using AsbCloudApp.Data;
|
|
|
|
using AsbCloudApp.Services.Notifications;
|
|
|
|
using Microsoft.AspNetCore.SignalR;
|
|
|
|
|
|
|
|
namespace AsbCloudWebApi.SignalR.Services;
|
|
|
|
|
2023-07-13 14:44:40 +05:00
|
|
|
public class SignalRNotificationTransportService : INotificationTransportService
|
2023-07-11 19:07:57 +05:00
|
|
|
{
|
2023-07-14 11:40:57 +05:00
|
|
|
private readonly ConnectionManagerService connectionManagerService;
|
2023-07-11 19:07:57 +05:00
|
|
|
private readonly IHubContext<NotificationHub> notificationHubContext;
|
|
|
|
|
2023-07-14 11:40:57 +05:00
|
|
|
public SignalRNotificationTransportService(ConnectionManagerService connectionManagerService,
|
|
|
|
IHubContext<NotificationHub> notificationHubContext)
|
2023-07-11 19:07:57 +05:00
|
|
|
{
|
2023-07-14 11:40:57 +05:00
|
|
|
this.connectionManagerService = connectionManagerService;
|
2023-07-11 19:07:57 +05:00
|
|
|
this.notificationHubContext = notificationHubContext;
|
|
|
|
}
|
2023-07-13 14:44:40 +05:00
|
|
|
|
|
|
|
public int IdTransportType => 0;
|
|
|
|
|
2023-07-11 19:07:57 +05:00
|
|
|
public async Task SendAsync(NotificationDto notification,
|
|
|
|
CancellationToken cancellationToken)
|
|
|
|
{
|
2023-07-17 11:48:52 +05:00
|
|
|
const string method = "receiveNotifications";
|
2023-07-11 19:07:57 +05:00
|
|
|
|
2023-07-14 11:40:57 +05:00
|
|
|
var connectionId = connectionManagerService.GetConnectionIdByUserId(notification.IdUser);
|
2023-07-11 19:07:57 +05:00
|
|
|
|
|
|
|
if (!string.IsNullOrWhiteSpace(connectionId))
|
|
|
|
{
|
2023-07-13 14:44:40 +05:00
|
|
|
notification.SentDate = DateTime.UtcNow;
|
2023-07-12 13:31:55 +05:00
|
|
|
|
2023-07-11 19:07:57 +05:00
|
|
|
await notificationHubContext.Clients.Client(connectionId)
|
|
|
|
.SendAsync(method,
|
2023-07-13 14:44:40 +05:00
|
|
|
notification,
|
2023-07-11 19:07:57 +05:00
|
|
|
cancellationToken);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-14 11:40:57 +05:00
|
|
|
public Task SendRangeAsync(IEnumerable<NotificationDto> notifications,
|
2023-07-11 19:07:57 +05:00
|
|
|
CancellationToken cancellationToken)
|
|
|
|
{
|
2023-07-17 11:48:52 +05:00
|
|
|
var tasks = notifications
|
|
|
|
.Select(notification => SendAsync(notification, cancellationToken));
|
2023-07-14 11:40:57 +05:00
|
|
|
|
|
|
|
return Task.WhenAll(tasks);
|
2023-07-11 19:07:57 +05:00
|
|
|
}
|
|
|
|
}
|