Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion SocialNetwork/Controllers/PostController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
using SocialNetwork.Repositories.GenericRepository;
using AutoMapper;
using System;
using Microsoft.AspNetCore.SignalR;
using SocialNetwork.SignalRChatHub;

namespace SocialNetwork.Controllers
{
Expand All @@ -19,10 +21,14 @@ public class PostsController : ControllerBase
private readonly IUnitOfWork unitOfWork;
private readonly IMapper mapper;

public PostsController(IUnitOfWork unitOfWork, IMapper mapper)
private IHubContext<ChatHub, INotifyHubClient> hubContext;

public PostsController(IUnitOfWork unitOfWork, IMapper mapper,
IHubContext<ChatHub, INotifyHubClient> hubContext)
{
this.unitOfWork = unitOfWork;
this.mapper = mapper;
this.hubContext = hubContext;
}

// GET api/posts/79
Expand Down Expand Up @@ -134,6 +140,11 @@ public async Task<ActionResult> AddPost([FromBody]Post post)
if (!ModelState.IsValid) return BadRequest();
await unitOfWork.PostRepository.Create(post);
await unitOfWork.Save();

post.Profile = await unitOfWork.ProfileRepository.GetById(post.ProfileRef);
var postDto = mapper.Map<Post, PostDto>(post);
await hubContext.Clients.Group(postDto.profile.Login).AddNewPostToNews(postDto);

return Created("api/post", post);
}
return BadRequest();
Expand Down
66 changes: 63 additions & 3 deletions SocialNetwork/SignalRChatHub/ChatHub.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,74 @@
using Microsoft.AspNetCore.SignalR;
using System.Collections.Generic;
using System.Threading.Tasks;
using System;

using SocialNetwork.Repositories;
using SocialNetwork.Repositories.GenericRepository;

namespace SocialNetwork.SignalRChatHub
{
public class ChatHub : Hub
public class ChatHub : Hub<INotifyHubClient>
{
public async Task SendMessage(string user, string message)
private readonly static ConnectionMapping connectionClients =
new ConnectionMapping();
private readonly IUnitOfWork unitOfWork;

public ChatHub(IUnitOfWork unitOfWork)
{
this.unitOfWork = unitOfWork;
}
public async Task AddNewClient(int id)
{
var profileClient = await unitOfWork.ProfileRepository.GetById(id);
var subscribers = await unitOfWork.ProfileRepository.GetSubscribersById(id);
var bloggers = await unitOfWork.ProfileRepository.GetBloggersById(id);

connectionClients.Add(profileClient.Login, Context.ConnectionId);

foreach (var profile in subscribers)
{
if (connectionClients.ClientOnline(profile.Login))
{
var connectionsId = connectionClients.GetConnections(profile.Login);
foreach (var connectionId in connectionsId)
await Groups.AddToGroupAsync(connectionId, profileClient.Login);
}
}

foreach (var profile in bloggers)
{
if (connectionClients.ClientOnline(profile.Login))
{
await Groups.AddToGroupAsync(Context.ConnectionId, profile.Login);
}
}
}
public async Task DeleteClient(int id)
{
var profileClient = await unitOfWork.ProfileRepository.GetById(id);
var subscribers = await unitOfWork.ProfileRepository.GetSubscribersById(id);
var bloggers = await unitOfWork.ProfileRepository.GetBloggersById(id);

foreach (var profile in subscribers)
{
if (connectionClients.ClientOnline(profile.Login))
{
var connectionsId = connectionClients.GetConnections(profile.Login);
foreach (var connectionId in connectionsId)
await Groups.RemoveFromGroupAsync(connectionId, profileClient.Login);
}
}

foreach (var profile in bloggers)
{
if (connectionClients.ClientOnline(profile.Login))
{
await Groups.RemoveFromGroupAsync(Context.ConnectionId, profile.Login);
}
}

await Clients.All.SendAsync("ReceiveMessage", user, message);
connectionClients.Remove(profileClient.Login, Context.ConnectionId);
}
}
}
76 changes: 76 additions & 0 deletions SocialNetwork/SignalRChatHub/ConnectionMapping.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
using System.Collections.Generic;
using System.Linq;

namespace SocialNetwork.SignalRChatHub
{
public class ConnectionMapping
{
private readonly Dictionary<string, HashSet<string>> _connections =
new Dictionary<string, HashSet<string>>();

public int Count
{
get
{
return _connections.Count;
}
}

public bool ClientOnline(string login)
{
HashSet<string> connections;
return _connections.TryGetValue(login, out connections);
}

public void Add(string key, string connectionId)
{
lock (_connections)
{
HashSet<string> connections;
if (!_connections.TryGetValue(key, out connections))
{
connections = new HashSet<string>();
_connections.Add(key, connections);
}

lock (connections)
{
connections.Add(connectionId);
}
}
}

public IEnumerable<string> GetConnections(string key)
{
HashSet<string> connections;
if (_connections.TryGetValue(key, out connections))
{
return connections;
}

return Enumerable.Empty<string>();
}

public void Remove(string key, string connectionId)
{
lock (_connections)
{
HashSet<string> connections;
if (!_connections.TryGetValue(key, out connections))
{
return;
}

lock (connections)
{
connections.Remove(connectionId);

if (connections.Count == 0)
{
_connections.Remove(key);
}
}
}
}
}
}
13 changes: 13 additions & 0 deletions SocialNetwork/SignalRChatHub/INotifyHubClient.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using Microsoft.AspNetCore.SignalR;
using System.Threading.Tasks;


namespace SocialNetwork.SignalRChatHub
{
public interface INotifyHubClient
{
Task AddNewPostToNews(PostDto post);
Task DeleteClient();
Task OnDisconnected(bool d);
}
}
Loading