using Diligent.WebAPI.Data.Entities; using Diligent.WebAPI.Host.DTOs.Chat; using Diligent.WebAPI.Host.Mediator.Chat.Commands; using Diligent.WebAPI.Host.Mediator.Messages.Commands; using MediatR; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.SignalR; namespace Diligent.WebAPI.Host.Hubs { public class ChatHub : Hub { private readonly IDictionary _connections; private readonly IMediator _mediator; public ChatHub(IDictionary connections,IMediator mediator) { _connections = connections; _mediator = mediator; } //public override Task OnDisconnectedAsync(Exception? exception) //{ // if (_connections.TryGetValue(Context.ConnectionId, out UserConnection room)) // { // _connections.Remove(Context.ConnectionId); // Clients.Group(room.RoomId).ReceiveMessage(new ChatMessage { User = room.UserId, Message = $"{room.UserId} has left room" }); // } // return base.OnDisconnectedAsync(exception); //} // Not completed //[HubMethodName("SendMessageToUser")] //public async Task DirectMessage(string message) //{ // // Send message to another user // await Clients.User(message.User).ReceiveMessage(new ChatMessage { User = "Ermin", Message = message.Message }); //} [HubMethodName("SendMessageToGroup")] public async Task SendMessageToGroup(ChatMessage message) { // Find user's room and send message to everyone //if (_connections.TryGetValue(Context.ConnectionId, out UserConnection room)) if (_connections.TryGetValue(message.ConnId, out UserConnection room)) { await Clients.Group(room.RoomId).ReceiveMessage(new ChatMessage { User = room.UserId, Message = message.Message }); await _mediator.Send(new AddMessageCommand(room.RoomId, new Message {Content=message.Message,SenderId=message.UserId,Username=room.Username })); } } [HubMethodName("JoinRoom")] public async Task JoinRoom(UserConnection userConnection) { // Check is user in requested room var result = _connections.Where(x => x.Value.UserId == userConnection.UserId && x.Value.RoomId == userConnection.RoomId).FirstOrDefault(); // If user is not in room, add him // Return message "User has joined room" to all users in room if(result.Value == null) { await Groups.AddToGroupAsync(Context.ConnectionId, userConnection.RoomId); _connections[Context.ConnectionId] = userConnection; await Clients.Group(userConnection.RoomId).ReceiveMessage(new ChatMessage { User = userConnection.UserId, Message = $"{userConnection.Username} has joined room", ConnId = Context.ConnectionId }); } } [HubMethodName("LeaveRoom")] public async Task LeaveRoom(LeaveChatRoomDTO connection) { if (_connections.TryGetValue(connection.ConnId, out UserConnection room)) { _connections.Remove(Context.ConnectionId); await Clients.OthersInGroup(room.RoomId).ReceiveMessage(new ChatMessage { User = room.UserId, Message = $"{room.Username} has left room" }); await _mediator.Send(new RemoveUserFromGroupCommand(room.RoomId, room.UserId)); } } } }