forked from ddrilling/AsbCloudServer
Степанов Дмитрий Александрович
5f459b79b8
1. Добавил отправку всех неотправленных уведомлений и кол-во непрочитаннах уведомлений при первом подключении 2. При изменении статуса прочтения уведомления, клиенту отправляется информация о том сколько непрочитанных уведомлений ещё есть. 3. Добавил объект NotificationMessage, который отправляется клиенту. 4. Сделал небольшой рефакторинг
56 lines
1.5 KiB
C#
56 lines
1.5 KiB
C#
using System;
|
|
using System.Threading;
|
|
using System.Threading.Tasks;
|
|
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 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();
|
|
|
|
if (!idUser.HasValue)
|
|
return;
|
|
|
|
string connectionId = Context.ConnectionId;
|
|
|
|
connectionManagerService.AddOrUpdateConnection(idUser.Value, connectionId);
|
|
|
|
await base.OnConnectedAsync();
|
|
|
|
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);
|
|
}
|
|
} |