< Summary

Class:SVETA.Api.Hubs.NotificationHub
Assembly:SVETA.Api
File(s):/opt/dev/sveta_api_build/SVETA.Api/Hubs/NotificationHub.cs
Covered lines:0
Uncovered lines:33
Coverable lines:33
Total lines:66
Line coverage:0% (0 of 33)
Covered branches:0
Total branches:18
Branch coverage:0% (0 of 18)

Metrics

MethodLine coverage Branch coverage
.ctor(...)0%100%
Send()0%100%
NewMessage()0%100%
OnConnectedAsync()0%0%
OnDisconnectedAsync(...)0%0%

File(s)

/opt/dev/sveta_api_build/SVETA.Api/Hubs/NotificationHub.cs

#LineLine coverage
 1using System;
 2using System.Collections.Generic;
 3using System.Linq;
 4using System.Threading.Tasks;
 5using Microsoft.AspNetCore.Authorization;
 6using Microsoft.AspNetCore.SignalR;
 7using Microsoft.Extensions.Logging;
 8using WinSolutions.Sveta.Server.Services.Interfaces;
 9
 10namespace SVETA.Api.Hubs
 11{
 12    [Authorize]
 13    public class NotificationHub : Hub
 14    {
 15        private readonly INotificationService _notificationService;
 16        private readonly ILogger<NotificationHub> _logger;
 17
 018        public NotificationHub(INotificationService notificationService, ILogger<NotificationHub> logger)
 019        {
 020            _notificationService = notificationService;
 021            _logger = logger;
 022        }
 23
 24        public async Task Send()
 025        {
 026            await Clients.All.SendAsync("notificationReceived", Context.UserIdentifier);
 027        }
 28
 29        public async Task NewMessage(long userName, string message)
 030        {
 031            await Clients.All.SendAsync("messageReceived", userName, message);
 032        }
 33
 34        public override Task OnConnectedAsync()
 035        {
 036            _logger.LogError($"Подключаем пользователя " +
 037                             $"{Context.User.Identities.SelectMany(d => d.Claims).FirstOrDefault(s => s.Type.Contains("u
 038            var tid = Context.User.Identities.SelectMany(d => d.Claims).FirstOrDefault(s => s.Type.Contains("user_TID"))
 039                .Value.ToString();
 040            string actual = default;
 041            UserHandler.ConnectedIds.TryGetValue(tid, out actual);
 042            if (actual != default)
 043            {
 044                OnDisconnectedAsync(new ArgumentException("User already have connection"));
 045            }
 046            UserHandler.ConnectedIds.Add(tid, Context.ConnectionId);
 047            return base.OnConnectedAsync();
 048        }
 49
 50        public override Task OnDisconnectedAsync(Exception exception)
 051        {
 052            _logger.LogError($"Удаляем подключение пользователя " +
 053                             $"{Context.User.Identities.SelectMany(d => d.Claims).FirstOrDefault(s => s.Type.Contains("u
 054            var tid = Context.User.Identities.SelectMany(d => d.Claims).FirstOrDefault(s => s.Type.Contains("user_TID"))
 055                .Value.ToString();
 056            UserHandler.ConnectedIds.Remove(tid);
 057            return base.OnDisconnectedAsync(exception);
 058        }
 59    }
 60    public static class UserHandler
 61    {
 62        public static Dictionary<string, string> ConnectedIds = new Dictionary<string, string>();
 63    }
 64
 65
 66}