-
Notifications
You must be signed in to change notification settings - Fork 0
/
NotificationsHub.cs
49 lines (42 loc) · 1.57 KB
/
NotificationsHub.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
using System;
using System.Threading.Tasks;
using Ancheta.Model.Repositories;
using Ancheta.Model.Services;
using Microsoft.AspNetCore.SignalR;
namespace Ancheta.WebApi.Hubs
{
public class NotificationsHub : Hub<INotificationsClient>
{
private readonly IPollRepository _pollRepository;
public ITinyMessengerHub MessengerHub { get; }
public NotificationsHub(ITinyMessengerHub messengerHub,
IPollRepository pollRepository)
{
MessengerHub = messengerHub ?? throw new ArgumentNullException(nameof(messengerHub));
_pollRepository = pollRepository ?? throw new ArgumentNullException(nameof(pollRepository));
}
/// <summary>
/// Method called by the client when it subscribes to a poll events.
/// </summary>
/// <param name="pollId">The id of the poll.</param>
public async Task SubscribePoll(string pollId)
{
if (Guid.TryParse(pollId, out var id))
{
var poll = _pollRepository.GetById(id);
if (poll != null)
{
await Groups.AddToGroupAsync(Context.ConnectionId, pollId);
}
}
}
/// <summary>
/// Method called by the client when it unsubscribes to a poll events.
/// </summary>
/// <param name="pollId">The id of the poll.</param>
public async Task UnsubscribePoll(string pollId)
{
await Groups.RemoveFromGroupAsync(Context.ConnectionId, pollId);
}
}
}